Search This Blog

Saturday, September 18, 2010

Overloading binary operators

When a member operator function overloads a binary operator, the function will have only one parameter. This parameter will receive the object that is on the right side of the operator. The object on the left side is the object that generates the call to the operator function and is passed implicitly by this.
It important to understand that operator functions can be written with many variations. The examples given illustrate several of the most common techniques.
The following program overloads the + operator relative to the coord class. This class is used to maintain X, Y co-ordinates.
// overload the + relative to coord class
#include
using namespace std;
class coord {
int x, y; // coordinate values
public:
coord( ) { x = 0; y = 0; }
coord(int i, int j) { x = i; y = j; }
void get_xy(int &i, int &j) { i = x; j = y; }

coord operator+(coord ob2);
};
// Overload + relative to coord class.
coord coord::operator+(coord ob2) {
coord temp;
temp.x = x + ob2.x;
temp.y = y + ob2.y;
return temp;
}
int main( ) {
coord o1(10, 10), o2(5, 3), o3;
int x, y;
o3 = o1 + o2; //add to objects,
// this calls operator+()
o3.get_xy(x, y);
cout << "(o1+o2) X: " << x << ", Y: " << y << "\n";
return 0;
}
The reason the operator+ function returns an object of type coord is that it allows the result of the addition of coord objects to be used in larger expressions. For example,
o3 = o1 + o2;
o3 = o1 + o2 + o1 + o3;
(o1+o2).get_xy(x, y);
In the last statement the temporary object returned by operator+( ) is used directly. Of course, after this statement has executed, the temporary object is destroyed.
The following version of the preceding program overloads the - and the = operators relative to the coord class.
// overload the +, - and = relative to coord class
#include
using namespace std;
class coord {
int x, y; // coordinate values
public:
coord( ) { x = 0; y = 0; }
coord(int i, int j) { x = i; y = j; }
void get_xy(int &i, int &j) { i = x; j = y; }
coord operator+(coord ob2);
coord operator-(coord ob2);
coord operator=(coord ob2);
};

// Overload + relative to coord class.
coord coord::operator+(coord ob2) {
coord temp;
temp.x = x + ob2.x;
temp.y = y + ob2.y;
return temp;
}
// Overload - relative to coord class.
coord coord::operator-(coord ob2) {
coord temp;
temp.x = x - ob2.x;
temp.y = y - ob2.y;
return temp;
}
// Overload = relative to coord class.
coord coord::operator=(coord ob2) {
x = ob2.x;
y = ob2.y;
return *this; // return the object that is assigned
}
int main( ) {
coord o1(10, 10), o2(5, 3), o3;
int x, y;
o3 = o1 + o2; // add two objects,
// this calls operator+()
o3.get_xy(x, y);
cout << "(o1+o2) X: " << x << ", Y: " << y << "\n";
o3 = o1 - o2; //subtract two objects
o3.get_xy(x, y);
cout << "(o1-o2) X: " << x << ", Y: " << y << "\n";
o3 = o1; //assign an object
o3.get_xy(x, y);
cout << "(o3=o1) X: " << x << ", Y: " << y << "\n";
return 0;
}
Notice that to correctly overload the subtraction operator, it is necessary to subtract the operand on the right from the operand on the left. The second thing you should notice is that the function returns *this. That is, the operator= function returns the object that is being assigned to. The reason for this is to allow a series of assignment to be made. By returning *this the overloaded assignment operator allows objects of type coord to be used in a series of assignment,
o3 = o2 = o1;

Here another example where the + operator is overloaded to add an integer value to a coord object.
// overload the + for obj+int and as well as obj+obj
#include
using namespace std;
class coord {
int x, y; // coordinate values
public:
coord( ) { x = 0; y = 0; }
coord(int i, int j) { x = i; y = j; }
void get_xy(int &i, int &j) { i = x; j = y; }
coord operator+(coord ob2); // obj + obj
coord operator+(int i); // obj + int
};
// Overload + relative to coord class.
coord coord::operator+(coord ob2) {
coord temp;
temp.x = x + ob2.x;
temp.y = y + ob2.y;
return temp;
}
// Overload + for obj + int.
coord coord::operator+(int i) {
coord temp;
temp.x = x + i;
temp.y = y + i;
return temp;
}
int main( ) {
coord o1(10, 10), o2(5, 3), o3;
int x, y;
o3 = o1 + o2; // add two objects,
// calls operator+(coord)
o3.get_xy(x, y);
cout << "(o1+o2) X: " << x << ", Y: " << y << "\n";
o3 = o1 + 100; // add object + int
// calls operator+(int)
o3.get_xy(x, y);
cout<< "(o1+100) X: "<< x << ", Y: "<< y << "\n";
return 0;
}
You can use a reference parameter in an operator function. For example,
// Overload + relative to coord class using reference.
coord coord::operator+(coord &ob2) {
coord temp;
temp.x = x + ob2.x;

temp.y = y + ob2.y;
return temp;
}
One reason for using a reference in an operator function is efficiency. Another reason is to avoid the trouble caused when a copy of an operand is destroyed.
There are many other variations of operator function overloading.

No comments:

Post a Comment