Type conversion in c++ | basic to class | class to basic | class to class
Tarun Sir Tarun Sir
55.5K subscribers
64,854 views
1K

 Published On Mar 17, 2021

Type conversion in c++
• When different types are mixed in an expression, then automatic type conversion is applied on operands according to certain rules. Similarly in assignment operation, the type of data on the right hand of assignment is automatically converted to the type of the variable on the left. For example:
int y;
float n=2.345;
y=n;
• In the above code snippet the value of n is converted into integer when it is stored into y. Thus the fractional part is truncated and only 2 is stored into the y.
• The automatic type conversion is performed only when only primitive types are involved in an expression. When user defined types are involved in an expression then automatic type conversion cannot be performed.
• We have to define operator functions (casting operator functions) for performing conversions with user defined types. We can have following three types of conversions where user defined types are involved:
i) Basic to class type : When we create objects using the variables of primary data types then it is called as basic to class type conversion. Generally we use single argument constructor to perform type conversion from basic type to class type.
#include<iostream>
using namespace std;
class Number
{ int n;
public:
void printDetails(){ cout<<n; }
Number(int n){ this->n=n;}
};
int main()
{ Number numb=100;
numb.printDetails();
return 0;
}
ii)Class type to basic type: When we assign an object to a primitive data type’s variable, then it is called as class type to basic conversion. To perform class type to basic type conversion we have to define the casting operator function. The casting operator function must be a member of the class. The casting operator function cannot have any return datatype and it does not take any parameter. It have following syntax:
operator datatype()
{ ___________;
___________;
}
Example:
#include<iostream>
using namespace std;
class Number
{ int n;
public:
void printDetails(){ cout<<n; }
Number(int n) { this->n=n; }
operator int() { return n; }
};
int main()
{ Number numb=100;
numb.printDetails();
int x=numb;//numb.operator int()
cout<<"\nThe value is"<<x;
return 0;
}
iii) One class type to other class type : When we assign an object of a class into the object of another class then it is called as class to class conversion. The class to class conversion can be performed either by defining casting operator function in source class or using the constructor in the destination class.
Conversion using casting operator function
#include<iostream>
using namespace std;
class Rectangle
{ int width,length,area;
public:
Rectangle(int w,int l)
{ width=w; length=l; area=width*length;
}
void output()
{ cout<<"\nLength : "<<length<<"\nWidth : "<<width<<"\nArea of rectangle : "<<area;
}
};
class Triangle
{ int base,height;
float area;
public:
Triangle(int b,int h)
{ base =b; height=h; area=0.5*base*height;
}
void print()
{ cout<<"\nBase : "<<base<<"\nHeight : "<<height<<"\nArea of triangle : "<<area;
}
operator Rectangle()
{ Rectangle temp(base,height);
return temp;
}
};
int main()
{ Triangle t(10,20);
Rectangle r=t;//Triangle to Rectangle
t.print();
r.output();
return 0;
}

Conversion using constructor
#include<iostream>
using namespace std;
class Triangle
{ int base,height;
float area;
public:
Triangle(int b,int h)
{ base =b; height=h; area=0.5*base*height;
}
void print()
{ cout<<"\nBase : "<<base<<"\nHeight : "<<height<<"\nArea of triangle : "<<area;
}
int getBase() { return base; }
int getHeight() { return height; }
};
class Rectangle
{ int width,length,area;
public:
void output()
{ cout<<"\nLength : "<<length<<"\nWidth : "<<width<<"\nArea of rectangle : "<<area;
}
Rectangle(Triangle t)
{ width=t.getBase(); length=t.getHeight(); area=width*length;
}
};
int main()
{ Triangle t(10,20);
Rectangle r=t;//Rectangle(t)
t.print();
r.output();
return 0;
}
Note: Replace all < with less than and all > with greater than.
#tarunsir #c++ #programming #c++inhindi

show more

Share/Embed