C++ operator overloading

C++ Operator Overloading

operator overloading
C++ Function Overloading(for more click on link)

overloading in function refers to the use of the same thing for different purpose. C++ language also permits overloading of functions. This means that we can use the same functions names to create functions that performs a variety of different tasks. This is known as function polymorphism in Object oriented programming. using the concept of function overloading, we can design a family of function of functions with one function name but with different argument (parameters) list. The function would perform different operations depending on the argument list. The function would perform different operations depending on the argument(parameters) list in the function call. The correct function to be invoked is determined by checking the number of arguments and type of the arguments but not on the function type.

for example an overloaded add() function handles different types of data as shown below

// declarations
int add(int a, int b); //prototype 1
int add(int a, int b, int c); // prototype 2
double add(double x, double y) // prototype 3
double add(int p,double q); // prototype 4
double add(double p, int q); // prototype 5
// FUNCTION calls
std::cout<<add(7,10); // for uses prototype 1
std::cout<<add(67,34,23); // for uses prototype 2
std::cout<<add(34.5,23.6); // for uses prototype 3
std::cout<<add(12,34.7); // for uses prototype 4
std::cout<<add(34.7,12); // for uses prototype 5

C++ Operators Overloading

Overloading means assigning different meanings to an operation, depending on context. C++ language permits overloading of operators, thus allowing us to assign multiple meanings to operators. Actually we have used the concept of overloading in c language also. example the operator *(Astrik) whenever applied to a pointer variable, gives the value pointed to by the pointer. but this is also commonly used for multiplying two numbers. so the number and type of operands decide the nature of operation to follow

The input/output operators << and >> are good examples of operators overloading, although the build-in definition of the << operator is for shifting of bits, it is also used for displaying the value or displaying the string. This has been made possible by the header file iostream where a number of overloading definitions for << are included. Thus the statement

cout << 90.89;

invokes the definition for displaying a double type-value and cout << “welcome”;

invokes the definition for displaying a char value. However , none of these definations in iostream affect the built in meaning of the operator.

similarly, so we can say define additional meanings to others c++ operators. for example, we can define + operator to add two structures or objects. Almost all C++ operators can be overloaded with a few exceptions such as the member access operators(. and .*), conditional operators (?:), scope resolution operator(::) and the size operator(sizeof). Definitions for operator overloading are discussed

Operator that cannot be overloaded are as follows:

Scope operator (::)
Sizeof
member selector(.)
member pointer selector(*)
ternary operator(?:)

Syntax of Operator Overloading

return_type class_name : : operator op(argument_list)
{
// body of the function.
}

Where the return type is the type of value returned by the function. class_name is the name of the class. operator op is an operator function where op is the operator being overloaded, and the operator is the keyword.

Rules for Operator Overloading

Existing operators can only be overloaded, but the new operators cannot be overloaded. The overloaded operator contains atleast one operand of the user-defined data type.
We cannot use friend function to overload certain operators. However, the member function can be used to overload those operators.
When unary operators are overloaded through a member function take no explicit arguments, but, if they are overloaded by a friend function, takes one argument.
When binary operators are overloaded through a member function takes one explicit argument, and if they are overloaded through a friend function takes two explicit arguments.

C++ Operators Overloading Example

// program to overload the unary operator ++.

#include < iostream >
using namespace std;
class Test
{
private:
int num;
public:
Test(): num(8){}
void operator ++()
{
num = num+2;
}
void Print()
{
cout<<“The Count is: “< }
};
int main()
{
Test tt;
++tt; // calling of a function “void operator ++()”
tt.Print();
return 0;
}

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise