Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

Type Conversions in C++

 
Category: C++
Comments (2)

Type Conversions in C++

In this C++ Tutorial you will learn about Type Conversions in C++, What is Type Conversion, How to achieve this, Automatic Conversion otherwise called as Implicit Conversion and Type casting otherwise called as Explicit Conversion



What is 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
Type casting otherwise
called as Explicit Conversion


Let us see each of these in detail:


Automatic Conversion otherwise called as Implicit Conversion

This is not done by any conversions or operators. In other words value gets automatically converted to the specific type in which it is assigned.


Let us see this with an example:


#include <iostream.h>
void main()
{
short x=6000;
int y;
y=x;
}


In the above example the data type short namely variable x is converted to int and is assigned to the integer variable y.


So as above it is possible to convert short to int, int to float and so on.


Type casting otherwise called as Explicit Conversion

Explicit conversion can be done using type cast operator and the general syntax for doing this is


datatype (expression);


Here in the above datatype is the type which the programmer wants the expression to gets changed as


In C++ the type casting can be done in either of the two ways mentioned below namely:


C-style casting
C++-style casting


The C-style casting takes the synatx as


(type) expression


This can also be used in C++.


Apart from the above the other form of type casting that can be used specifically in C++ programming language namely C++-style casting is as below namely:


type (expression)


This approach was adopted since it provided more clarity to the C++ programmers rather than the C-style casting.
Say for instance the as per C-style casting


(type) firstVariable * secondVariable


is not clear but when a programmer uses the C++ style casting it is much more clearer as below


type (firstVariable) * secondVariable


Let us see the concept of type casting in C++ with a small example:


#include <iostream.h>
void main()
{
int a;
float b,c;
cout<< “Enter the value of a:”;
cin>>a;
cout<< “n Enter the value of b:”;
cin>>b;
c = float(a)+b;
cout<<”n The value of c is:”<<c;
}


The output of the above program is


Enter the value of a: 10
Enter the value of b: 12.5
The value of c is: 22.5



In the above program a is declared as integer and b and c are declared as float. In the type conversion statement namely


c = float(a)+b;


The variable a of type integer is converted into float type and so the value 10 is converted as 10.0 and then is added with the float variable b with value 12.5 giving a resultant float variable c with value as 22.5


-R.Sripriya



Read Next: Class Constructors and destructors in C++



 

 

Comments


sailaja said:

  HI,

I think in the above example y=a,It should be y=x.we cannot assign a variable with out declaring.In the above example 'a' is not declared.
December 17, 2008, 8:00 pm

Tejas Gajera said:

  hi,

In this page program is given to explain type casting from short to int. which is

#include <iostream.h>
void main()
{
short x=6000;
int y;
y=a;
}

but there is an error in line 6, which is y=a. Instead of that it should be y=x; because a is undeclared variable.

Tejas Gajera
June 17, 2009, 12:53 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape