Publicité
Publicité

Contenu connexe

Publicité
Publicité

Oprerator overloading

  1. #include<iostream.h> #include<conio.h> class space { int x ,y,z; public: void getdata(int a, int b) { x=a; y=b; } void display() { cout<<"value of x "<<x<<endl; cout<<"value of y "<<y<<endl; } void operator-() { x=-x; y=-y; z=x+y; cout<<"Sum of x+y ="<<z<<endl; } }; void main() { clrscr(); space s; s.getdata(10,-20); s.display(); -s; s.display(); getch(); }
  2. class coord { int x, y; public: coord() {x=0, y=0;} coord(int i, int j) {x=i; y=j;} void get_xy(int &i, int &j) {i=x, j=y;} coord operator+(coord ob2); }; coord coord::operator+(coord ob2) { coord temp; temp.x=x+ob2.x; temp.y=y+ob2.y; return temp; } main() { int x, y; coord o1(10, 10), o2(4, 8), o3; o3=o1+o2; o3.get_xy(x, y) cout<<“o3 coordinates are: x: “<<x<<“ y: “<<y<<endl; }
Publicité