// vectors: overloading operators example
#include <iostream>
using namespace std;
class CVector {
public:
int x,y;
CVector () {};
CVector (int,int);
CVector operator + (CVector);
};
CVector::CVector (int a, int b) {
x = a;
y = b;
}
CVector CVector::operator+ (CVector param) {
CVector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}
int main () {
CVector a (3,1);
CVector b (1,2);
CVector c;
c = a + b;
cout << c.x << "," << c.y;
return 0;
}
can some one help me understand this program. or you can also take another example to explain the concept Just my brain is not able to execute the instructions. so Please help me understandin this problem..thanks in advance:-)
CVector CVector::operator+ (CVector param) {
CVector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
becomes
This is a CVector function with the + operator and one argument; another CVector named 'param'.
Lets declare another CVector (which we will return later) to be 'temp'.
This 'temp' will contain the sum of both CVectors x values for its x...
And the sum of both y values for its y.
Finally, we shall return temp, which now contains the sum of both vectors.
Breadman's example is the same, but with one argument and unnecisaraly simple functions.
I don't know if this will help or confuse you more, when you call the opperator with syntax such as c = a + b; it is equivalent to the following call c = a.operator+(b);.
i have this prob here is the thing i can read and understand it and probly even remember the syntax to re write it with diff arguments and so forth but i dont think if you asked me to write a program to do just that on my own based off that knowlage i could
I don't know if this will help or confuse you more, when you call the opperator with syntax such as c = a + b; it is equivalent to the following call c = a.operator+(b);.