1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
// vectors: overloading operators example
#include <iostream>
using namespace std;
class CVector {
public:
int x,y,z;
CVector () {};
CVector (int,int,int);
CVector operator + (CVector);
};
CVector::CVector (int a, int b, int c) {
x = a;
y = b;
z = c;
}
CVector CVector::operator+ (CVector parameter) {
CVector temp;
temp.x = x + parameter.x;
temp.y = y + parameter.y;
temp.z = z + parameter.z;
return (temp);
}
int main () {
CVector a (3,1,3);
CVector b (1,2,1);
CVector c (3,1,3);
CVector d;
d = a + b + c;
cout << d.x << "," << d.y << "," << d.z;
return 0;
}
|