Updated April 11, 2023
Introduction to C++ operator=()
Operator=() is an assignment Operator overloading in C++. Operator overloading is used to redefine the operators to operate 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. Operator=() is an assignment Operator overload that overloads the assignment operator and redefines to perform the operation on user-defined data.
We know that the assignment operator assigns value to a variable represented by =(equal) sign; it is a binary operator with R-Value and L-value, the R-value assigned or copied to L-Value.
So when the assignment operator is overloaded, 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 that 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 –
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 returns 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 calls to the operator =(Employee n ); defined function as e3.operator =(e1). Here e1 object is passed as a parameter, and e3 is the object on which the assignment operator function is called, so e1.salary is assigned or copied to the e3 object.
Next, we rewrite the above C++ code to see what happens if we do not define the 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 overloaded, but object e1 is copied to e3 as we got the same output 20000. So the assignment operator is overloaded for user-defined objects, whereas other binary operators, by default not overloaded like ‘+,’ ‘-, ‘‘*’ etc.
Next, we rewrite the above c++ code to see what happens if we do not define the Operator +() function in class to perform the 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 get the below error –
Output:
Next, we rewrite the above C++ code to overload the Operator=() function where it copies 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 accepts the Employee class object as a parameter, and it updates the salary of the calling object by the salary of the passed object with the addition of 1000. So when an assignment operator is used as in the above code, Employee e3 = e1; the e3 salary is updated or copied 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 used to redefine the assignment operators to perform the operation on the user-defined data type.
Recommended Articles
This is a guide to C++ operator=(). Here we have discussed the introduction to Operator=() function in C++ and the programming examples. You may also have a look at the following articles to learn more –