Data Type Conversion in C++

16,784 views 17 slides May 18, 2014
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

No description available for this slideshow.


Slide Content

Data Type Conversion in C++ BY Mirza Danial Masood. BS(IT) Department,UOS M.B.Din.

C++ Type Conversion The process of converting the data type of a value in another data type is known as data type conversion. The conversion can be performed in two ways: Implicit (Automatic) Explicit (User-defined)

C++ Implicit Conversion It convert data type to another data type automatically. Implicit conversion do not required any operator. The operands in arithmetic operation must be of similar types. An expression in which different data types is called Mixed-type expression.

C++ Implicit Conversion In this case, the result of an expression is evaluated to larger data type in the expression . Suppose an expression contains an integer and double as operands. The result will be evaluated to double data type.

C++ Implicit Conversion

C++ Implicit Conversion Expression Intermediate Type Char + Float Float Int - Long Long Int * Double Double Float / Long Double Long Double

C++ Implicit Conversion Suppose x is an integer and y is long and we want to add up these two variables. x + y . In the above data type of x is lower then data type y . So the value of x will be changed or converted int long during executation.

C++ Implicit Conversion #include< iostream > Using namespace std; Int main() { Int iv=16777217; Float fv=16777216.0; Cout<<“The Integer value is=“<<iv; Cout<<“The Float value is=“<<fv; Cout<<“The Equality is=“<<iv==fv; Return 0; }

C++ Implicit Conversion The Equality is 1. This odd behavior is caused by an implicit cast of iv to float when it is compared with fv; a cast which loses precision, making the values being compared the same. This odd behavior is caused by an implicit cast of iv to float when it is compared with fv; a cast which loses precision, making the values being compared the same.

C++ Implicit Conversion #include< iostream > Using namespace std; int main() { int x, y; A a (10); B b =a; b.getXY ( x,y ); cout <<"x: "<<x<<" y: "<<y<< endl return 0; }

C++ Explicit Conversion It can be converted or performed by the programmer . It will be performed by the using of cast operator or operators . Cast operator tells the computer to convert the data type of a value. Syntax : (type)expression . Type : It indicates the data type to which operand is to be converted.

C++ Explicit Conversion Expression : It indicates the constant, variable or expression or whose data type is to be converted. Example: #include< iostream > Using namespace std; Int main() { float a,b ; int c; c=(int)a % (int)b; Cout<<“Result is”<<c; Return 0; }

C++ Explicit Conversion In the above program two float value are declared . Above Program generate an error because of using the reminder operator with float values. Reminder operator can be used with integer values so we converted these values in int data type. (int)a + (int)b;

C++ Explicit Conversion Traditional explicit type casting allow to convert any pointer into any other pointer type, independently of type they point to. Pointer : It is a simple variable which is declared by using the asterisk sign before the variable.

C++ Explicit Conversion There are Four Types of casting: dynamic_cast : It can be used only with pointers and references to object. Static_cast : It can perform conversions between pointers to related classes, not only from the derived class to its base, but also from a base class to its derived.

C++ Explicit Conversion reinterpret_cast : It convert any pointer type to any other pointer type, even of unrelated classes. const_cast : It minipulates the constness of an object, either to be set or to be moved.

The End cout <<“The End”;
Tags