Operator Over Loading
Operator Over Loading
Operator overloading is the ability for a language to redefine the way its operators behave
for certain objects. It allows the programmer to extend the language and give it new
abilities.
a1 = a2 + a3;
The above operation is valid, if a1, a2 and a3 are instances of in-built Data Types. But what
if those are, say objects of a Class; is the operation valid?
Yes, it is, if you overload the ‘+’ Operator in the class, to which a1, a2 and a3 belong.
Operator overloading is used to give special meaning to the commonly used operators (such
as +, -, * etc.) with respect to a class. By overloading operators, we can control or define
how an operator should operate on data with respect to a class.
The C++ language has a large number of operators. C++ allows you to overload the
following list:
Since the number of C++ operators is very large, it is easier to memorize the operators that
cannot be overloaded and assume that anything else can be overloaded.
1
Overloading Rules
• You cannot invent new operators that do not exist in the language. For instance, @ is
not a valid C++ operator and cannot be overloaded.
• Overloaded operators must follow the same syntax rules. For example:
• SomeClass x, y;
• x ++ 12; // Not allowed: ++ is not a binary operator.
• x++; // This is allowed
• % x; // Not allowed: % is not an unary operator.
• y = x % 3; // This is allowed
• You cannot change the way an operator works between native C++ types. For
instance, you cannot change how + works between a double and an integer variable,
as this operation is native to the compiler. You can however change how + works
between a Complex class and a double variable, since Complex isn't a native C++
type.
• The order of operator evaluation remains unchanged. You cannot change the operator
precedence rules.
• You cannot change the operator associativity rules either. For example, the unary
minus (-) operator associates from right to left.
• X = -Y; // Unary minus associates from right to left.
• X = Y-; // You CANNOT alter unary minus to go from left to right.
• Overloading some operators doesn't implictly define other operators for you. For
example, overloading the + and = operators for a class doesn't mean that the +=
operator is overloaded automatically for you. You will need to write separate overload
code for the += operator as well.
Unwritten Rule: Try to overload operators where they make sense. For example, virtually
everyone is used to seeing arithmetic symbols for matrix operations such as addition,
subtraction, multiplication etc., because this is how we were taught to represent them at
school (surely you weren't sleeping there, were you?). Therefore, it makes sense to overload
the arithmetic operators (+, -, *) for matrix objects. You should note that just because you
CAN overload the + sign to mean subtraction between two matrices, doesn't mean you
SHOULD actually do it. You should keep the overloaded operators analogous to their original
meaning. Call this the "behaves like an int" rule -- i.e. if an operator behaves a certain way
for an int, you should overload it to an analogous meaning for your class.
2
1. Creating operator functions using a Member Function of a class:
ret-type operator#(arg-list);
Here: ret-type is commonly the name of the class itself as the operations would
commonly return data (object) of that class type.
#include <iostream.h>
#include<conio.h>
class myclass{
public:
};
myclass temp;
temp.sub1=sub1 + ob.sub1; // Add the data of the object that generated the call with
temp.sub2=sub2 + ob.sub2; // the data of the object passed to it and store in temp
3
}
void main()
clrscr();
myclass ob1(10,20);
myclass ob2(30,40);
myclass ob3;
ob1.show();
ob2.show();
ob3.show();
getch();
Please notice that the operator function myclass operator +(myclass); is taking only one
argument when it’s operating on two objects (i.e. it’s a binary operator).
Now assume that ‘operator+’ function is just a regular function which is called as below
when the respective operator (‘+’ in this case) is encountered with respect to the objects of
its class.
ob3 = ob1.operator+(ob2);
Something like this happens internally when an overloaded operator is encountered. As you
can understand from this, when an operator is encountered, the objects to the left (here
ob1) generates a call to the operator function to which ob2 is passed, the function does the
operation as defined and returns the result as an object which is then assigned to ob3.
Therefore when a binary operator is overloaded using a Member Function of a class, it takes
only one argument.
4
// Eg program 2: to illustrate operator overloading : Binary Operator +,-,=
#include <iostream.h>
#include<conio.h>
class ThreeD
{
int x, y, z;
public:
ThreeD() { x = y = z = 0; }
ThreeD(int i, int j, int k) { x = i; y = j; z = k; }
temp.x = x - op2.x;
temp.y = y - op2.y;
temp.z = z - op2.z;
return temp;
}
ThreeD ThreeD::operator+(ThreeD op2) // Overload +
{
ThreeD temp;
temp.x = x + op2.x;
temp.y = y + op2.y;
temp.z = z + op2.z;
return temp;
}
5
ThreeD ThreeD::operator=(ThreeD op2) // Overload assignment
{
x = op2.x;
y = op2.y;
z = op2.z;
return *this; // i.e., return object that generated call
}
void ThreeD::show() // Show X, Y, Z coordinates.
{
cout << x << ", ";
cout << y << ", ";
cout << z << "\n";
}
void main()
{
clrscr();
ThreeD a(1, 2, 3), b(10, 10, 10), c;
c = a - b;
cout << "a - b: ";
c.show();
c = a + b;
cout << "a + b: ";
c.show();
c=a;
cout << "a = c: ";
c.show();
getch();
}
6
// Eg program3: to illustrate operator overloading : Binary Operator +,-,=,++
#include <iostream.h>
#include<conio.h>
class Point
{
int x, y;
public:
Point() {} // needed to construct temporaries
Point(int px, int py) { x = px; y = py; }
temp.x = op2.x + x;
temp.y = op2.y + y;
return temp;
}
7
Point Point::operator=(Point op2) // Overload asignment for Point.
{
x = op2.x;
y = op2.y;
return *this; // i.e., return object that generated call
}
void main()
{
clrscr();
ob1.show();
ob2.show();
++ob1;
ob1.show(); // displays 11 21
ob2 = ++ob1;
ob1.show(); // displays 12 22
ob2.show(); // displays 12 22
getch();
}
8
2. Creating operator overloading functions using a Friend Function of a class:
#include <iostream.h>
#include<conio.h>
class Point {
int x, y;
public:
Point() {} // needed to construct temporaries
Point(int px, int py) { x = px; y = py; }
Point operator+(Point op1, Point op2) // Now, + is overloaded using friend function.
{
Point temp;
return temp;
}
9
Point Point::operator=(Point op2) // Overload assignment for Point.
{
x = op2.x;
y = op2.y;
return *this; // i.e., return object that generated call
}
void main()
{
clrscr();
getch();
}
#include<conio.h>
class Num
int count;
10
public:
};
Num& Num::operator=(int i)
count=i;
return *this;
Num temp;
temp.count=ob.count+i;
return temp;
Num temp;
temp.count=ob.count+i;
return temp;
void main()
clrscr();
11
Num obj;
obj=10;
obj.Show();
obj=10+obj;
obj.Show();
obj=obj+12;
obj.Show();
getch();
#include <iostream.h>
#include<conio.h>
class Point
{
int x, y;
public:
Point() {}
Point(int px, int py) { x = px; y = py; }
void show() { cout <<"\n x = "<< x << "\n y = "<< y<<"\n"; }
Point operator=(Point op2);
friend Point operator++(Point &op);
friend Point operator--(Point &op);
};
12
Point operator++(Point &op) // Now a friend; use a reference parameter.
{
op.x++;
op.y++;
return op;
}
Point operator--(Point &op) // Make op-- a friend; use reference.
{
op.x--;
op.y--;
return op;
}
void main()
{
clrscr();
ob1.show();
++ob1;
ob1.show(); // displays 11 21
ob2 = ++ob1;
ob2.show(); // displays 12 22
--ob2;
ob2.show(); // displays 11 21
getch();
}
13