Operator overloading can transform complex, obscure program listings into intuitively obvious ones. For examples, statements like :
ob3.addobjects ( ob1, ob2) or ob3 = ob1.addobjects( ob2) can be changed to the much more readable like ob3 = ob1 + ob2.
Note : The process of selecting the most appropriate overloaded function or operator is called overload resolution.
2. Contents
● 7.1 Defining operator Overloading
● 7.2 Overloading Unary Operator
● 7.3 Overloading Binary Operator
● 7.4 Overloading Operator Using Friend function
● 7.6 Type Conversion
3. Introduction
● C++ has ability to provide the operators with a special meanings for a data type.
● The mechanism of giving such special meaning to an operator is known as operator overloading.
Why Operator Overloading?
● Readable code
● Make operators sensitive to context
4. General Format for operator overloading
returnType operator operator_symbol(parameters);
➔ Return type may be whatever the operator returns
➔ Including a reference to the object of the operand
➔ Operator symbol may be any overloadable operator from the list.
5. Restrictions on Operator Overloading
● C++ operators that can be overloaded.
● C++ Operators that cannot be overloaded Operators that cannot be overloaded .
6. Overloading Unary Operator
#include <iostream.h>
class temp
{ Output Count: 6
private:
int count;
Public:
temp():count(5) { }
void operator ++()
{ count=count+1;}
void Display()
{ cout<<"Count: "<<count; }
};
int main()
{
temp t;
++t; /* operator function void operator ++() is called */
t.Display();
return 0;
}
7. Note
Operator overloading cannot be used to change the way operator works on built-in types. Operator
overloading only allows to redefine the meaning of operator for user-defined types.
Only existing operators can be overloaded. New operators cannot be created.
The overloaded operator must have at least one operand that is of user-defined type.
We cannot change the basic meaning of an operator.
Overloaded operators follow the syntax rules of the original operators.
10. Overloading operators using Friend
● Friend function using operator overloading offers better flexibility to the class.
● These functions are not a members of the class and they do not have 'this' pointer.
● When you overload a unary operator you have to pass one argument.
● When you overload a binary operator you have to pass two arguments.
● Friend function can access private members of a class directly.
● friend return-type operator operator-symbol (Variable 1, Varibale2)
{
//Statements;
}
11. #include<iostream>
class UnaryFriend
{
int a=10;
int b=20;
int c=30;
public:
void getvalues()
{cout<<"Values of A, B & Cn";
cout<<a<<"n"<<b<<"n"<<c<<"n
"<<endl;
}
void show()
{cout<<a<<"n"<<b<<"n"<<c<<"
n"<<endl;
}
void friend operator-(UnaryFriend &x);
};
void operator-(UnaryFriend &x)
{
x.a = -x.a;
x.b = -x.b;
x.c = -x.c;
}
int main()
{
UnaryFriend x1;
x1.getvalues();
cout<<"Before Overloadingn";
x1.show();
cout<<"After Overloading n";
-x1;
x1.show();
return 0;
}
12. class time
{
Private:
int hours,minutes;
Public:
time( )
{
hours=0;
minutes=0;
}
time(int x,int y)
{
hours=x;
minutes=y;
}
void display( )
{
cout<<endl<<hours<<" hours and
"<<minutes<<" minutes.";
}
friend time operator + (time, time);
};
time operator + (time y, time z)
{
int h = y.hours + z.hours;
int m = y.minutes + z.minutes;
while (m>=60)
{
m = m-60;
h = h+1;
}
return time(h,m);
}
int main( )
{
time t1(2,40);
time t2(3,30);
time t3;
t3 = t1+t2;
t3.display( );
return 0;
}
13. class time
{
Private:
int hours,minutes;
Public:
time( )
{
hours=0;
minutes=0;
}
time(int x,int y)
{
hours=x;
minutes=y;
}
void display( )
{
cout<<endl<<hours<<" hours and
"<<minutes<<" minutes.";
}
friend time operator + (time, time);
};
time operator + (time y, time z)
{
int h = y.hours + z.hours;
int m = y.minutes + z.minutes;
while (m>=60)
{
m = m-60;
h = h+1;
}
return time(h,m);
}
int main( )
{
time t1(2,40);
time t2(3,30);
time t3;
t3 = t1+t2;
t3.display( );
return 0;
}
14. Type Conversions
● the type conversions are automatic only when the data types involved are built-in types.
● int m; float x = 3.14159; m = x;// convert x to integer before its value is assigned // to m.
● For user defined data types, the compiler does not support automatic type conversions.
● Different situations of data conversion between incompatible types.
● Conversion from basic type to class type.
● Conversion from class type to basic type.
● Conversion from one class type to another class type.
15. Basic to Class Type
A constructor to build a string type object from a char * type variable.
string : : string(char *a)
{
length = strlen(a);
P = new char[length+1];
strcpy(P,a);
}
The variables length and p are data members of the class string.
16. Class To Basic Type
● A constructor function do not support type conversion from a class type to a basic type.
● An overloaded casting operator is used to convert a class type data to a basic type. It is also
referred to as conversion function.
● operator typename( )
{ … ( function statements ) … }
● This function converts a class type data to typename.
vector : : operator double( )
{
double sum = 0;
for (int i=0; i < size ; i++)
sum = sum + v[i] * v[i];
return sqrt (sum);
}
● This function converts a vector to the square root of the sum of squares of its components.
17. Continue...
The casting operator function should satisfy the following conditions:
● It must be a class member.
● It must not specify a return type.
● It must not have any arguments.
18. One Class To Another Class Type
● Type objX = objY ; // objects of different types objX is an object of class X and objY is an object of
class Y.
● The class Y type data is converted to the class X type data and the converted value is assigned to
the objX.
● Conversion is takes place from class Y to class X.
● Y is known as source class.
● X is known as destination class.