Created
December 5, 2010 13:31
-
-
Save popmentos/729079 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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