Open In App

C++ Assignment Operator Overloading

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Prerequisite: Operator Overloading

The assignment operator,”=”, is the operator used for Assignment. It copies the right value into the left value. Assignment Operators are predefined to operate only on built-in Data types.

  • Assignment operator overloading is binary operator overloading.
  • Overloading assignment operator in C++ copies all values of one object to another object.
  • Only a non-static member function should be used to overload the assignment operator.

We can’t directly use the Assignment Operator on objects. The simple explanation for this is that the Assignment Operator is predefined to operate only on built-in Data types. As the class and objects are user-defined data types, so the compiler generates an error.

Example 1:

int a = 5, b;
b = a;
cout<<b<<endl;

Output: 5

here, a and b are of type integer, which is a built-in data type. Assignment Operator can be used directly on built-in data types.

Example 2:

class C

{

};

int main() 
{
    C c1,c2;
    c1 = c2;
    return 0;
}

c1 and c2 are variables of type “class C”. Here compiler will generate an error as we are trying to use an Assignment Operator on user-defined data types.

The above example can be done by implementing methods or functions inside the class, but we choose operator overloading instead. The reason for this is, operator overloading gives the functionality to use the operator directly which makes code easy to understand, and even code size decreases because of it. Also, operator overloading does not affect the normal working of the operator but provides extra functionality to it.

Now, if the user wants to use the assignment operator “=” to assign the value of the class variable to another class variable then the user has to redefine the meaning of the assignment operator “=”.  Redefining the meaning of operators really does not change their original meaning, instead, they have been given additional meaning along with their existing ones.

Example 1:

C++




// C++ program for Assignment Operator overloading
#include <iostream>
using namespace std;
  
class Complex {
private:
    int real, img; // real, imaginary
  
public:
    // Parameterized Constructor
    Complex(int r, int i)
    {
        real = r;
        img = i;
    }
  
    // This is automatically called
    // when '=' operator is
    // used between C1 and C2.
  
    void operator=(const Complex& C)
    {
        real = C.real;
        img = C.img;
    }
  
    // function to print
    void print() { cout << real << "+i" << img << endl; }
};
  
int main()
{
    // Assigning by overloading constructor
    Complex C1(2, 3), C2(4, 6);
    
    cout << "BEFORE OVERLOADING ASSIGNMENT OPERATOR"<< endl;
    cout << "C1 complex number: ";
    
    C1.print();    
    
    cout << "C2 complex number: ";
    
    C2.print();     
    
    // overloading assignment operator to copy values
    C1 = C2;
    
    cout << "AFTER OVERLOADING ASSIGNMENT OPERATOR" << endl;
    cout << "C1 complex number: ";
    
    C1.print();    
    
    cout << "C2 complex number: ";
    
    C2.print();
    
    return 0;
}


Output

BEFORE OVERLOADING ASSIGNMENT OPERATOR
C1 complex number: 2+i3
C2 complex number: 4+i6
AFTER OVERLOADING ASSIGNMENT OPERATOR
C1 complex number: 4+i6
C2 complex number: 4+i6

Example 2:

C++




// C++ program to copy marks of Student 2 to Student 1
#include <iostream>
using namespace std;
  
class student {
private:
    int english;
    int math;
  
public:
    student(int e, int m)
    {
        english = e;
        math = m;
    }
    void operator=(const student& s)
    {
        english = s.english;
        math = s.math;
    }
  
    // method to display marks
    void marks()
    {
        cout << "English: " << english << ", Math: " << math
             << endl;
    }
};
  
int main()
{
    student s1(6, 2), s2(5, 10);
  
    cout << "Student 1 marks : ";
    s1.marks();
  
    cout << "Student 2 marks : ";
    s2.marks();
  
    // use assignment operator
    s1 = s2;
  
    cout << endl;
  
    cout << "Student 1 marks : ";
    s1.marks();
  
    cout << "Student 2 marks : ";
    s2.marks();
  
    return 0;
}


Output

Student 1 marks : English: 6, Math: 2
Student 2 marks : English: 5, Math: 10

Student 1 marks : English: 5, Math: 10
Student 2 marks : English: 5, Math: 10


Last Updated : 27 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads