Assignment Operator Overloading in C++
Let’s see the Assignment Operator Overloading in Object oriented Programming (OOP) with C++. Before this we have studied about unary operator overloading and Binary operator overloading.
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 36 37 | #include<iostream> #include<conio.h> using namespace std; class T4Tutorials { private: int x, y; public: void DataByUser () { cout <<" Please enter the value of x and y for object1.."<<endl; cin>> x>>y; } void operator = (T4Tutorials obj) { x= obj.x; y= obj.y; } void display () { cout<<" value of x="<<x<<endl; cout<<" value of y="<<y<<endl; } }; int main () { T4Tutorials Object1, Object2; Object1.DataByUser (); Object1.display (); Object2=Object1; cout<<"after assigning values of object 1 to object 2."<<endl; Object2.display(); getch (); } |
Output
Please enter the value of x and y for object1.
value of x= 3
value of y= 4
after assigning values of object 1 to object 2.
value of x= 3
value of y= 4