Introduction to C++ operator=()
Operator=() is an assignment Operator overloading in C++. As we know that an operator overloading is used to redefines the operators to perform the operation on the user-defined data type. An Operator overloading in C++ is a static polymorphism or compile-time polymorphism. In c++ almost all operators can be overloaded, except few operators. So one of the operator overloadings is Operator=(), which is an assignment Operator overload that overload the assignment operator and redefine to perform the operation on user-defined data.
We know about the assignment operator that, it is Uses to assign value to a variable represented by =(equal) sign; it is a binary operator that has R-Value and L-value the R-value assign or copies to L-Value.
So when the assignment operator is overload the assignment operator should be performed on the user-defined data types as well, so all values of one object (user-defined data types) can be copied to another object.
Syntax
Below is the syntax mentioned:
return_type : : operator =(Parameter_list )
{
// statements to be executed to overload functionality of an assignment operator.
}
An operator is an operator function where the operator is a keyword which will be there for all operators overload and = is an assignment operator being overloaded.
Working and Examples of the Operator=() function in C++
Next, we write the C++ code to understand the Operator=() function working more clearly with the following example where we use Operator=() function to copy one object to another object, as below –

4.5 (8,479 ratings)
View Course
Example #1
Code:
#include <iostream>
using namespace std;
// create user define class
class Employee
{
public:
// declar instance variable
int salary;
Employee( int sal )
{
salary = sal;
}
// Assignment Operators Overloading
Employee operator =(Employee n)
{
Employee temp = n.salary;
return temp;
}
};
int main()
{
// create user deined objects
Employee e1( 20000 );
Employee e2( 25000 );
Employee e3 = e1;
cout<< e3.salary;
return 0;
}
Output:
As in the above code the Employee operator =(Employee n ); function is defined for an assignment operator overload, as here this function accepted the Employee class object as a parameter and it return accepted object salary that is what assign or copy to another object when used assignment operator as in code Employee e3 = e1;. Once compiler encounter e3 = e1; statement it call to the operator =(Employee n ); defined function as e3.operator =(e1). Here e1 object is passes as a parameter and e3 is the object on which the assignment operator function is called, so e1.salary is assign or copy to e3 object.
Next we rewrite the above C++ code to see what happen if we not define Operator =() function in class to copy one object to another object, as below –
Example #2
Code:
#include <iostream>
using namespace std;
// create user define class
class Employee
{
public:
// declar instance variable
int salary;
Employee( int sal )
{
salary = sal;
}
// No Assignment Operators Overloading
};
int main()
{
// create user deined objects
Employee e1( 20000 );
Employee e2( 25000 );
Employee e3 = e1;
cout<< e3.salary;
return 0;
}
Output:
As in the above code the Employee operator =(Employee n); function is not defined for an assignment operator to be overload, but object e1 is copied to e3 as we got same output 20000. So the assignment operator is by default overloaded for user defined objects, where as other binary operator by default not overloaded like ‘+’, ‘-‘, ‘*’ etc.
Next we rewrite the above c++ code to see what happen if we not define Operator +() function in class to perform addition of object, as below –
Example #3
Code:
#include <iostream>
using namespace std;
// create user define class
class Employee
{
public:
// declar instance variable
int salary;
Employee( int sal )
{
salary = sal;
}
// No addition Operators Overloading
};
int main()
{
// create user deined objects
Employee e1( 20000 );
Employee e2( 25000 );
// addition operator is using on define object which give comple time error here
Employee e3 = e1 + e2;
cout<< e3.salary;
return 0;
}
Once we compile the above code we gets below error –
Output:
Next we rewrite the above C++ code to overload the Operator=() function where it copy one object to another object with some operation, as below –
Example #4
Code:
#include <iostream>
using namespace std;
// create user define class
class Employee
{
public:
int salary;
Employee( int sal )
{
salary = sal;
}
// Assignment Operators Overloading
void operator =(Employee n)
{
salary = n.salary + 10000;
}
};
int main()
{
// create user deined objects
Employee e1( 20000 );
Employee e2( 25000 );
e2 = e1;
cout<< e2.salary;
return 0;
}
Output:
As in the above code the Employee operator =(Employee n); function is defined for an assignment operator overload, as here this function accepted the Employee class object as a parameter and it update the salary of the calling object by salary of the pass object with addition of 1000. So when an assignment operator used as in the above code Employee e3 = e1; the e3 salary update or copy by e1 object salary plus 10000. Therefore the output is 30000 not just 20000.
Conclusion
Operator=( ) overload an assignment Operator in C++ which can be uses to redefines the assignment operators to perform the operation on the user-defined data type.
Recommended Articles
This is a guide to C++ operator=(). Here we discuss the introduction to Operator=() function in C++ along with the programming examples. You may also have a look at the following articles to learn more –