C++ Operator Overloading – A Comprehensive Guide

Filed Under: C++
C Operator Overloading

Hello, folks! In this article, we will understand a very interesting yet magical power provided by C++Operator Overloading.


Getting started with Operator Overloading

Operator overloading provides the ability to a mathematical operator to perform other manipulation operations other than its original operation.

For example, the '+' operator is used for arithmetic addition purpose, but with the help of operator overloading, we can use ‘+’ operator to concatenate two strings at runtime efficiently.

Thus, C++ has the ability to make operators work into different dimensions to perform various operators other than the designated operation of its own.

Syntax:

Class_name operator_keyword operator_symbol(Class_name object)

We will get to know more about the syntax and usage further in this article.

Example:

#include<iostream>  
#include<conio.h>
#include<string.h>
using namespace std;  
  
class Concatenate  
{  
    char inp_str[100];  
public:  
    void enter_string() 
    {  
        cout<<"Input the string: ";  
        cin>>inp_str;  
    }  
    void show_string()  
    {  
        cout<<inp_str;  
    }  
    Concatenate operator+(Concatenate St)  
    {  
        Concatenate ob;  
        strcat(inp_str,St.inp_str);  
        strcpy(ob.inp_str,inp_str);  
        return ob;  
    }  
};  
int main()  
{  
   Concatenate st1,st2,res;   
    st1.enter_string();  
    st2.enter_string();  
    res=st1+st2;
    cout<<"The concatenated string is:\n";
    res.show_string();   
    return 0;  
}   

In the above example, we have overloaded ‘+’ operator to perform string concatenation. We have created an object of type Class and further used strcpy() function to copy the input string and strcat() function to associate it with the class object.

Output:

Input the string: Journal
Input the string: Dev
The concatenated string is:
JournalDev

General Rules of Operator Overloading

  • Operator overloading can overload only the predefined operators. No new operator can be created or overloaded.
  • The basic requirement of the operators cannot be changed. For example, for ‘+’ operator it is necessary for us to supply two variables to the function. Thus, this needs to be maintained.
  • The overloaded operators do not have default arguments.
  • The associativity and precedence of the operators cannot be changed.
  • While performing operator overloading, there has to be at least one operand of user-defined type.
  • We cannot overload the following operators: ‘.'(dot) operator, scope resolution operator(::), Ternary operator(?:) and sizeof operator.

Types of Operator Overloading in C++

There are broadly two types of Operator Overloading:

  1. Unary Operator Overloading: It works for only one operand.
  2. Binary Operator Overloading: It works for two operands.

1. Unary Operator Overloading

Unary Operator Overloading operators on a single operand and works with one class object respectively. Thus, we need not pass any argument to the unary overloading function.

Note: In case, if we use friend function, then the unary operator can have one argument. Other than that, if unary operator function represents a class/non-static function, then we no to pass zero arguments to the function.

Syntax: Unary Function Definition

return_type operator_keyword operator_symbol()
{
    //body
}

Syntax: Calling a unary operator overloading function

Class_name object;
operator_symbol object;

Example:

#include<iostream>  
#include<conio.h>
#include<string.h>
using namespace std;  
  
class Overload 
{  
    int a,b;  
public:  
    void enter_string() 
    {  
        cout<<"Input 1: ";  
        cin>>a;  
        cout<<"Input 2: ";  
        cin>>b;  
    }  
   
    void operator+()  
    {  
        a++;
        b++;
        cout<<"Incremented values:\n";
        cout<<a<<'\t'<<b;
    }  
};  
int main()  
{  
   Overload obj;
   obj.enter_string();
   +obj;
    return 0;  
}   

In the above example, we have overloaded ‘+’ operator to increment the values passed to the unary function.

Output:

Input 1: 100
Input 2: 200
Incremented values:
101	201

2. Binary Operator Overloading

Binary Operator Overloading function works for two operands and thus we need to pass a single argument to the binary overloading function.

Note: In case of friend function, we need to pass two arguments to the function.

Syntax: Binary operator overloading function

return_type operator_keyword operator_symbol(Class_name argument1)
{
    //body
}

Thus, now it must be clear for all of you, that the example in the beginning of this tutorial is of type binary overloading.

Syntax: Calling a unary operator overloading function

Class_name obj1, obj2, obj3;
obj3 = obj1 operator_symbol obj2;

Example:

#include<iostream>  
#include<conio.h>
#include<string.h>
using namespace std;  
  
class Overload  
{  
   
public:  
 int num1, num2; 
    void enter_num() 
    {  
        cout<<"Input 1: ";  
        cin>>num1;  
        cout<<"Input 2: ";  
        cin>>num2; 
    }  
     
    Overload operator+(Overload St)  
    {  
        Overload ob;  
        ob.num1 = this->num1 + St.num1;
        ob.num2 = this->num2 + St.num2;
        return ob;  
    }  
};  
int main()  
{  
   Overload A, B, res;  
   cout<<"Enter the input values for object A:\n";
   A.enter_num();
   cout<<"Enter the input values for object B:\n";
   B.enter_num();  
    res=A+B;
    cout<<"Result:\n";
    cout<<res.num1<<'\t'<<res.num2; 
    return 0;  
}   

In the above example, we have overloaded ‘+’ operator to add two class objects.

Output:

Enter the input values for object A:
Input 1: 10
Input 2: 10
Enter the input values for object B:
Input 1: 20
Input 2: 30
Result:
30	40

Operator Overloading at a glance!

  • Thus, Operator overloading redefines the usual or basic functionality of the operator.
  • It preserves the original function of the operators and work with user defined types only.
  • Operator overloading function can be used as class or friend function too.

Conclusion

Thus, in this article, we have understood the working of Operator overloading along with the different types of overloading functions in C++.


References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages