Page 168 - DCAP404 _Object Oriented Programming
P. 168

Unit 8: Type Conversion




          to the source class and is passed to the destination class for conversion. Therefore the conversion  Notes
          constructor must be placed in the destination class.
          The following Figure 8.1 illustrates the above two approaches.

                                 Figure 8.1:  Conversions between  Objects






















          The Table 8.1 summarizes all the three conversions. It shows that the conversion from a class to
          any other type (or any other class) makes use of a casting operator in the source class. To perform
          the  conversion from  any other  type or  class to  a class  type, a  constructor is  used  in  the
          destination class.

                                             Table  8.1




















          When a conversion using a constructor is performed in the destination class, we must be able to
          access the data members of the object sent (by the source class) as an argument.





              Task ’Since data members of the source class are private, we must  use special  access
             functions in the source class to facilitate its data flow to the destination class.’ Explain this
             statement.

          Consider the following example of an inventory of products in a store. One way of keeping
          record of the details of the products is to record their code number, total items in the stock and
          the cost of each item. Alternatively we could just specify the item code and the value of the item




                                           LOVELY PROFESSIONAL UNIVERSITY                                   161
   163   164   165   166   167   168   169   170   171   172   173