Updated February 21, 2023
Introduction to Operator Overloading in C++
Operator overloading is used to redefine the operators to operate on the user-defined data type. An Operator overloading is a compile-time polymorphism. An operator overloading is a static polymorphism where operators are overloaded to perform some meaning on user-defined data types. In c++ almost all operator can be overloaded, except Scope operator ( :: ), member selector( . ), sizeof, ternary operator( ? : ) and member pointer selector( * ).
Syntax of Operator Overloading
return_type class_name : : operator op (Parameter_list)
{
// statements to be execute.
}
An operator op is an operator function where the operator is a keyword that will be there for all operator’s overload, and op is the operator being overloaded.
The lists of operators which can be overloaded are –
- Unary Operators Overloading
- Binary Operators Overloading
- Relational Operators Overloading
- Assignment Operators Overloading
- ++ and — Operators Overloading
- Input / Output Operators Overloading
- Subscripting [] Operator Overloading
- Class Member Access Operator -> Overloading
Examples of Operator Overloading in C++
Here are the following examples mentioned below:
Example #1 – Unary Operators Overloading
Code:
#include <iostream>
using namespace std;
class number
{
public:
int no;
number(int num)
{
no = num;
}
void print( )
{
cout<<no<<endl;
}
// - unary operators overloading
void operator - ( )
{
no = -no;
}
};
int main()
{
number n1(10);
cout<<" The number before call is : ";
n1.print( );
-n1;
cout<<" The number after call is : ";
n1.print( );
return 0;
}
Output:
Example #2 – Binary Operators Overloading
Code:
#include <iostream>
using namespace std;
class Employee
{
public:
int salary;
Employee( int sal )
{
salary = sal;
}
void print( )
{
cout<< salary <<endl;
}
// Binary Operators Overloading
Employee operator + ( Employee n )
{
return salary + n.salary;
}
};
int main()
{
Employee e1(20000);
Employee e2(25000);
Employee e3 = e1 + e2;
cout<<"Addition of salaries is "<< e3.salary;
return 0;
}
Output:
Example #3 – Relational Operators Overloading
Code:
#include <iostream>
using namespace std;
class Employee
{
public:
int salary;
Employee( int sal )
{
salary = sal;
}
void print( )
{
cout<<salary<<endl;
}
bool operator > ( Employee n )
{
if(salary > n.salary)
{
return true;
}
else
{
return false;
}
}
};
int main()
{
Employee e1(20000);
Employee e2(25000);
if(e1 > e2)
cout<<"Employee e1 slary is greater than employee e2. ";
else
cout<<"Employee e1 slary is lesser than employee e2. ";
return 0;
}
Output:
Example #4 – Assignment Operators Overloading
Code:
#include <iostream>
using namespace std;
class Employee
{
public:
int salary;
Employee( int sal )
{
salary = sal;
}
// Assignment Operators Overloading
Employee operator = ( Employee n )
{
Employee temp = n.salary;
return temp;
}
};
int main()
{
Employee e1(20000);
Employee e2(25000);
Employee e3 = e1;
cout<< e3.salary;
return 0;
}
Output:
Example #5 – ++ or — Operators Overloading
Code:
#include <iostream>
using namespace std;
class Employee
{
public:
int salary;
Employee(int sal)
{
salary = sal;
}
//post increment operator overloading
Employee operator ++ ( int )
{
Employee temp = salary;
salary++;
return temp;
}
//pre increment operator overloading
Employee operator ++( )
{
salary=salary+1;
Employee temp=salary;
return temp;
}
};
int main()
{
Employee e1(20000);
Employee e2 = ++e1;
cout<<"Pre increment salary: "<< e2.salary<<endl;
Employee e3(20000);
Employee e4=e3++;
cout<<"Post increment salary: "<<e4.salary<<endl;
return 0;
}
Output:
Example #6 – Input / Output Operators Overloading
Code:
#include <iostream>
using namespace std;
class Employee
{
public:
int salary;
Employee(int sal)
{
salary = sal;
}
// Output Operator Overloading
friend ostream &operator << ( ostream &output, const Employee &e ) {
output << "This object is of employee class. ";
return output;
}
};
int main()
{
Employee e1(20000);
cout<<e1;
return 0;
}
Output:
Example #7 – Subscripting Operator Overloading
Code:
#include <iostream>
using namespace std;
class Employee
{
public:
int allsalaries[50];
Employee(int sal)
{
static int count = 0;
allsalaries[count] = sal;
count++;
}
//Subscripting [] Operator Overloading
//return first employee salary
int &operator [] ( int i ) {
if( i > 10 ) {
cout << "Out of bound index. " <<endl;
return allsalaries[0];
}
}
};
int main()
{
Employee e1(20000);
Employee e2(21000);
int res = e1[1];
cout<< res;
return 0;
}
Output:
Example #8 – Class Member Access Operator -> Overloading
Code:
#include <iostream>
using namespace std;
class Employee{
int salary;
public:
Employee( int sal ){
salary = sal;
}
void print( )
{
cout << "Salary is "<< salary << endl;
}
//Class Member Access Operator -> Overloading
Employee *operator -> ( )
{
return this;
}
};
int main()
{
Employee e1( 25000);
e1.print();
e1 -> print();
return 0;
}
Output:
Conclusion
Operator overloading is a static polymorphism where operators are overloaded to perform the same operation (as per need) on user defined data types as on built-in data types. In C++, almost all operators can be overload, as we have seen above with an example.
Recommended Articles
This is a guide to Operator Overloading in C++. Here we discuss the Examples of Operator Overloading in C++ along with the Syntax, codes, and outputs. You may also have a look at the following articles to learn more –