Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/myscript/public_html/classes/classdb.php on line 42
Operator Overloading - C++ Tutorial

Operator Overloading in C++

What is Operator Overloading in C++?


Operator Overloading is a method to define additional task or special meaning to an operator in refernce to an class. Operators that cannot be overloaded are class member access operator (.,.*), scope resolution operator "::", "sizeof" operator and conditional operator "?:".

Example :

#include <iostream.h> using namespace std; class space
int x, y, z;
void getdata(int a, int b, int c);
void display(void);
void operator-();
}; void space :: getdata(int a, int b, int c)
} void space :: display(void)
cout << x << "";
cout << y << " ";
cout << z << " ";
} void space :: operator-()
} int main()
space S;
S.getdata(10, -20, 30);
cout << "Values before overloading:: " << "\n";
cout << "\n";
cout << "Values after overloading:: " << "\n";
return 0;

Result :

Values before overloading::
10-20 30
Values after overloading::
-10 20-30

In the above example the unary "-" operator is used to overload the objects got using "getdata()" of the class space. Usually the unary "-" operator is changes the sign, but here it is done for the objects of an class "space".

This is operator overloading in C++.

C++ Tutorial

Ask Questions

Ask Question