There are a lot of things I would improve with that example. Here is my first shot at improving it:
Code:
// vectors: overloading operators example
#include <iostream>
using namespace std;
class CVector {
int x,y; // Note that these are now private. That is certainly optional though
public:
CVector() {} // No semicolon needed here
CVector(int x, int y) : x(x), y(y) {} // Constructor initialisation list. Learn it, love it.
// Putting these two inside the class means no need to write CVector:: any more
// Prefer the two parameter friend version of this operator
friend CVector operator + (const CVector& lhs, const CVector& rhs) { // Pass by const-reference
return CVector(lhs.x + rhs.x, lhs.y + rhs.y);
}
// This overload tells the class how to print itself instead of main having to know how!
friend ostream& operator << (ostream &os, const CVector &v) { // Const-correctness is always good
return os << v.x << "," << v.y;
}
};
int main () {
CVector a(3, 1);
CVector b(1, 2);
CVector c = a + b;
cout << c; // booyah!
return 0;
}
Warning: Untested code