Operator Overloading in C++: Types With Examples
Did you know that in C++, you can simply change the behavior of operators on user-defined types such as structures and objects? This magical approach is called operator overloading in C++. It is a way to conveniently change a particular operator’s functionality to perform different tasks. In this blog, we will learn about operator overloading, its different types, methods, approaches, and benefits.
What is Operator Overloading in C++?
C++ overloading is one of the most impressive features of C++. This function can be easily used to add meaning to operators such as +, -, *, /, =, etc., by overloading them. These operators are originally meant to function with only common data types such as char, void, float, int, etc.
Overloading is considered an essential concept in C++ by many developers. An operator can be overloaded to provide it with the user-defined meaning. This phenomenon is a type of polymorphism. The compiler performs logical, mathematical, and other operations based on the symbol used as an operator.
The Syntax for Operator Overloading in C++
Return_Type classname :: operator op(Argument list)
{
Function Body
}// This can be done by declaring the function
In the above syntax,
- Return_Type is the value type that has to be returned to another object.
- Operator op is referred to as the function where an operator is a keyword.
- op is the operator that has to be overloaded.
Methods to Implement Operator Overloading in C++
There are two different methods to implement operator overloading. These methods are:
- Member Function: This method of operator overloading is in the scope of the class in which the function is declared.
- Friend Function: This method of operator overloading is referred to as a non-member function inside a class. It has permission to access both types of members, including private and protected members.
Rules for C++ Operator Overloading
Here are some rules to remember when performing C++ operator overloading.
- For successful overloading of an operator in C++, one of the operators, at least, must be a class object of the user-defined type.
- New operators cannot be overloaded in C++. We are only allowed to overload existing operators.
- Some of the operators in C++ cannot be overloaded using the friend function. However, these operators can be overloaded with the help of the member function.
Types of Operator Overloading in C++ With Examples
The different types of C++ operator overloading are as follows:
1. Overloading Unary Operator
In the unary operator overloading function, the arguments should not be passed. It only works with one class object. In simple terms, unary operator overloading is a type of operator overloading performed on a single operand.
For example, the class ‘Length’ consists of two member objects. These member objects are feet and inches. Now, let’s create a function by which our class Length should decrement the feet and inches value by 1. This way, we have a single operand of type Length.
Given below is how you can perform unary operator overloading.
INPUT
#include <iostream>
using namespace std;
class Length {
public:
int feet, inches;
Length(int f, int i)
{
this->feet = f;
this->inches = i;
}
void operator-()
{
feet--;
inches--;
cout << "\nFeet & inches(Decrement): " <<
feet << "'" << inches;
}
};
// Driver Code
int main()
{
Length l1(8, 9);
// Using (-) unary operator by the single operand
-l1;
return 0;
}
OUTPUT
Feet & inches(Decrement): 7’8
Explanation:
In the above code, we receive that no argument is passed and no return_type value has been returned by the function. This is because the unary operator only works on a single operand. The (-) operator alters the functionality into the member function.
2. Overloading Binary Operator
In the binary operator overloading function, only one argument should be present to be passed. This is a type of operator overloading performed on precisely two operands. You can learn more about operator overloading through this comprehensive C++ course.
Given below is how you can display the overloading of a binary operator (+) with the help of class Length having two distant objects.
INPUT
#include <iostream>
using namespace std;
class Length {
public:
int feet, inches;
Length()
{
this->feet = 0;
this->incheses = 0;
}
Length(int f, int i)
{
this->feet = f;
this->inches = i;
}
Length operator+(Length& l2)
{
Length l3;
l3.feet = this->feet + l2.feet;
l3.inches = this->inches + l2.inches;
return l3;
}
};
// Driver Code
int main()
{
Length l1(8, 9);
length l2(10, 2);
length l3;
// Using an overloaded operator
l3 = l1 + l2;
cout << "\nTotal Feet & inches: " <<
l3.feet << "'" << l3.inches;
return 0;
}
OUTPUT
Total Feet & inches: 18’11
3. Overloading Binary Operator Using a Friend Function
In binary operator overloading using a friend function, the operator overloading function must be present prior to the friend function. Also, the function has to be declared in the scope of the class. In a binary operator, the friend operator function usually takes two different parameters and alters one parameter with the help of a unary operator.
The working and implementation of this approach are quite similar to the binary operator. The only difference is that this type of operator function is implemented outside of the scope of the class.
Given below is how you can use binary operator overloading with the help of the friend function.
INPUT
#include <iostream>
using namespace std;
class Length {
public:
int feet, inches;
Length()
{
this->feet = 0;
this->inches = 0;
}
Length(int f, int i)
{
this->feet = f;
this->inches = i;
}
friend Length operator + (Length&,
Length&);
};
Length operator+(Length& l1,
Length& l2)
{
Length l3;
l3.feet = l1.feet + l2.feet;
l3.inches = l1.inches + l2.inches;
return l3;
}
// Driver Code
int main()
{
Length l1(8, 9);
Length l2(10, 2);
Length l3;
// Using an overloaded operator
l3 = l1 + l2;
cout << "\nTotal Feet & inches: " <<
l3.feet << "'" << l3.inches;
return 0;
}
OUTPUT
Total Feet & inches: 18’11
Explanation:
In the above program, the operator function is performed outside the scope of the class by declaring the process as a friend function. In this way, we can successfully overload an operator and perform certain operations.
Advantages of Operator Overloading in C++
Some of the many advantages of operator overloading are:
- Programmers can use notation much closer to the target domain.
- It provides similar support to the built-in user-defined functions in C++.
- Its process makes the code simple to understand.
Conclusion
Operator overloading in C++ gives you an extra edge while playing with different functions in the C++ language. It makes written code easier for programmers to understand. It also helps the user class provide an intuitive interface. This approach makes it feasible for different templates to perform equally well with the built-in types and classes.