Presentation is loading. Please wait.

Presentation is loading. Please wait.

Type Conversions. 2 When different types of variables are mixed in an expression, C applies automatic type conversion to the operands The type of data.

Similar presentations


Presentation on theme: "Type Conversions. 2 When different types of variables are mixed in an expression, C applies automatic type conversion to the operands The type of data."— Presentation transcript:

1 Type Conversions

2 2 When different types of variables are mixed in an expression, C applies automatic type conversion to the operands The type of data to the RHS of an assignment operator is automatically converted to the type of the variable on LHS Eg: int a; float b=12.5; a=b; b is converted to integer before its value assigned to a ie truncate the fractional part The type conversions are automatic only when the data types involved are built-in types.

3 3 Type Conversions For user defined data types, the compiler does not support automatic type conversions. C++ design the conversion routines by ourselves. Different situations of data conversion between incompatible types. 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.

4 4 Type Conversions Basic to class type The conversion from basic type to class type can be done with constructor eg class time { int hrs; int mins; public : time (int t) { hrs = t / 60; mins = t % 60; } }; main() { int d = 80; time T1 = d; // conversion (implicit) }

5 5 Type Conversions Basic to class type Here time T1; - object of the class time int d=80; - integer T1=d; convert integer to class time type After the conversion the hrs of T1 will contains 1 And mins will contains 20 A constructor that takes a single argument, used for the type conversion to class type

6 6 Type Conversions Class to Basic type A constructor function do not support type conversion from a class type to a basic type. An overloaded casting operator is used to convert a class type data to a basic type. It is also referred to as conversion function. operator typename( ) { … … ( function statements ) … } This function converts a class type data to typename.

7 7 Type Conversions Class to Basic type class check { int a[10],i,n; public: check(int x){n=x;} void read() { for(i=0;i<n;i++) { cin>>a[i];} } operator float( ) { float sum = 0; for (int i=0; i < size ; i++) { sum = sum + a[i] * a[i]; } return sqrt (sum);} }; void main() { int a=5; check k=a; // basic to class, using constructor k.read(); float p=k; // class to basic type using casting operator cout<< p; }

8 8 Type Conversions Class to Basic type The casting operator function should satisfy the following conditions: – It must be a class member. – It must not specify a return type. – It must not have any arguments. operator float( ) { float sum = 0; for (int i=0; i < size ; i++) { sum = sum + a[i] * a[i]; } return sqrt (sum);}

9 9 Type Conversions One Class To Another Class Type obj1 = obj2 ; // objects of different types obj1 is an object of class X and obj2 is an object of class Y. The class Y type data is converted to the class X type data and the converted value is assigned to the obj1. Conversion is takes place from class Y to class X. – Y is known as source class. – X is known as destination class.

10 10 Type Conversions One Class To Another Class Type Conversion between objects of different classes can be carried out by either a constructor or a conversion function. The source class contains constructor And The destination class contains the conversion function

11 11 Type Conversions One Class To Another Class Type class X { int x; public: operator int() { return(x); } }; class Y { int y; public: Y(X obj) { y=obj; } void show() {cout<<y;} }; void main() { clrscr(); int a=100; X obj1; Y obj2=obj1; obj2.show(); getch(); } Output 100

12 12 Type Conversions One Class To Another Class Type Conversion required Conversion Take place in Source ClassDestination Class Basic to ClassNot applicableConstructor Class to BasicCasting OperatorNot Applicable Class to ClassCasting OperatorConstructor


Download ppt "Type Conversions. 2 When different types of variables are mixed in an expression, C applies automatic type conversion to the operands The type of data."

Similar presentations


Ads by Google