Binary OperatorWhen a member operator functionoverloads a binary operator, the functionwill have only one parameter.Consider:x+ yx: object that generates the call to theoperator functiony: parameter of the operator function
Binary Operator When a member operator function overloads a binary operator, the function will have only one parameter. Consider: x + y x: object that generates the call to the operator function y: parameter of the operator function
Example: coord classclass coord {int X, Y;/ coordinate valuespublic:/ Constructor function is overloadedcoordO {x = 0; y = 0;}coord(int i, int j) {x = i; y = j;)coord operator+(coord ob2);
Example: coord class class coord { int x, y; // coordinate values public: // Constructor function is overloaded coord() {x = 0; y = 0;} coord(int i, int j) {x = i; y = j;} coord operator+(coord ob2); }
Example: operator+()// Overload + relative to coord classcoord coord::operator+(coord ob2) (coord temp;temp.x = x + ob2.x;temp.y = y + ob2.y;return temp;
Example: operator+() // Overload + relative to coord class coord coord::operator+(coord ob2) { coord temp; temp.x = x + ob2.x; temp.y = y + ob2.y; return temp; }
Example: overloaded +int mainO (co00rd o1(10,10), 02(5,3), 03, 04;03 = 01 + 02;/ a string of additions is allowed.04 = 01 + 02 + 03;return O;7
Example: overloaded + int main() { coord o1(10,10), o2(5,3), o3, o4; o3 = o1 + o2; // a string of additions is allowed. o4 = o1 + o2 + o3; return 0; }
Example: coord classclass coord {int X, y;// coordinate valuespublic:coordO (x = 0; y = 0;)coord(int i, int j) {x = i; y = j})coord operator+(coord ob2);coord operator- (coord ob2)coord operator=(coord ob2)
Example: coord class class coord { int x, y; // coordinate values public: coord() {x = 0; y = 0;} coord(int i, int j) {x = i; y = j;} coord operator+(coord ob2); coord operator– (coord ob2); coord operator=(coord ob2); }