C++ operator overloading is one of the most powerful features of C++ that allows a user to change the way the operator works. In this article, you will learn in depth about C++ operator overloading and its types with corresponding examples.
In C++ the meaning of existing operator can be extended to operate on user-defined data or class data.
C++ has the ability to prove the operators with a special meaning for a data type. So the mechanism of adding special meaning to an operator is called operator overloading.
Operators in C++ like +, -, *, /
can operate in datatypes like int
, float
, double
etc as predefined operational meanings. But these operators can’t operate in user-defined datatypes like objects without extension or adding some sort of code to alter their operational meaning.
Such a way of extending the operational functionality of certain operators in C++ is called operator overloading.
return_type operator operator_symbol (argument_list)
{
//body of function
}
To extend the meaning of an operator, an operator function is defined with a keyword operator
followed by the operator symbol.
Operator overloading in C++ can be achieved in following ways
Following are the operators that cannot be overloaded
Following is the list of overloadable operators
Category | Operators |
Airthmetic | + , – , * , / , % |
Bit-wise | & , | , ~ , ^ , << , >> |
Bit-wise assignment | &= , |= , ^= , <<== , >>= |
Relational | < , > , == , != , <= , >= |
Logical | || , && , ! |
Assignment | = |
Arithmetic assignment | -=, += , *= , /= , %= |
Unary | ++ , — |
Subscripting | [ ] |
Deference | * |
Function call | ( ) |
Address of | & |
Member access through member pointer | ->* |
Member access through object pointer | -> |
Dynamic Allocation and release | new, delete, new[ ], delete[ ] |
Comma | , |
As the name suggests, Unary operators operate on single operand or data.
Following are the examples of Unary operators:
Note : If increment/decrement operators are used before variable, they are called prefix operators i.e ++x
. And if increment/decrement operators are used after variable, they are called postfix operators i.e x++
.
++
and decrement --
operator overloading// unary_operator_overloading.cpp
#include <iostream>
using namespace std;
class check_count
{
public:
int count_plus;
int count_minus;
check_count()
{
count_plus = 0;
count_minus = 2;
};
void operator ++() { ++count_plus; } // count increment
void operator --() { --count_minus; } // count increment
};
int main()
{
check_count x, y; //creating objects
//before increment/decrement
cout << "x =" << x.count_plus<<"\n";
cout <<"y =" << y.count_minus<<"\n";
++x;
--y;
//after increment/decrement
cout<<"\nAfter increment/decrement\n";
cout<<"x ="<<x.count_plus<<"\n";
cout<<"y ="<<y.count_minus<<"\n";
return 0;
}
Output
As the name suggests, those operators which operate on two operands or data are called binary operators.
Here is an example to show how binary operators are overloaded in C++.
#include<iostream>
using namespace std;
class complex
{
private:
int real,imag;
public:
void getvalue()
{
cout<<"Enter the value of real number:";
cin>>real;
cout<<"Enter the value of imaginary number:";
cin>>imag;
}
complex operator+(complex obj)
{
complex temp;
temp.real=real+obj.real;
temp.imag=imag+obj.imag;
return(temp);
}
complex operator-(complex obj)
{
complex temp;
temp.real=real-obj.real;
temp.imag=imag-obj.imag;
return(temp);
}
void display()
{
cout<<real<<"+"<<"("<<imag<<")"<<"i"<<"\n";
}
};
int main()
{
complex c1,c2,c3,c4;
c1.getvalue();
c2.getvalue();
c3 = c1+c2;
c4 = c1-c2;
cout<<"Result is:\n";
c3.display();
c4.display();
return 0;
}
Output
This is all about Operator Overloading in C++.