 Welcome to the session on type conversion. At the end of this session, student will be able to write C++ program with type conversion. We know that when constants and variables of different types are mixed in an expression, C applies automatic type conversion to the operands as per certain rules. Similarly, an assignment operation also causes the automatic type conversion. The type of data to the right of an assignment operator is automatically converted to the type of the variable on the left. For example, the statements end m, float x equal to 3.14159, m equal to x, assigns an integer value to m and truncates fractional part. The type conversions are automatic as long as data types involved are built in types. Let's see a program of implicit conversion. Here x is a variable of integer type, it has value 10. y is a character variable, its value is a. The statement x equal to x plus y implicitly converts y to integer. And the ASCII value of a, that is 97, is considered for addition. So, 10 plus 97 is 107, that is stored in x. Float z equal to x plus 1.0. This statement implicitly converts x to float. So, the value of x is 107. 107 plus 1 is 108, that is stored in z. The values of x, y and z are displayed. Let's see the output. x equal to 107, y equal to a, z equal to 108. We have seen type conversions for built-in types. What happens when they are user-defined data types? Consider the statement that adds two objects and then assigns the result to a third object. v3 equal to v1 plus v2, where v1, v2 and v3 are class-type objects. When the objects are of the same type, the operations, addition and assignment are carried out smoothly. And the compiler does not make any complaints. After addition, the values of all the data members of the right-hand object are simply copied into corresponding members of the object on the left-hand. But what if one of the operands is an object and the other is a built-in type variable? Or what if they belong to two different classes? The compiler does not support automatic conversions for user-defined data types. We must therefore design the conversion routines by ourselves if such operations are required. Three types of situations might arise in data conversion between incompatible types. First, conversion from basic type to class type. Second, conversion from class type to basic type. Third, conversion from one class type to another class type. In this video, we will discuss first two cases in detail. For type conversions, we need to use constructor or casting operator function. First case is conversion from basic type to class type. The conversion from basic type to class type is accomplished with constructors. Constructors are generally used to initialize objects. The constructor used for type conversion take a single argument whose type is to be converted. The general form is given here. To convert from basic type to class type, the left-hand operand of equal to operator is always a class object. We can also accomplish this conversion using an overloaded equal to operator. Let's see a program to convert basic type to class type. The program has a class time. It has two data members, hours and minutes. Two constructors, a default constructor which initializes hours and minutes to zero. And the other constructor used for conversion of basic type to class type. A display function which displays the data members. T1 is an object of time class. Duration is a variable of integer type. Duration is initialized with 85 that is 85 minutes. The statement T1 equal to duration where T1 is an object of a class. Duration is a variable of integer type. Duration is passed to the constructor and T1 is passed implicitly to the constructor function. So the value of T is 85 that is 85 minutes. To convert 85 minutes to hours and minutes we divide it by 60 and the quotient is stored in hours and the remainder is stored in minutes. So after conversion we receive the values hours equal to 1 and minutes equal to 25. Then a statement T1.display calls display function and displays the values of hours and minutes. Let's see the output. After conversion the values printed are 1 hour 25 minutes. Now we will see conversion from class type to basic type. In the previous example we have seen constructor converted basic to class type. What about conversions from class to basic type? The constructor functions do not support this operation. C++ allows us to define an overloaded casting operator that could be used to convert class type data to basic type. The general form of an overloaded casting operator function is given here. Usually it is referred to as conversion function. This function converts a class type data to specified type. For example, the operator double function converts class object to type double. 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. Since it is a member function it is invoked by the object and therefore the values used for conversion inside the function belong to the object that invoked the function. This means that the function does not need an argument. Let's see a program to convert class type to basic type. Here a class name is time. It has two data members, hours and minutes of integer type. A constructor to initialize the data members and operator integer function to convert class to integer type. Duration is a variable of integer type. T is an object of time class. When T is constructed the values 1 and 25 are passed to the constructor. The constructor initializes hours with 1 and minutes with 25. The statement duration equal to T where duration is integer variable and T is class type object. So this statement calls operator integer function. T is implicitly passed to the operator function. The function converts T to integer. The statement class type to basic type conversion is displayed. hours data member of object T is 1. 1 into 60 plus minutes. Minutes is 25. So the converted value that is 85 is returned from this function. And the value returned is assigned to duration variable. And after conversion total minutes are displayed. So total minutes are 85 will be the result. Let's see the output. Now pause the video for some time and define appropriate conversion function in the given code to make the program error free. Assign 5.5 to both data members A and B of object N. Once you have done this, you may resume the video. The answer to the previous question is given here. A constructor with double type parameter is added to the program. N is an object of number class. The statement N equal to 5.5 converts basic type to class type. It calls a constructor with double type parameter. The value 5.5 is assigned to both data members A and B inside a constructor. This table provides the summary of type conversions. In this video we have seen basic to class type and class to basic type conversion. Thank you.