Assignment Operator Overloading in C++

C++ is a very versatile language due to its diversity of providing different functions, data structures usages, and many more. One of the functionalities it provides is Operator overloading. This means a single operator can be overloaded in a program for multiple functionalities at a time. Thus, the assignment “=” operator can also be overloaded in C++ code to do multiple things. Therefore, we will be using the assignment operator, i.e., “=” for overloading in the C++ language. So, let’s get started by login in from the Linux system.

Let’s have a fresh start to this tutorial with the opening of a Linux shell. Linux system provides us with the built-in shell. Thus, there is no need to install a new one. We can simply open it in Ubuntu 20.04 with a small “Ctrl+Alt+T” shortcut while residing at its desktop. After this, a dark purple terminal will be opened. The very first step for doing a code is the generation of a new C++ file. This can be done using a “touch” query at the terminal, as shown below. To do code, you need to open this new file with some built-in editor provided by Linux. Thus, we are using the “GNU Nano” editor of Ubuntu 20.04. The command is also displayed below.

Example 01

You need to understand one thing about assignment operators that you must not use them in your main method when your class is not using some pointer. After opening the file within an editor, you need to add some header files of C++. These are required for standard input-output usage within the code and standard syntax. After the namespace, we have created a new class named “New” containing a data member pointer “p” of integer type. It also contains one constructor and two user-defined methods.

The constructor is used to designate some memory to a pointer according to the value passed to it as an integer, i.e., “I”. The user-defined “set()” function is used to set a new value to the address a pointer has. The last user-defined function, “show()” has been displaying the value a pointer address has. Now, the class has been closed, and the main() function starts. As we have used the pointer in the class, then we have to use the assignment operator in the main() function, but it’s not a user-defined operator. The object of class “New” have been created, i.e., n1 and n2. The first one is passing a value of 13 to the constructor. Operator overloading has been performed to reflect the changes of object n1 in object n2. When we call the function “Set” with object n1 and pass it a value 14, it will also be saved to object n2 as the overloading works. Thus, the show() method will be displaying the second value, i.e., 14, on the output screen upon the function call. The main method ends here.

Let’s just save the code completed in its file to make it executable and avoid inconvenience. The use of “Ctrl+S” will work for it. Now, a user needs to compile the code first after quitting the editor. The editor can be closed using the “Ctrl+X”. For compilation, a Linux user needs a “g++” compiler of the C++ language. Install it with the apt command. Now, we will be compiling our code with a simple “g++” keyword instruction along with the name of a C++ file displayed within the image. After the easy compilation, we are going to run the compiled code. The execution command “./a.out” shows 14 as the first value 13 has been overridden here.

Example 02

Within the above example, we have noticed that the change of value for one object reflects the change in another as well. This approach is not commendable. Thus, we will try to avoid such things within this example try to solve this issue as well. So, we have opened the C++ old file and made an update to it. So, After adding all the user-defined functions and a constructor, we have used the user-defined assignment operator with the class name. Within the user-defined assignment operator, we used the “if” statement to check the object for its self-assessment. The implementation of a user-defined assignment operator has been showing the overloading using the deep copy of the pointer here. When an assignment operator is used to overloading, the previous value will be saved at its place. The previous value can be accessed with the first object with whom it has been saved, while the other value can be simply accessed using the other object. Therefore, the object n1 stores the value 13 to a pointer “p” within the main function using the constructor. Then, we have performed the assignment operator overloading via the “n2 = n1” statement. The object n1 has been setting a new value 14 to pointer “p” using the function set(). But, due to the deep copy concept within the user-defined assignment operator function, the change of value using the n1 object doesn’t affect the value saved using the object n2. This is why, when we call a function show() with object n2, it will display a previous value 13.

After using the g+= compiler and the execution command on the code, we have got the value 13 in return. So, we have resolved the issue that we have got in the above example.

Example 03

Let’s have another simple example to see the working of the assignment operator in the overloading concept. Thus, we have changed the whole code of file “assign.cc” and you can see from the picture as well. We have defined a new class named “Height” with two integers type private data members, i.e., feet and inch. The class contains two constructors. The first one is to initialize values of both variables to 0 and the other one to take values by passing in parameters. The assignment operator function has been used to bind the object of a class with the operator. The show method is used to display the values of both variables in the shell.

Within the main() function, two objects have been created to pass the values to variable feet and inches. The show() function has been called with objects h1 and h2 to show the values. We have used the assignment operator to overload the contents of the first object h1 to the second object h2. The show() method will show the updated overloaded contents of the object h1.

After compilation and running the file code, we have got the results for objects h1 and h2 before assignment operator overloading as passed in parameters. While the third result shows the overloading of object h2 contents into object h1 fully.


This article brings up some quite simple and understandable examples to use the assignment operator overloading concept in C++.  We have also used the concept of deep copy in one of our examples to avoid a little issue of overloading. To sum up, we believe this article will be helpful to each individual looking for an assignment operator overloading help in C++.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.