Operator Overloading in C++
What is Operator Overloading in C++?
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 "::"
operator and conditional operator "?:"
#include <iostream.h> using namespace std; class space
int x, y, z;
void getdata(int a, int b, int c);
}; void space :: getdata(int a, int b, int c)
} void space :: display(void)
cout << x << "";
cout << y << " ";
cout << z << " ";
} void space :: operator-()
} int main()
S.getdata(10, -20, 30);
cout << "Values before overloading:: " << "\n";
cout << "\n";
cout << "Values after overloading:: " << "\n";
Values before overloading::
Values after overloading::
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++.