"Rama" writes:
wht is operator overloading, how you overload an operator
Operator overloading is a means of calling a class member function when an
operator occurs in the source code. It allows nicer looking source code
when there is some reasonable similarity between the operator symbols and
some function to be performed on an object. One of the more appealing
examples is concatenation of a string, which could be codified as '+'.
Another nice one is that operators can be applied to complex numbers where
there is a one to one correlation between most of the arithmetic symbols for
real numbers and complex numbers. Beware, it is easy to be tempted to let
operator overloading run amok and use it where there is a dubious
relationship between the operators and the resulting action.
A simple example is as follows:
---------------------------
#include <iostream>
using namespace std;
class C
{
public:
C(int na) {n = na;}
bool operator>(C rhs); // rhs - right hand side
private:
int n;
};
//-----------------------
bool C::operator>(C rhs)
{
if(n > rhs.n)
return true;
else
return false;
}
//=====================
int main()
{
C c(1024);
C d(2048);
bool x, y;
x = c>d;
y = d>c;
cout << x << ' ' << y << endl;
cin.get();
}