OPERATOR OVERLOADING
- It is a compile time polymorphism which is used to overload or redefine the operators available in c++
- A special operator symbol which is used to perform specific operations
- Unlike java, c++ supports operator overloading
- It provides the special meaning for the user defined data types such as objects, structuresIn c++, the operator overloading can be done with the help of operator symbols such as -, +, >, <, ==, !=, <<, >>, etc,…
- It is optional.
- We can redefine the c++ operators through operator overloading
Example
TYPES OF OPERATORS
1. Unary Operator
- It needs single operand (single variable) to perform the operations
Example
2. Binary Operators
- It needs two operands (variables) to perform the operations
Example
RULES FOR OPERATOR OVERLOADING
- The overloading function must include the keyword operator and its symbol
- The operator function will be called using an object with an overloaded operator symbol.
- When overloading unary operator using member function won’t take any arguments but if they are overloaded through friend function will take one argument
- When overloading a binary operator using a member function will take one argument but if they are overloaded through a friend function it will take two arguments.
- The operator overloading can be implemented using three different approaches. They are
- Member Function (Inside the class)
- Non Member Function (Outside the class)
- Friend Function (Outside the class)
OPERATORS THAT CAN’T BE OVERLOADED IN C++
- C++ does not support the following operators for overloading. They are
- :: Scope Resolution Operator
- ?: ternary Operator
- . Member Selector Operator
- sizeof Operator
- * Member Pointer Selector.
I. EXAMPLE OF UNARY OPERATOR OVERLOADING
1. SOURCE CODE
#include <iostream>
using namespace std;
class UTest
{
public:
int i=14;
// unary operator –
void operator -()
{
i=-i;
}
// display the data
void disp()
{
cout<<“\tValue of i:\t”<<i<<“\n”;
}
};
int main()
{
cout<<“—————————————————\n”;
cout<<“\t C++ Unary Operator Overloading\n”;
cout<<“—————————————————\n”;
// object creation for current class (local object)
UTest u;
cout<<“Value Before Calling Unary Operator Overloading\n”;
u.disp();
cout<<“—————————————————\n”;
// calling unary operator overloading via operator symbol
-u; // u.operator();
cout<<“Value After Calling Unary Operator Overloading\n”;
u.disp();
return 5;
}
2. OUTPUT
II. EXAMPLE OF UNARY OPERATOR OVERLOADING
1. SOURCE CODE
#include <iostream>
using namespace std;
class UTest
{
public:
int i=14;
// unary operator ++
void operator ++()
{
++i;
}
// display the data
void disp()
{
cout<<“\tValue of i:\t”<<i<<“\n”;
}
};
int main()
{
cout<<“————————————————-\n”;
cout<<“\t C++ Unary Pre ++ Operator Overloading\n”;
cout<<“————————————————-\n”;
// object creation for current class (local object)
UTest u;
cout<<“Value Before Calling Unary Operator Overloading\n”;
u.disp();
cout<<“————————————————-\n”;
// calling unary operator overloading via operator symbol
++u; // u.operator();
cout<<“Value After Calling Unary Operator Overloading\n”;
u.disp();
return 5;
}
NOTE
- It is important to note that the operator ++ can be used for both prefix and postfix operators.
- There is no separate operator used for postfix operators like ++, —
2. OUTPUT
III. EXAMPLE OF BINARY OPERATOR OVERLOADING
1. SOURCE CODE
#include<iostream>>
using namespace std;
class BTest
{
public:
int w,x;
// initialize the instance variable using constructor
BTest(int r)
{
w=r;
}
// binary operator +
void operator +(BTest tmp)
{
x=w+tmp.w;
cout<<“\tSum: “<<x<<“\n”;
}
};
int main()
{
cout<<“—————————————————–\n”;
cout<<“\t C++ Binary Operator Overloading\n”;
cout<<“—————————————————–\n”;
// local objects creation and calling parameterized constructor
BTest b1(27),b2(45);
cout<<“Inputs:\n”;
cout<<“\t Number 1 for Object 1: “<<b1.w<<“\n”;
cout<<“\t Number 2 for Object 2: “<<b2.w<<“\n”;
cout<<“Outputs:\n”;
// calling binary operator overloading
b1+b2; // b1.operator();
return 5;
}
2. OUTPUT
IV. EXAMPLE OF RELATIONAL OPERATOR OVERLOADING
1. SOURCE CODE
#include<iostream>>
using namespace std;
class GTest
{
public:
int i;
// initialize the variable through constructor
GTest(int y)
{
i=y;
}
bool operator >(GTest tmp)
{
bool flag=false;
if(i>tmp.i)
flag=true;
return flag;
}
};
int main()
{
cout<<“—————————————————–\n”;
cout<<“\t C++ Relational Operator Overloading > \n”;
cout<<“—————————————————–\n”;
// local objects creation
GTest r1(95),r2(23);
cout<<“Inputs:\n”;
cout<<“\t Number 1 for Object 1: “<<r1.i<<“\n”;
cout<<“\t Number 2 for Object 2: “<<r2.i<<“\n”;
cout<<“Output:\n”;
// calling relational operator >
if(r1>r2) // r1.operator(r2)
{
cout<<“\tObject r1 is greater than r2…\n”;
}
else
{
cout<<“\tObject r2 is greater than r1…\n”;
}
return 5;
}
2. OUTPUT
2.1 SUCCESS CASE
2.2 FAILURE CASE (If the inputs are 14, 55)
V. EXAMPLE OF RELATIONAL OPERATOR OVERLOADING
1. SOURCE CODE
#include<iostream>>
using namespace std;
class LTest
{
public:
int i;
// initialize the variable through constructor
LTest(int y)
{
i=y;
}
// relational operator <
bool operator <(LTest tmp)
{
bool flag=false;
if(i<tmp.i)
flag=true;
return flag;
}
};
int main()
{
cout<<“———————————————–\n”;
cout<<“\t C++ Relational Operator Overloading < \n”;
cout<<“———————————————–\n”;
// local objects creation
LTest r1(15),r2(72);
cout<<“Inputs:\n”;
cout<<“\t Number 1 for Object 1: “<<r1.i<<“\n”;
cout<<“\t Number 2 for Object 2: “<<r2.i<<“\n”;
cout<<“Output:\n”;
// calling relational operator <
if(r1<r2) // r1.operator(r2)
cout<<“\tObject r1 is less than r2…\n”;
{
}
else
{
cout<<“\tObject r2 is less than r1…\n”;
}
cout<<“———————————————–\n”;
return 5;
}
2. OUTPUT
2.1 SUCCESS CASE
2.2 FAILURE CASE (If inputs are 84, 21)
VI. EXAMPLE OF RELATIONAL OPERATOR OVERLOADING
1. SOURCE CODE
#include<iostream>>
using namespace std;
class ETest
{
public:
int i;
// initialize the variable through constructor
ETest(int y)
{
i=y;
}
// relational operator ==
bool operator ==(ETest tmp)
{
bool flag=false;
if(i==tmp.i)
flag=true;
return flag;
}
};
int main()
{
cout<<“———————————————–\n”;
cout<<“\t C++ Relational Operator Overloading == \n”;
cout<<“———————————————–\n”;
// local objects creation
ETest r1(51), r2(51);
cout<<“Inputs:\n”;
cout<<“\t Number 1 for Object 1: “<<r1.i<<“\n”;
cout<<“\t Number 2 for Object 2: “<<r2.i<<“\n”;
cout<<“Output:\n”;
// calling relational operator ==
if(r1==r2)
{
cout<<“\tObject r1 is equal to r2…\n”;
}
else
{
cout<<“\tObject r1 is not equal to r2…\n”;
}
return 5;
}
2. OUTPUT
2.1 SUCCESS CASE
2.2 FAILURE CASE (If Inputs are 14, 191)