C++ operator=()

C++ operator=()
Techiio-author
Written by Sagar RabidasFebruary 7, 2022
8 min read
C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss C++ operator=()

C++ operator=()

Operator=() is an assignment operator overloading in c++. As we recognize that an operator overloading is used to redefine the operators to operate on the user-described statistics kind. An operator overloading in c++ is a static polymorphism or compile-time polymorphism. In c++ almost all operators can be overloaded, except some operators. So one of the operator overloadings is operator=(), which is a challenge operator overload that overloads the venture operator and redefines to operate on consumer-defined facts.

We know about the assignment operator that, it is Used 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 overloaded the assignment operator should be performed on the user-defined data types as well, 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:

20000

As inside the above code, the worker operator =(worker n ); function is described for a project operator overload, as right here this feature established the employee magnificence item as a parameter and it returns conventional item profits that's what assigned or copied to some other object while used mission operator as in code worker e3 = e1; as soon as compiler stumbles upon e3 = e1; statement it calls to the operator =(employee n ); defined characteristic as e3.Operator =(e1). Right here e1 object is handed as a parameter and e3 is the object on which the undertaking operator characteristic is referred to as, so e1. Revenue is assigned or copied to the e3 object.

Next, we rewrite the above C++ code to see what happen 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:

20000

As inside the above code, the employee operator =(worker n); characteristic is not described for a mission operator to be overload, but object e1 is copied to e3 as we got the equal output 20000. So the project operator is via default overloaded for person-defined gadgets, while another binary operator via default isn't overloaded like ‘+’, ‘-‘, ‘*’ etc.

Next, we rewrite the above c++ code to see what happen 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;
}

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;
}

C
C++
C++ operator
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance