Home / C++ / Operator Overloading in C++ with examples

Operator Overloading in C++ with examples

Operator overloading in C++ is a process in which we define the different implementations or working of an operator. This process enables the operator to perform operations depending upon the type of operands, it also allows the programmer to perform operations on user-defined data types by the basic operators of C++. As we know we can perform basic arithmetic operations such as addition (+), subtraction (-), multiplication (*) and division (/) on a primitive (built-in) data types such as int, float, double and long. Because the working of operators with these data types are already defined in a language. But these basic operations cannot be performed on the user-defined data types (objects of a class) unless operators are overloaded for them. If you try to use without overloading the required operator them it will generate error.

The below example will clear your mind.

For Example:-

If q1, q2 and q3 are the variables of integer type then you could perform the following operation without generating an error:

int q1=2;
int q2=3;
int q3= q1+q2;

This operation will perform successfully because compiler already know how to manipulate built-in data types with addition operator but if q1, q2 and q3 were the objects of some class just say players then the compiler would have generate error. This problem could overcome by the process of C++ operator overloading, which enables the addition operator to work with user-defined data types.

Overloading an operator in C++:-

In order to overload an operator, we have to define a special member function in a class with the operator keyword and then the symbol of an operator which is to be overloaded.

Syntax of Operator Overloading:-

Following is the syntax of overloading an operator:

Return_type operator Symbol()

{

Body of a function;

}

Return_type: It shows the value returned by the member function.

Operator: It is the keyword used for overloading an operator.

Symbol: The symbol of the required operator.

For Example:-

Void operator ++()

{

Function body;

}

Unary operators in C++:-

These are the type of operators which works with the single operand. These operators are overloaded in order to enhance their capabilities.

Following unary operators can be overloaded in C++ language:

+, -, *, ! , ~, &, ++, –, (), ->, new, delete

Overloading increment (++) Operator:-

This is a type of unary operator, it only works with single operand. By default it only work for the numeric values, it increases there value by 1. Increment operator works in two types of notations prefix and postfix. In below examples we will overload both in order to illustrate there use with user-defined data types.

Example of overloading prefix increment operator:

#include <iostream>
#include<conio.h>
using namespace std;
class Team
{
private:
int players;
public:
Team()
{
players=0;
}
void show_players()
{
cout<<"Number of players= "<<players<<"\n"<<endl;
}
void operator ++()
{
players= players+1;
}
};
int main()
{
Team t1;
cout<<"Number of Players before Increment : "<<endl;
t1.show_players();
++t1;
cout<<"Number of Players after Increment : "<<endl;
t1.show_players();
return 0;
}

Explanation:-

In above example we have overloaded the prefix increment operator to work with the objects of a user defined class Team. When we apply the increment operator it will increase the value of data member players by 1. Now there working is same as it works with built-in types.

Example of Overloading Postfix Increment Operator:-

As I described above, increment operator works in two notation i.e. prefix notation and postfix notation. In order to work with both notations the operator have to be overloaded separately.

#include <iostream>
using namespace std;
class Team
{
private:
int players;
public:
Team()
{
players=0;
}
void show_players()
{
cout<<"Number of players= "<<players<<endl;
}
// for postfix notation
void operator ++(int)
{
players= players+1;
}
};
int main()
{
Team t1;
t1.show_players();
t1++;
t1.show_players();
return 0;
}

Explanation:-

The only difference in this and above program is the addition of this statement:

void operator ++(int)

This statement is used to overload the operator in postfix notation. The int inparenthesis does not taking an integer value but it is an indication to the compiler to overload the operator in postfix notation.

Example of Overloading both increment notations in a same program:-

#include <iostream>
using namespace std;
class Team
{
private:
int players;
public:
Team()
{
players=0;
}
void show_players()
{
cout<<"Number of players= "<<players<<endl;
}
// For prefix notation
void operator ++()
{
players= players+1;
}
// for postfix notation
void operator ++(int)
{
players= players+1;
}
};
int main()
{
Team t1;
t1.show_players();
++t1;
t1.show_players();
t1++;
t1.show_players();
return 0;
}

Explanation:-

In the above program we have overloaded the increment operator for both notations separately to works for a class Team. After using the increment operator in main() function in both notations the value of players is incremented twice.

Binary Operators in C++:-

The operators which needs two operands to perform the functionality are called binary operators. Following binary operators could be overloaded:

+, -, *, /, %, &, |, ^, <<, >>, ==, +=, -=, /=, %=, &=, |=, ^=, <<, >>, <=, >=, &&, ||, [], ().

The simple arithmetic operators such as +,-,*, / are binary operators. They need two operands to perform the functionality. They can be used with user-defined data types by overloading them.

For Example:-

#include <iostream>
using namespace std;
class Addition
{
private:
int w1, w2;
public:
Addition()
{
w1=w2=0;
}
void input()
{
cout<<"enter first value: ";
cin>>w1;
cout<<"enter Second value: ";
cin>>w2;
}
void show()
{
cout<<"First value ="<<w1<<endl;
cout<<"Second value ="<<w2<<endl;
}
Addition operator +(Addition t)
{
t.w1= t.w1 + w1;
t.w2= t.w2 + w2;
return t;
}
};
int main()
{
Addition obj1;
Addition obj2;
Addition obj3;
obj1.input();
obj2.input();
obj3 = obj1 + obj2;
obj1.show();
obj2.show();
cout<<"\nAfter adding the contents of objects the values are: "<<endl;
obj3.show();
return 0;
}

Explanation:-

The above code will overload the binary ‘+’ operator, so that we could use it with the objects of a class Addition. When we add the two objects by this operator then the object left to the binary operator will be act as a calling function and the object at right will be act as a called function. Therefore the right object will be passes as a parameter to the left object, therefore operations will performed and the returned object will be stored in a third object, present at the right side of assignment operator.

Overloading comparison operator in C++:-

The comparison operator (==) is used to compare the value of two objects. It cannot be used with user-defined data types but it could be overloaded in order to use it with them.

For Example:-

#include <iostream>
#include <string>
using namespace std;
class test
{
private:
char st[50];
public:
test()
{
st[0] = '\0';
}
void in()
{
cout<<"Enter String: ";
cin>>st;
}
void show()
{
cout<<st<<endl;
}
int operator == (test s)
{
if(strlen(s.st) == strlen(st) )
return 1;
else
return 0;
}
};
int main()
{
test s1,s2;
s1.in();
s2.in();
cout<<"\ns1 = ";
s1.show();
cout<<"s2 = ";
s2.show();
if(s1 == s2)
cout<<"\nBoth strings are of equal length. ";
else
cout<<"\nBoth strings are of different length. ";
return 0;
}

Explanation:-

[yop_poll id=”19″] The above code will overload the comparison operator to work with the objects of a class Test. We have created two objects of this class and checked whether they are equal or not by comparison operator. If strings are equal it will return true otherwise it will return false.

If you like my article on operator overloading in C++ and wanted to read my other articles then visit my Homepage.

@ 2014 HellGeeks.com

Operator Overloading in C++ with examples
5 (100%) 1 vote

About

(royal52) is a software engineer. He is good at authoritative link building, Content Writing, SEO, Web Development, Wordpress, HTML, CSS, JAVASCRIPT, Game Development, Unity3d, Graphic Designing, Logo Designing, Android Development and Ios Development. Last but not least, he is passionate about development and gaming.

Check Also

File Handling in C++ with Examples

Files are used to save or retrieve data from storage devices, e.g. we can store …

Leave a Reply