Type Conversions
Different situations of data conversion between incompatible types
• 4 types of situation might arise in the data conversion:
– conversion from basic type to basic type
– Conversion from basic type to class type
– Conversion from class type to basic type
– Conversion from class type to class type
Type Conversion 1
basic type to class type
Constructors perform this type of conversions
int main()
{
int duration=85;
time t=duration;
t.display();
return 0;
}
Output:
1 hours 25 minutes
Type Conversion
class time
{
int hrs;
int mins;
public:
time()
{ }
time(int t)
{ hrs=t/60;
mins=t%60;
}
void display()
{cout<<hrs<<" hours";
cout<<mins<<" minutes";
}
};
2
class type to basic type
• constructor does not support this type of conversion
• So c++ allow us to define an overloaded casting operator that could be
used to convert a class type data to basic type.
• It is also known as conversion function
• Overloaded casting operator actually overloads the built in casting
operator.
• The casting operator function should satisfy following conditions
– It must be a class member
– It must not specify return type
– It must not have any arguments
3
Type Conversion
• Here typename is any basic data type.
• This overloaded typecast operator function does not have any return
type(not even void)
• Because the return type is the typename to which, the object is being
converted.
• Moreover it does not take any parameter
4
Type Conversion
• Syntax:-
operator typename()
{
…………
}
Example:
operator double()
{
……..
}
operator int()
{
………..
}
Cont…
class type to basic type
class time
{ int hrs;
int mins;
public:
time(int h,int m)
{hrs=h;
mins=m;
}
void display()
{ cout<<hrs<<" hours";
cout<<mins<<" minutes";
}
operator int()
{
return hrs*60+mins;
}
};
int main()
{
int duration;
time t(3,20);
t.display();
duration=t;
cout<<"duration:"<<duration<<" minutes";
return 0;
}
Output:
3 hours20 minutes
duration:200 minutes
5
Type Conversion
class type to class type
1. Using conversion function in source class
2. Using constructor in destination class
A objA;
B objB;
objA=objB;
6
Type Conversion
Destination
class Source class
class dollar
{
public:
int doll;
dollar(int x)
{
doll=x;
}
void show()
{
cout<<"Money dollars="<<doll<<endl;
}
};
class rupee
{ double rs;
public:
rupee(dollar d)
{
rs=d.doll*71.59;
}
void show()
{
cout<<"money in rupees:"<<rs;
}
};
int main()
{
dollar d1(5);
d1.show();
rupee r1=d1;
r1.show();
return 0;
}
Output:
Money dollars=5
money in rupees:357.95
7
Type Conversion
constructor in destination class
class rupee
{
public:
double rs;
rupee()
{ }
rupee(double rs)
{
this->rs=rs;
}
void show()
{
cout<<"money in rupees="<<rs;
}
};
class dollar
{
public:
int doll;
dollar(int x)
{
doll=x;
}
operator rupee()
{
return rupee(doll*71.59);
}
void show()
{
cout<<"Money in dollars="<<doll<<endl;
}
};
int main()
{
dollar d1(5);
d1.show();
rupee r1=d1;
r1.show();
return 0;
}
Output:
Money in dollars=5
money in rupees=357.95
8
Type Conversion
Using conversion function in source class

13.Data Conversion.pptx

  • 1.
    Type Conversions Different situationsof data conversion between incompatible types • 4 types of situation might arise in the data conversion: – conversion from basic type to basic type – Conversion from basic type to class type – Conversion from class type to basic type – Conversion from class type to class type Type Conversion 1
  • 2.
    basic type toclass type Constructors perform this type of conversions int main() { int duration=85; time t=duration; t.display(); return 0; } Output: 1 hours 25 minutes Type Conversion class time { int hrs; int mins; public: time() { } time(int t) { hrs=t/60; mins=t%60; } void display() {cout<<hrs<<" hours"; cout<<mins<<" minutes"; } }; 2
  • 3.
    class type tobasic type • constructor does not support this type of conversion • So c++ allow us to define an overloaded casting operator that could be used to convert a class type data to basic type. • It is also known as conversion function • Overloaded casting operator actually overloads the built in casting operator. • The casting operator function should satisfy following conditions – It must be a class member – It must not specify return type – It must not have any arguments 3 Type Conversion
  • 4.
    • Here typenameis any basic data type. • This overloaded typecast operator function does not have any return type(not even void) • Because the return type is the typename to which, the object is being converted. • Moreover it does not take any parameter 4 Type Conversion • Syntax:- operator typename() { ………… } Example: operator double() { …….. } operator int() { ……….. } Cont…
  • 5.
    class type tobasic type class time { int hrs; int mins; public: time(int h,int m) {hrs=h; mins=m; } void display() { cout<<hrs<<" hours"; cout<<mins<<" minutes"; } operator int() { return hrs*60+mins; } }; int main() { int duration; time t(3,20); t.display(); duration=t; cout<<"duration:"<<duration<<" minutes"; return 0; } Output: 3 hours20 minutes duration:200 minutes 5 Type Conversion
  • 6.
    class type toclass type 1. Using conversion function in source class 2. Using constructor in destination class A objA; B objB; objA=objB; 6 Type Conversion Destination class Source class
  • 7.
    class dollar { public: int doll; dollar(intx) { doll=x; } void show() { cout<<"Money dollars="<<doll<<endl; } }; class rupee { double rs; public: rupee(dollar d) { rs=d.doll*71.59; } void show() { cout<<"money in rupees:"<<rs; } }; int main() { dollar d1(5); d1.show(); rupee r1=d1; r1.show(); return 0; } Output: Money dollars=5 money in rupees:357.95 7 Type Conversion constructor in destination class
  • 8.
    class rupee { public: double rs; rupee() {} rupee(double rs) { this->rs=rs; } void show() { cout<<"money in rupees="<<rs; } }; class dollar { public: int doll; dollar(int x) { doll=x; } operator rupee() { return rupee(doll*71.59); } void show() { cout<<"Money in dollars="<<doll<<endl; } }; int main() { dollar d1(5); d1.show(); rupee r1=d1; r1.show(); return 0; } Output: Money in dollars=5 money in rupees=357.95 8 Type Conversion Using conversion function in source class