C++ : Type conversions in Operator Overloading/ Polymorphism

Type Conversions:
-------------------------
In an expression when operands of different type are present in an expression before manipulation operands get converted automatically to certain types as per language rules. Similarly = (assignment operator) also performs automatic conversion where right side value type converted to left side type. Check the following example,

float a=7.35;
int b;
b=a;//Automatic conversion from float to int

i.e. b is now 7

What happens when operands are of type user defined ?

Since user defined data types are designed by us to suit our requirements hence compiler does not support automatic type conversion . Therefore we must design our own conversion routines/functions for handling such conversions. 

Three types of situations might arise in the data conversion between incompatible data types namely,

1. Conversion from basic type to class type
2. Conversion from class type to basic type
3. Conversion from one class type to another class type

1. Conversion from basic type to class type
-------------------------------------------------------------
In the following example int value will be converted to Time class object. To do this we are using constructor that takes one int argument  to do above conversion.

Time- hr
min

int x=85;

Time t1;

class Time
{
 private: int hr,min;
 public: Time()
{
hr=min=0;
}
  Time(int h, int m)
{
hr=h;
min=m;
}
void display()
{
cout<<"\nHour="<<hr<<"\nMinute="<<min;
}
Time(int y)
{
hr=y/60;
min=y%60;
}
};
main()
{
 int x=85;
 Time t1;
  t1=x;
  t1.display();
}


2. Conversion from class type to basic type
----------------------------------------------------------
This is doing reverse operation of above one. But in this we are using a built function called operator op() .

class Time
{
 private: int hr,min;
 public: Time()
{
hr=min=0;
}
  Time(int h, int m)
{
hr=h;
min=m;
}
void display()
{
cout<<"\nHour="<<hr<<"\nMinute="<<min;
}
operator int()
{
int x;
x=hr*60+min;
return x;
}
};
main()
{
 int x;
 Time t1=Time(1,25);
  x=t1;
  t1.display();
 cout<<"\nx="<<x;
}

3. Conversion from one class type to another class type
-----------------------------------------------------------------------------

Inventory1 Inventory2
-------------- --------------
code code
price amount
qty

#include<iostream>
class Inventory1
{
 private: int code, price, qty;

 public: Inventory1()
{
code=price=qty=0;
}
Inventory1(int c,int p, int q)
{
code=c;
price=p;
qty=q;
}
void display()
{
cout<<"\nCode="<<code
<<"\nPrice="<<price
<<"\nQty="<<qty;
}
int getCode()
{
return code;
}
int getPrice()
{
  return price;
}
int getQty()
{
return qty;
}
};

class Inventory2
{
 private: int code, amount;

 public: Inventory2()
{
code=amount=0;
}
Inventory2(int c,int a)
{
code=c;
amount=a;
}
void display()
{
cout<<"\nCode="<<code
<<"\namount="<<amount;
}
Inventory2(Inventory1 &t)
{
code=t.getCode();
amount=t.getPrice() * t.getQty();
}
};

main()
{

Inventory1 t1=Inventory1(101,50,20);
Inventory2 t2=Inventory2(t1);
t1.display();
t2.display();
}

















Comments