Page 163 - DCAP404 _Object Oriented Programming
P. 163

Object-oriented Programming                           Mithilesh Kumar Dubey, Lovely Professional University




                    Notes                               Unit 8: Type Conversion


                                     CONTENTS
                                     Objectives
                                     Introduction
                                     8.1  Type Conversions

                                     8.2  Basic Type to Class Type
                                     8.3  Class Type to Basic Type
                                     8.4   Class Type to another Class Type

                                     8.5  Summary
                                     8.6  Keywords
                                     8.7  Review Questions
                                     8.8  Further Readings

                                  Objectives

                                  After studying this unit, you will be able to:

                                      Recognize the type conversions
                                      Describe the basic type to class type
                                      Explain the class type to basic type

                                      Discuss the class type to another type
                                  Introduction


                                  It is the process of converting one type into another. In other words converting an expression of
                                  a given type into another is called type casting.

                                  A type conversion may either be explicit or implicit, depending on whether it is ordered by the
                                  programmer or by the compiler. Explicit type conversions (casts) are used when a programmer
                                  want to get around the compiler’s typing system; for success in this endeavour, the programmer
                                  must use them correctly. Problems which the compiler avoids may arise, such as if the processor
                                  demands that data of a given type be located at certain addresses or if data is truncated because
                                  a data type does not have the same size as the original type on a given platform. Explicit type
                                  conversions between objects of different types lead, at best, to code that is difficult to read.

                                  8.1 Type Conversions

                                  In  a mixed  expression constants  and variables  are of  different data  types. The assignment
                                  operations cause automatic type conversion between the operand as per certain rules.

                                  The type of data to the right of an assignment operator is automatically converted to the data
                                  type of variable on the left.
                                  Consider the following example:

                                  int  x;



          156                               LOVELY PROFESSIONAL UNIVERSITY
   158   159   160   161   162   163   164   165   166   167   168