0% found this document useful (0 votes)
10 views6 pages

Typeconversion in C++

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Typeconversion in C++

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

BCA-2001 Object Oriented Programming using C++

Type Conversion in C++


Type conversion is the process that converts the predefined data type of one variable into an
appropriate data type. The main idea behind type conversion is to convert two different data
type variables into a single data type to solve mathematical and logical expressions easily
without any data loss.
For example, we are adding two numbers, where one variable is of int type and another of
float type; we need to convert or typecast the int variable into a float to make them both
float data types to add them.
Type conversion can be done in two ways in C++, one is implicit type conversion, and the
second is explicit type conversion. Those conversions are done by the compiler itself, called
the implicit type or automatic type conversion. The conversion, which is done by the user or
requires user interferences called the explicit or user define type conversion.

Implicit Type Conversion


The implicit type conversion is the type of conversion done automatically by the compiler
without any human effort. It means an implicit conversion automatically converts one data
type into another type based on some predefined rules of the C++ compiler. Hence, it is
also known as the automatic type conversion.
For example:
int x = 20;
short int y = 5;
int z = x + y;

Order of the typecast in implicit conversion

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;
}

Explicit type conversion

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

2. Explicit conversion using the assignment 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

Program to convert one data type into another using the


assignment operator

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

Basic data type convert to class type:-

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

Class type conversion to Basic data type (By using casting


object):-
When we assign an object to a primitive data type variable then it is called as class type object convert
to the basic data type.
Ex:-
#include <iostream>
using namespace std;
class Number
{
int n;
public:
void print()
{
cout<<n<<endl;
}
Number(int n)
{
this->n=n;
}
operator int()
{
return n;
}
};

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

You might also like