[go: up one dir, main page]

0% found this document useful (0 votes)
7 views19 pages

3.3 Type Casting

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

Type Conversion

--Prof. S.N.Shelke
Type Conversion
 It is the process of converting one type into another. In other words

converting an expression of a given type into another is called type


casting.

 How to achieve this?

 There are two ways of achieving the type conversion namely:

 Automatic Conversion otherwise called as Implicit Conversion

This is not done by any conversions or operators. In other words the


value gets automatically converted to the specific type to which it is
assigned.

 Type casting otherwise called as Explicit Conversion


1. Implicit type conversions

 Implicit type conversions can occur when assigning a value to a

memory location

 Or, when processing calculation statements

 Value can be promoted or demoted

 Implicit demotion can adversely affect output

3
Implicit type conversion (cont...)
 Implicit type conversion (also called automatic type
conversion or coercion) is performed whenever one fundamental data type
is expected, but a different fundamental data type is supplied, and the user
does not explicitly tell the compiler how to perform this conversion (via a
cast).
double d = 3; // convert integer 3 to a double
short s = 2; // convert integer 2 to a short
short int i = 2;
int s = i; // convert from short to int
cout << s; Output:
float f = 0.1234; 2
0.1234
double d= f;
cout << d;
2. Explicit Type casting
 Use explicit type conversion (type casting) to convert an item from one
data type to another

 C-style casts

In standard C programming, casts are done via the () operator, with the
name of the type to cast to inside. For example:

int i1 = 10;
int i2 = 4;
float f = (float)i1 / i2;

 C++ will also let you use a C-style cast with a more function-call like syntax:

int i1 = 10;
int i2 = 4;
float f = float(i1) / i2;
Type conversion

int x;

float m=4.345;

x=m;

What happens with user defined data types?

For eg:

v3 =v1+v2; // same class


Three situations for type conversion

1) Conversion from basic type to class type

2) Conversion from class type to basic type

3) Conversion from one class type to another class type


1. Basic to class type

// int to class type:


class time
{
int hrs;
int mins;
Public:
………….
time(int t) // constructor
{
hours = t/60; // t in minutes
mins = t%60;
}
};
 Following conversion can be used in a function

time T1; // object T1 created


int duration = 120;
T1= duration; // int to class

 Constructor is used for type conversion take a single argument


whose type is to be converted
2. Class to basic type

 Constructor does not support for this type conversion

 Overloaded casting operator could be used to convert a class


type data to basic type
 Syntax :

Operator typename()
{
……………..
……………. (Function statements)
…………….
}
#include<iostream> sample::operator double()
Using namespace std; {
class sample double sum=0;
{ for(int i=0;i<5;i++)
int v[5]; {
public: sum=sum+v[i];
void read(); }
operator double(); return sum;
}; }

void sample::read() int main()


{
{
sample x1;
for(int i=0;i<5;i++)
x1.read();
{
double s=double(x1);
cout<<"enter value";
cout<<"s="<<s;
cin>>v[i];
return 0;
} }
}
The casting operator should satisfy the following conditions:

1. It must be a class member.

2. It must not specify a return type.

3. It must not have any arguments.


3. One class to another class type

 Eg : objx=objy; // objects of different class

 Conversions of objects of different class can be carried out by


either a constructor or conversion function

 Compiler treats both in the same way.

 It depends upon where we want the conversion function located in


source class or in destination class.
One class to another class type

operator typename();

 typename may be built in data type or user defined.

 When a class needs to be converted, a casting operator function can be

used(i.e. source class).

 The conversion take place in source class & result is given to

destination class object.


One class to another class type

Conversion takes place in


Coversion required
Source Class Destination class

Basic  class Not applicable Constructor

Class  Basic Casting operator Not applicable

Class  Class Casting operator Constructor


#include<iostream> class seconds
using namespace std; {
double s;
class minutes
public:
{
seconds(double y)
double m; {
public: s=y;
minutes(double x) }
{ void display()
m=x; {
} cout<<“seconds=“<<s;
Converter
void display() }
function
{
operator minutes()
cout<<“minutes=“<<m;
{
}
return minutes(s/60);
double getdata() } Constructor
{
return m; seconds (minutes m1)
} {
}; s=m1.getdata()*60;
}
};
int main()

cout<<“conversion using operator function”;

seconds sec1=90;

minutes min1=sec1; //conversion from seconds to minutes

sec1.display();

min1.display();

cout<<“conversion using constructor”;

minutes min2=5;

seconds sec2=min2; //conversion from minutes to seconds

min2.display();

sec2.display();

return 0;

}
Prof. S. N. Shelke
(Assistant Professor)
Department of Computer
Engineering
Sinhgad Academy of Engineering,
Kondhwa, Pune

You might also like