Type Conversion:
cast operator- Type Casting
float f;
(int)f; / int(f);
All the operands areof priliminary type/basic type
User defined type---------
There are 3 types of situations:
1. Basic type to class type
x is an object and y is a variable
x=y, this assignment is not possible unless you convert the basic type to class
type. It's not possible by type conversion. But we can achieve it by including a
parameterized constructor in the class.
Example- We want to convert an int type to a class type.
class time
{
int hrs;
int mins;
public:
void display()
{
cout<<"The duration is"<<hrs<<" hrs and "<<mins<<"mins.";
}
time(int t)
{
hrs=t/60; //t in hrs
mins=t%60; //t in mins
}
};
main()
{
time T1;
int duration=85;
T1=duration; //Compiler is going search for a parameterized constructor
suppose to take one integer as argument and invoke that constructor. Compiler
interpretation is T1.time(duration)
T1.display();
}
Output:
The duration is 1 hrs and 15 mins.
2. class type to basic type
x is a variable and y is an object
x=y, this assignment is not possible unless you convert the class type to basic
type. It's not possible by type conversion. But we can achieve it by including an
operator function to overload type cast operator in the class.
int(Operand)- int(), double(), char()
operator typename()
{
......
......Function statement
}
Example:
class vector
{
int v[10];
public:
........
........
void get();
double operator double();
};
double vector :: operator double()
{
double sum=0;
for(int i=0;i<10;i++) sum=sum+v[i]*v[i];
return sqrt(sum);
}
main()
{
vector V1;
V1.get();
double length=V1;// error
double length=double(V1);
cout<<"The scalar magnitude of the vector the vector componemts is"<<length;
}
3. class type to class type.
x=y (x is the class type) parameterized constructor on the class type of x
x=y (y is the class type) Operator function on the class type of y
x=y (both x and y are of class type) (A parameterized constructor on the class type
of x and operator function on the class type of y.)
Example:
class invent2; //Forward declaration of a class
class invent1 //source class
{
int code;
int items;
float price;
public:
invent1(int a,int b, float c)
{
code=a;
items=b;
price=c;
}
void putdata()
{
cout<<"Code:"<<code<<"\n";
cout<<"Items:"<<items<<"\n";
cout<<"Price:"<<price<<"\n";
}
int getcode() { return code; }
int getitems() { return items; }
float getprice() { return price; }
operator float() { return (items*price); }
operator invent2() //operator function for conversion as a source class here
{
invent2 temp;
temp.code=code;
temp.value=price*items;
return temp;
}
};
class invent2 //destination class
{
int code;
float value;
public:
invent2()
{
code=0; value=0;
}
invent2(int x, float y)
{
code=x;
value=y;
}
void putdata()
{
cout<<"Code:"<<code<<"\n";
cout<<"Value:"<<value<<"\n\n";
}
invent2(invent1 p) //constructor for conversion as a destination class here
{
code=p.getcode();
value=p.getitems()*p.getprice();
}
};
int main()
{
invent1 s1(100,5,140.0);
invent2 d1;
float total_value;
total_value=s1;
d1=s1; //d1(s1)
cout<<"Product details - Inventory 1"<<"\n";
s1.putdata();
cout<<"\nStock value"<<"\n";
cout<<"Value="<<total_value<<"\n\n";
cout<<"Product details- Inventory 2"<<"\n";
d1.putdata();
return 0;
}
Forward Declaration of a class: Declaration of a class which is going to be used in
other classes before its definition.