 Welcome to the session on type conversion. At the end of this session, student will be able to write C++ program with type conversion. This video is in continuation with my previous video on type conversion. As we know, 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 the previous video, we have seen basic to class type and class to basic type conversion. In this video, we will see conversion from one class type to another class type. For type conversions, we need to use constructor or casting operator function. Consider the statement v3 equal to v1 plus v2, where v1, v2 and v3 are objects of the same class. The statement adds two objects and then assigns the result to a third object. When the objects are of the same class type, the operations, addition and assignment are carried out smoothly and compiler does not make any complaints. In case of class objects, the values of all the data members of the right hand object are simply copied into the corresponding members of the object on the left hand. But what if the objects belong to two different classes? Consider two classes, time and minute. Time class represents time in hours and minutes. Minute class represents time in minutes. T is an object of time class and m is an object of minute class. What happens if our program contains statements like t equal to m or m equal to t? The compiler does not support automatic type conversions for user-depend data types. We must therefore design the conversion routines by ourselves if such operations are required. Let's see one more example. Object x equal to object y. Object x is an object of class x. Object y is an object of class y. These are objects of different types. The class y type data is converted to class x type data and the converted value is assigned to object x. Since the conversion takes place from class y to class x, y is known as source class and x is known as the destination class. Such conversions between objects of different classes can be carried out by either a constructor or a conversion function. Then how do we decide which form to use? It depends upon where we want the conversion function to be located in the source class or in the destination class. Let's see conversion from one class type to another class type. To perform the conversion from one class to another class type, a constructor should be used in the destination class. The syntax is shown here. A single argument constructor function serves as an instruction for converting the arguments type to the class type of which it is a member. This implies that the argument belongs to the source class and it is passed to the destination class for conversion. This makes it necessary that the conversion constructor be placed in the destination class. Let's see an example of conversion from one class to another class For conversion, we have defined constructor in destination class. Here we have two classes class time and class minute. Time represents time in hours and minutes. Time class has two constructors, a default constructor and a parameterized constructor. Special access functions get minutes and get hours. As the data members of time class are private, so to access these data members from our destination class, we have designed special access functions. These functions are called from the constructor of a destination class. A display function is used to display the values. Class minute has a data member minute. It has two constructors, a default constructor which initializes minute with zero. The other constructor is used for conversion of one class to another class. Display function displays total minutes. T1 is an object of time class. When T1 is constructed, the values 2 and 30 are passed to the constructor. The constructor initializes hours with 2 and minutes with 30. M1 is an object of minute class. It is constructed, a default constructor is called and it initializes minutes with zero. Here we have a statement m1 equal to T1. Time is a source class and minute is a destination class. And we have defined a conversion function that is constructor in a destination class that is minute class. When this statement is executed, T1 is passed to the constructor function. So, T refers to T1. T.get us, this function is called from time class. It returns value 2. Get minutes function is called from time class. It returns value 30. So, 2 into 60 plus 30, the value is 150. That is assigned to minutes. Now, T1.display, a display function is called on T1 object. It is of time class. So, it displays hours and minutes. That is, it will display hours equal to 2 and minutes equal to 30. And m1.display, it calls the display function from minute class. So, it displays total minutes, that is 150. Let's see the output. We should make use of casting operator function in the source class. In the case of convergence between objects, type name refers to the destination class. Therefore, when a class needs to be converted, a casting operator function can be used. The conversion takes place in the source class and the result is given to the destination class object. Let's see an example of conversion from one class to another class that uses casting operator function in source class. We have used the same classes time and minute in this program as well. When T1 is constructed, values 2 and 30 are passed to the constructor. It initializes hours with 2 and minutes with 30. When m1 object of minute class is constructed, it initializes minutes with 0. A statement m1 equal to T1 where time is the source class and minute is the destination class. Now in this program, we have used a casting operator function and that is defined in the source class, that is time class. When this statement is executed, T1 is passed implicitly to this function. The values of T1 object are used here. So, 2 into 60 plus 30, the value is 150. 150 is passed to the set function. Set function is called an object of minute class. So, a function with value 150 is called. This assigns this value of m that is 150 to minute and it returns object T and that returned object is assigned to m1. Now, display function is called on T1 object. So, it displays the value 2 and 30 and the display function on m1 object is called. So, it displays 150. Let's see the output. Hours 2 minutes 30, total minutes 150. Now pause the video for some time and find the answer. If we have two classes X and Y, A is an object of class X, B is an object of class Y. We want to say A equal to B. What type of conversion routine should be used and where? The answer is with statement A equal to B, class X is destination and class Y is source. If constructor is used, it should be defined in destination class that is X. If casting operator function is used, it should be defined in source class that is Y. This table provides a summary of all the three conversions. It shows that the conversion from a class type to any other type should make use of casting operator in the source class. On the other hand, to perform the conversion from any other type to a class type, a constructor should be used in the destination class. In this video, we have seen conversion from one class type to another class type. Thank you.