Typeconversion in C++
Typeconversion in C++
The following is the correct order of data types from lower rank to higher rank:
bool -> char -> short int -> int -> unsigned int -> long int -> unsigned long int -> long
long int -> float -> double -> long double
#include <iostream>
using namespace std;
int main()
{
int num;
double num2 = 15.25;
num = num2;
cout << " The value of the int variable is: " << num << endl;
cout << " The value of the double variable is: " << num2 << endl;
return 0;
}
Conversions that require user intervention to change the data type of one variable to
another, is called the explicit type conversion. In other words, an explicit conversion allows
the programmer to manually changes or typecasts the data type from one variable to
another type. Hence, it is also known as typecasting. Generally, we force the explicit type
conversion to convert data from one type to another because it does not follow the implicit
conversion rule.
The explicit type conversion is divided into two ways:
1. Explicit conversion using the cast operator
Program to convert float value into int type using the cast
operator
Cast operator: In C++ language, a cast operator is a unary operator who forcefully converts
one type into another type.
#include <iostream>
using namespace std;
int main ()
{
float f2 = 6.7;
int x = static_cast <int> (f2);
cout << " The value of x is: " << x;
return 0;
}
Output
The value of x is: 6
Let's consider an example to convert the data type of one variable into another using the
assignment operator in the C++ program.
#include <iostream>
using namespace std;
int main ()
{
float num2;
int num1 = 25;
num2 = (float) num1;
cout << " The value of int num1 is: " << num1 << endl;
cout << " The value of float num2 is: " << num2 << endl;
return 0;
}
Output
The value of int num1 is: 25
The value of float num2 is: 25.0
When we create object using the variables of primary data types then it is called as basic data type
convert to the class type.
Ex:-
#include <iostream>
using namespace std;
class Number
{
int n;
public:
void print()
{
cout<<n;
}
Number(int n)
{
this->n=n;
}
};
int main() {
Number numb1=100;
numb1.print();
return 0;
}
Output
100
int main() {
Number numb1=100;
numb1.print();
int x=numb1;
cout<<"The value of X="<<x;
return 0;
}
Output
100
The value of X=100
Class to class conversion:-
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 by defining casting operator function in source class.
Ex.:-
#include <iostream>
using namespace std;
class Rectangle
{
int width,length,area;
public:
Rectangle(int w,int l)
{
width=w;
length=l;
area=length*width;
}
void output()
{
cout<<"\nLength="<<length<<"Width="<<width<<"Area="<<area<<endl;
}
};
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<<"Height="<<height<<"Area Tri="<<area<<endl;
}
operator Rectangle()
{
Rectangle tmp(base,height);
return tmp;
}
};
int main() {
Triangle t(10,20);
Rectangle r=t;
t.print();
r.output();
return 0;
}
Output:-
Base=10 Height=20 Area Tri=100
Length=20 Width=10 Area=200