Operator
Let’s start off by overloading a unary operator.
unary operators act on only one operand. (An operand is simply a variable acted on by an operator.)
Examples of unary operators are the increment and decrement operators ++ and –.
#include <iostream>
using namespace std;
class Counter
{
private:
unsigned int count;
public:
Counter() //constructor
{
count = 0;
}
unsigned int get_count() //return count
{
return count;
}
void operator ++ () //increment (prefix)
{
++count;
}
};
int main()
{
Counter c1, c2; //define and initialize
cout << “nc1=” << c1.get_count(); //display
cout << “nc2=” << c2.get_count();
++c1; //increment c1
++c2; //increment c2
++c2; //increment c2
cout << “nc1=” << c1.get_count(); //display again
cout << “nc2=” << c2.get_count() << endl;
return 0;
}
In this program we create two objects of class Counter, c1 and c2. The counts in the objects are displayed, they are initially 0. Then, using the overloaded ++ operator, we increment c1 once and c2 twice, and display the resulting values.
Here’s the program’s output:
c1=0 c2=0 counts are initially 0
c1=1 incremented once
c2=2 incremented twice
The statements responsible for these operations are
++c1;
++c2;
++c2;
The ++ operator is applied once to c1 and twice to c2.
Overloading Binary Operator
A Statement like,
C = Sum(A, B); // functional notation.
was used.The functional notation can be replaced by a natural looking expression
C = A + B; // arithmetic notation.
by overloading the + operator using an operator +() function.
A Simple program which demonstrate Binary overloading:
#include <iostream>
using namespace std;
class complex
{
float x;
float y;
public:
complex();
complex(float real, float img)
{
x = real;
y = img;
}
complex operator+(complex);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp.x = x + c.x;
temp.y = y + c.y;
return temp;
}
void complex:: display(void)
{
cout << x << “+ j” << y << endl;
}
int main()
{
complex c1, c2, c3;
c1 = complex(2.5, 3.5);
c2 = complex(1.6, 2.7);
c3 = c1 + c2;
c1.display();
c2.display();
c3.display();
return 0;
}
Lets see how operator+() function is implemented
complex complex::operator+(complex c)
{
complex temp;
temp.x = x + c.x;
temp.y = y + c.y;
return temp;
}
We should note the following features of this function:
1. It receives only one complex type argument explicitly.
2. It return complex type value
3. It is the member function of complex.
The function is expected to add two compex values and return complex value as a result but receive only one value as argument.Where does the other value come from? Now let us look at the statement that invokes this function:
C3 = C1 + C2; invokes operator+() function
We know that a member function can be invoked only by an object of the same class.Here, the object c1 takes the responsibility of invoking the function and c2 plays the role of an argument that is passed to the function.The above statement is equivalent to
C3 = C1.operator+(C2); Normal function call syntax
Therefore, in the operator+() function the data members of C1 are accessed directly and the data member of C2 (that is passed as an argument) are accessed using the dot operator.Thus, both the objects are available for the function.
Overloading Binary Operator using Friends
Friend function may be used in the place of member functions for overloading a binary operator, the only difference being that a friend function requires two arguments to be explicitly passed to it, while a member function requires only one.
The complex number program discussed in the previous section can be modified using friend operator function as follows:
1. Replace the member function declaration by the friend function declaration:
friend complex operator+(complex, complex)
2. Redefine the operator function as follows:
complex operator+(complex a, complex b)
{
return complex((a.x + b.x),(a.y + b.y));
}
In this case, the statement
C3 = C1 + C2;
is euivalent to
C3 = operator+(C1,C2);
In most cases, we will get the same results by the use of wither a friend function or a member function.Why then an alternative is made available? There are certain situations where we would like to use a friend function rather than a member function. For Example, consider a situation where we need to use two different type of operands for a binary operator, say one an object and another one is built-in type data as shown below:
A = B + 2; (or A = B * 2;)
where A and B are the objects of the same class.This will work for member function but the statement
A = 2 + B; (or A = 2 * B;)
will not work.This is because the left-hand operand which is responsible for invoking the member function should be an object of the same class.However, friend function allows both approaches. How?
It may be recalled that an object need not be used to invoke a friend function but can be passed as an argument.Thus, we can use the friend function with a built-in type data as a left-hand operand and object as right-hand operand.
Class time
{
int r;
int i;
public:
friend time operator + (const time &x, const time &y ); // operator overloading using friend
time ( )
{
r = 0;
i = 0;
}
time (int x, int y)
{
r = x;
i = y;
}
};
time operator + (const time &x, const time &y)
{
time z;
z.r = x.r +y.r;
z.i = x.i + y.i;
return z;
}
main ( )
{
time x,y,z;
x = time (5,6);
y = time (7,8);
z = time (9, 10);
z = x + y; // addition using friend function +
}