You may have heard the term “Overload” in programming. But, the concept of overloading can also be used for operators in programming. The operator overloading only works for the user-defined methods in C++ programming. The operators are specified to perform the arithmetic operations only. While the overloading of operators can allow us to do some different tasks with the operators i.e., string concatenation and much more.
Meanwhile, we will only be using the overloading of the addition operator in our C++ code examples in this article today. So, make sure you have a C++ compiler configured on your system as we have been working on the Ubuntu 20.04 system.
After login from your Linux distribution, try opening the console application from the activity bar. It can also be done by the use of the keystroke “Ctrl+Alt+T” at the Linux desktop. Now, the console application is launched and you need to generate a new C++ file. This file will be used to make code in it. Thus the touch instruction will be helpful to create one.
After that, the file needs to be opened in some editor. Use the “GNU Nano” editor that is already built-in within Ubuntu 20.04 and can be used with the nano keyword along with the title of the C++ document. Both the queries have been demonstrated from the snapshot image attached.
We have been using the “++” unary operator in this illustration. The very first thing to do is, to add the header file with the “#include” keyword. Add the line “using namespace std” for standard syntax. A dummy class has been declared having the name “Test”. The class contains the initialization of private integer type variables i.e., num. The public type Constructor of class Test has been defined that has been initializing the value of variable “num” to 5 as the constructors are used for direct initializations.
The “++” operator has been overloaded with a void operator built-in while used as a prefix. It increments the value of the variable “num” and replaces the last value with the new one. The overloading has been done here.
On the next line, we have initialized a user-defined function named “show” using the standard cout statement in it. The cout is displaying the incremented value of variable “num” by addition operator overloading using the prefix method. The execution started from the main() method where we have created an object of the dummy class Test as “t”. The creation of object “t” will lead to the execution of constructor “test()” and initialization of variable “num” value i.e., 5, The “++t” line is incrementing the object value to call the operator ++() function to overload the addition operator. After the addition operator overloading, the show() function has been called to show the overloaded value on the shell. The main function ends here as well as the program code.
The standard g++ compiler will be used so far to compile the “overload.cc” C++ file and make it error-free. The compilation is successful and we have got no errors or messages in return. The execution of such programming codes doesn’t require any special packages but the “./a.out” commands. We have used it on our Ubuntu shell and got the result 6 as addition operator overloading applied on the variable “num”. This is how the addition operator overloading can be achieved within C++ using the overload prefix method.
Let’s begin with another example to overload the addition operator in the C++ programming code while using the Linux distribution i.e. Ubuntu 20.04 system. Thus, we have updated the document named “overload.cc” by simply launching it within the Editor. Your first task is to add the “io” stream header at the first line of C++ code within the “overload.cc” file. After this, use the namespace “std” as standard. Without it, our code is of no use. We have been declaring a dummy class “Test” as we did before in the above illustration. This class contains two private data members i.e., “r” and “I”. Both the variables are of integers type. The “r” is used for real value while the “I” will be used as the imaginary value.
After the data members’ initialization, we have declared public type functions in the class. The very first function is the constructor function of the class i.e. “Test”. This constructor is initializing the variable “r” and “I” with the initial value of “0” as the constructors are used for the direct initialization. Another class operator overloading function is used named as “Test operator” with “+” sign between its name and its parameter.
This overloading function has been taking the constant type of “test” class “object” as a parametric argument. This object value would be stored in the class type object variable “val”. This object type variable “obj” is being used here to overload the addition operator to add the old variable values with the new ones assigned by the objects i.e., r and i.
The operator overload function will be called when the objects will be concatenated within the main function and the resultant value will be returned to the main() function to store in the new object.
Here comes the user-defined method named “show()”. It will be used to display the real value and the imaginary variable value with “I” at its start to show that it’s imaginary while it is not real. The cout statement is used as the standard output statement here so far. The Class definition and declaration will be closed here after the creation of its data members and functions i.e., constructor function, overloading function, user-defined function.
The main() function has been coded in the file. The actual implementation and execution started from here. The class “test” objects have been created i.e., t1 and t2. With the first object creation, the constructor function “Test” is called and executed. The values have been passed to constructors and saved to the variables “r” and “I”. The second object does the same and new values have been stored within the new memory address. Another object “t3” of class Test has been created to overload the addition operator between the previously created two objects i.e., t1 and t2.
With the creation of this object t3, the operator overloading function of class “test” for the addition operator has been executed. The first object values have been summed up with the second object values using the “addition” operator overloading. Here, you need to understand that the first value of the first object has been summed up with the first value of a second object and vice versa.
The overall whole code of C++ for Addition operator overloading has been shown in the image below. Save your code with Ctrl+S to reflect the updates on the shell.
In return, we have got the output showing the real number and imaginary numbers summed up by addition operator overloading.
Within this article, we have discussed the use of addition operator overloading in C++ programming. We have used the concepts of constructors, overloading operator functions, main method, and objects to make it understand well. We hope this guide will be helpful.