To understand the concept of overloading an operator, we are going to implement the most basic and commonly overloaded operator that we use, the “+” operator. Normally the “+” operator is used to add two numbers. But if we write it between a string and a number, it will concatenate both of them. This is a fine example of overloading a unary operator.
In the above C# program, we demonstrated a simple function in which we use the “+” operator to add 2 numbers in the first line, and then in the second line, we use the same operator to add a string and a number together this word result in the concatenation of the string and a number as we are overloading the operator to do so.
The output of the above program showcases the dual nature of the “+” operator; in the first operation, it is simply adding two numbers for the result as 4, but in the other operation, the “+” operator is acting as an agent of concatenating a string to a number as it is pre-defined in the C# programming language.
Now we will look at some examples regarding the overloading of an operator in which we will use the “operator” keyword to state it as the overloading of a certain operator.
Example 01: Overloading the “-” Operator in Ubuntu 20.04
In this example, we will be focusing on the extensive overloading of an operator through the inheritance of the overloading methods. In this case, the “-” operator will be overloaded. The value of an integer variable will be allocated to the “-” operator; however, the typical operation of the “-” will be altered by overloading. We will use algebra to translate the “-” operator into a sign that can be given to a number.
In the above example, we have two integer variables and substituted their values other two integers. Then we created the overloading function with the help of the “operator” keyword in which the conventional operation of the “-” operator was redefined. Then we called the overloaded operator in the main program to see the difference between the normal and overloaded operator. After compiling and executing the above program, we will get the following result:
As we can see that the operator is now overloaded, and the predefined function from the overloaded method is carried out on the last call.
Example 02: Overloading the “+” Operator in Ubuntu 20.04
As we discussed earlier, the “+” operator is overloaded and can perform multiple operations, but it can still be overloaded through methods and inheritance of class objects. In this example, we will be overloading the “+” operator in which we will be making the “+” operator into adding two objects of a class rather than numbers which is the predefined function of the “+” operator. The “+” operator will be overloaded by using the “operator” keyword method.
In this C# program, we will be creating a method for overloading the “+” operator. First, we will create a class and call its object in the overloading method. These objects will be used in defining the functionality of the “+” operator, which is adding two objects to a class with the help of the “+” operator. Then the Display method will be created, which will display the “num” object. In the main program, we will create new objects and assign values to the classes and then use the overloaded “+” operator to add two objects to each other. After compiling the above program, we will get the output as shown below:
The object values of the class are shown in the output, which are “5” and “10,” and the sum of these objects is “15,” indicating that we have now overloaded the “+” operator to add two objects of the class and sum them for a result, much like adding two integers ordinarily.
Example 03: Overloading the “*” Operator in Ubuntu 20.04
The method for overloading the “*” operator will be discussed in this example. The operator keyword will be used to provide an overloading method for the “*” operator. The “*” operator’s fundamental job is to multiply two integers, but in this example, we’ll overload it by defining it to multiply two objects of a class and treat them as two numbers.
We have created a public method in which we have assigned an integer to an object. Then we created a public method for overloading the “*” operator by using the operator keyword before the symbol and the parameters. The parameters of this function are the objects of the previous class; hence it is inheriting the previous class entity, which is then being multiplied by each other in a new object, and this object will be returned from this method. In the main function, we will create three new objects and assign two of them some values in the parameter. Then these objects will be multiplied using the overloaded operator, and the result will be displayed on the how output screen. After successfully compiling and executing the above program, you will get the following result, as shown in the snippet:
As we can see that the first two numbers are displayed using the display function, which returns the objects of the Exam class. Then we have the resultant value of these two objects’ multiplication which is “50,” although the “*” operator only takes the product of two numbers due to our overloading, the operator successfully multiplied two objects as well.
Example 04: Overloading the Equality Operator in Ubuntu 20.04
Now we will be overloading the equality operator in the C# programming language. In this example, the equality operator will be used to compare objects of a class rather than its normal function for assigning values to variables. The object’s values will be the same in this scenario, but to differentiate, we will use a different reference and see the difference in the result of this operation.
We will build a class “Exam” in the aforementioned C# program and initialize certain variables in it, which we will use in the intrinsic function “Display(),”. Following that, we will give equivalent numbers to two objects of the “Exam” class and compare them to each other since the third item was referred from the second, resulting in an equal result. On the Ubuntu 20.04 command line terminal, we will test this feature by running the aforementioned application.
As we can see that the first comparison was not a success, but the second one was because they had the same reference.
In this article, we have discussed the basic concept of Operator Overloading. The C# programming language allows the operator loading concept and even has already adapted this in regard to the “+” operator, which adds numbers and concatenates strings as well. The overloadable operators were discussed and implemented in the Ubuntu 20.04 environment to have a better understanding of how overloading methods are created.