Skip to content

Instantly share code, notes, and snippets.

@popmentos
Created December 5, 2010 13:31
Show Gist options
  • Save popmentos/729079 to your computer and use it in GitHub Desktop.
Save popmentos/729079 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
class Coord {
int x,y;
public:
Coord();
Coord(int);
Coord(int, int);
friend Coord &operator+(Coord ob1, Coord ob2);
void operator=(Coord &);
Coord operator-();
Coord& operator++();
Coord operator++(int);
void print();
};
Coord::Coord()
{
x = 0;
y = 0;
}
Coord::Coord(int a)
{
x = a;
y = a;
}
Coord::Coord(int a,int b)
{
x = a;
y = b;
}
Coord &operator+(Coord ob1, Coord ob2) {
Coord temp = Coord(ob1.x + ob2.x , ob1.y + ob2.y);
return temp;
}
void Coord::operator=(Coord &c)
{
this->x = c.x;
this->y = c.y;
}
Coord Coord::operator-() { // prefix -
this->x = this->x * (-1);
this->y = this->y * (-1);
}
Coord& Coord::operator++() { // prefix ++
this->x = this->x + 1;
this->y = this->y + 1;
return *this;
}
Coord Coord::operator++(int) {//postfix ++
Coord temp = *this;
this->x = this->x + 1;
this->y = this->y + 1;
return temp;
}
void Coord::print()
{
cout <<"(" << x << "," << y << ")" <<endl;
}
int main() {
Coord o1(10,10),o2;
cout << "o1=";
o1.print();
cout << "o2=8+o1" << endl;
o2=8+o1;
o2.print();
cout << "-o1" << endl;
-o1;
o1.print();
cout << "++o1" << endl;
++o1;
o1.print();
cout << "o1++" << endl;
o1++;
o1.print();
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment