A Complete Guide to Programming in C++ part 56. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | chapter Type Conversion in Class Hierarchies This chapter describes implicit type conversion within class hierarchies which occurs in the context of assignments and function calls. In addition explicit type casting in class hierarchies is discussed in par ticular upcasting and downcasting. 529 530 CHAPTER 24 TYPE CONVERSION IN CLASS HIERARCHIES CONVERTING TO BASE CLASSES Example for implicit conversion include bool compare Car Car int main PassCar beetle New Beetle false 3421 VW miata Miata true 2512 Mazda bool res compare beetle miata . X X ok Implicit conversion to base class. Car a beetle Car b miata bool compare Car a Car b Here a is the base part of beetle b is the base part of miata. If this is inconvenient an explicit type cast to type PassCar has to be performed. CONVERTING TO BASE CLASSES 531 Implicit Conversion If a class is derived from another class by public inheritance the derived class assumes the characteristics and features of the base class. Objects of the derived class type then become special objects of the base class just like an automobile is a special type of vehicle. You can utilize the is relationship when handling objects. It is possible to assign an object of a derived class to an object of the base class. This causes an implicit type conversion to a base class type. The base class thus becomes a generic term for multiple special cases. Given that the classes PassCar and Truck were derived from the Car class objects of the PassCar or Truck type can always be managed like objects of Car type. Assignments Implicit type conversion in class hierarchies occurs in assignments to base class objects pointers or references to the base class. Function Calls Additionally a similar kind of implicit type conversion takes place for the arguments of function calls. Given the function compare with the following prototype Example bool compare Car Car and two objects of the derived PassCar class type beetle and miata the following statement is valid