Type Conversions Type Conversions When different types of






![Type Conversions Class to Basic type class check { int a[10], i, n; public: Type Conversions Class to Basic type class check { int a[10], i, n; public:](https://slidetodoc.com/presentation_image_h2/9d44ed76a7d7ccca2291772eb8bc8e46/image-7.jpg)





- Slides: 12

Type Conversions

Type Conversions 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. 2

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. 3

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 T 1 = d; // conversion (implicit) } 4

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

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. 6
![Type Conversions Class to Basic type class check int a10 i n public Type Conversions Class to Basic type class check { int a[10], i, n; public:](https://slidetodoc.com/presentation_image_h2/9d44ed76a7d7ccca2291772eb8bc8e46/image-7.jpg)
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; } 7

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

Type Conversions One Class To Another Class Type obj 1 = obj 2 ; // objects of different types obj 1 is an object of class X and obj 2 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 obj 1. Conversion is takes place from class Y to class X. – Y is known as source class. – X is known as destination class. 9

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 10

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 obj 1; Y obj 2=obj 1; obj 2. show(); getch(); } Output 100 11

Type Conversions One Class To Another Class Type Conversion required Conversion Take place in Source Class Destination Class Basic to Class Not applicable Constructor Class to Basic Casting Operator Not Applicable Class to Class Casting Operator Constructor 12