Assignment Operator Overloading in C++
What is assignment operator overloading in C++?
The assignment operator is a binary operator that is used to assign the value to the variables. It is represented by equal to symbol(=). It copies the right value into the left value i.e the value that is on the right side of equal to into the variable that is on the left side of equal to.
Overloading assignment operator in C++
- Overloading assignment operator in C++ copies all values of one object to another object.
- The object from which values are being copied is known as an instance variable.
- A non-static member function should be used to overload the assignment operator.
The compiler generates the function to overload the assignment operator if the function is not written in the class. The overloading assignment operator can be used to create an object just like the copy constructor. If a new object does not have to be created before the copying occurs, the assignment operator is used, and if the object is created then the copy constructor will come into the picture. Below is a program to explain how the assignment operator overloading works.
C++ program demonstrating assignment operator overloading
#include <iostream> using namespace std; class Length { private: int kmeter; int meter; public: Length() //default constructor { kmeter = 0; meter = 0; } Length(int km, int m) //overloaded constructor. { kmeter= km; meter = m; } void operator = (const Length &l) { kmeter = l.kmeter; meter = l.meter; } //method to display length. void displayLength() { cout << kmeter << " Kms "<< meter << " meters" << endl; } }; int main() { // Assigning by overloading contructor Length len1(1, 100); Length len2(2, 200); cout <<"Len1 length: "; len1.displayLength(); cout <<"Len2 length: "; len2.displayLength(); // overloading assignment operator to copy values len1 = len2; cout << "\nLen1 Length: "; len1.displayLength(); cout << "Len2 Length: "; len2.displayLength(); return 0; }
Output:
Len1 length: 1 Kms 100 meters Len2 length: 2 Kms 200 meters Len1 Length: 2 Kms 200 meters Len2 Length: 2 Kms 200 meters
Example 2
// Program to overload assignment (=) operator to // copy height of Student 1 to Student 2 #include using namespace std; class Height { private: int feet; // 0 to infinite int inches; // 0 to 12 public: // required constructors Height() { feet = 0; inches = 0; } Height(int f, int i) { feet = f; inches = i; } void operator = (const Height &h) { feet = h.feet; inches = h.inches; } // method to display height void displayDistance() { cout << feet << " feet " << inches << "inches " << endl; } }; int main() { Height H1(6, 2), H2(5, 10); cout << "Student 1 Height : "; H1.displayDistance(); cout << "Student 2 Height : "; H2.displayDistance(); // use assignment operator H1 = H2; cout << endl; cout << "Student 1 Height : "; H1.displayDistance(); cout << "Student 2 Height : "; H2.displayDistance(); return 0; }
Output
Student 1 Height : 6 feet 2inches Student 2 Height : 5 feet 10inches Student 1 Height : 5 feet 10inches Student 2 Height : 5 feet 10inches
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment