Unit3 - Operator Overloading and Type Conversion
Unit3 - Operator Overloading and Type Conversion
C++ tries to use the user defined data types much in the way, the built in data types are
used. C++ has the ability to add special features to the functionality and behaviour of
already existing operators like arithmetic operators. The mechanism of giving special
meaning to an operator is known as operator overloading. In C++ one can overload any valid
operator of C++ except the following:
Ø Class member access operator ( . ) (dot)
Ø Class member access operator when pointer is used ( .* )
Ø Scope resolution operator ( :: )
Ø sizeof operator
Ø Conditional operator ( ? : )
Operator overloading is used to make most of the standard operators (available for built in
data types) to be used with user defined data type items.
v Operator overloading is applicable only to the existing operators, no new operator
can be created using operator overloading.
v The overloaded operator must use at least one user defined data type.
v Through operator overloading semantics of an operator can be enhanced but the
syntax, the general grammar like number of operands, precedence and associativity
remains the same.
v Hence when an operator is overloaded, its original meaning is never lost. for
example if + is used to find sum of integers or floats, then it can only be used to find
sum but after overloading + can be redefined to find sum of objects or to
concatenate strings but not to subtract numbers.
v However either a friend or a member function can be used to overload the
operators, some of the operators like = , ( ) , [ ] and -> cannot be overloaded by
using fried function.
v Binary operators + , - , * , / must return a relevant value.
int main( )
{
fruit basket1(5,9) , basket2(15, 5);
cout<<”\n ...... initially no of fruits in
baskets.........”;
basket1.display( );
basket2.display( );
++basket1;
++basket2;
cout<<”\n after using overloaded increment ++ operator
with objects ”;
basket1.display( );
basket2.display( );
return 0;
}
Output:
...... initially no of fruits in baskets.........
number of bananas : 5
number of mangoes : 9
number of bananas : 15
number of mangoes : 5
after using overloaded increment ++ operator with objects
number of bananas : 15
number of mangoes : 19
number of bananas : 25
number of mangoes : 15
void display( )
{ cout<<”\n complex number = “<< real<<” +
“<<img” i”; }
int main ( )
{
complex num1(5, 8), num2;
cout<<”\n original complex number......................”;
num1.display( );
num2 = - num1; // calling operator function
cout<<”\n negative complex umber.......................”;
num2.display( );
return 0;
}
Output:
original complex number......................
complex number = 5 + 8 i
negative complex number.......................
complex number = - 5 + -8 i
time time::operator+(time t)
{
time temp;
int m = mins + t.mins;
temp.mins = m % 60;
temp.hours = hours + t.hours + m / 60;
return temp;
}
void main()
{
clrscr();
time t1(5,45), t2(2,30), t3;
t3 = t1 + t2; //operator function called – see note
t1.show();
t2.show();
cout<<"\n Sum of times ";
t3.show();
return 0;
}
Output:
time = 5 : 45
time = 2 : 30
Sum of times
time = 8 : 15
Explanation:
In the statement t3 = t1 + t2;
v t3 stores the value of object returned by operator overloading function.
v Because operator +( ) function is a member function, it can only be invoked by using
an object of the same class. t1 is the object which has the responsibility of invoking
the operator function. Hence data items of t1 are accessed directly in operator
function.
v The operator+( ) function accepts one argument of type class time. t2 is accepted by
the function as actual parameter.
long-double
double
long-double
The expression will give result in long double type after implicit conversions.
int main ( )
{
int wgm;
cout<<”\n enter weight in grams : “;
cin>> wgm;
weight mangoes ;
mangoes = wgm; // wgm gets converted into kg
and gm
magoes.display ( );
return 0;
}
Output:
enter weight in grams : 4520
kg = 4 gm = 520
void display ( )
{ cout<<”\n weight in kg & gm : “<< kg<<” kg
“<<gm << “ gm “; }
}; // end of class
int main ( )
{
int wfruits , wvegies ;
weight fruits(3 , 500 );
weight vegies( 2 , 250 );
fruits.display ( );
vegies.display ( );
wfruits = fruits ; // invokes operator function
and return weight in grams
wvegies = vegies; // invokes operator function ,
behaves as vegies.opertor int ()
int total_weight = wfruits + wvegies;
cout<<”\n total weight of fruits and vegetables : “<<
total_weight;
return 0;
}
Output:
weight in kg & gm : 3 kg 500 gm
weight in kg & gm : 2 kg 250 gm
total weight of fruits and vegetables : 5750
class two
{
int code;
float value;
public:
two()
{
code = 0;
value = 0;
}
two(int x,float y)
{
code = x;
value = y;
}
void showdata()
{
cout<<"\n CODE= "<<code;
cout<<"\n VALUE= "<<value;
}
two(one obj1)
{
Code = obj1.getcode();
Value = obj1.getitems()*obj1.getprice();
}
}; // end of class two
main()
{
one ob1(1000,20,150);
float tot_value;
two ob2;
tot_value=ob1; // invokes operator function of class
one
ob2 = one( ob1); // conversion from class two to class
one
cout<<"\n item from class one ";
ob1.showdata();
cout<<"\n VALUE= "<<tot_value;
cout<<"item from class two ";
ob2.putdata();
}