13.9 IMPLICIT DERIVED-CLASS OBJECT TO BASE-CLASS OBJECT CONVERSION

In this chapter, we made a statement “To support polymorphism in an elegant way, C++ makes a special deviation from its highly typed nature. It allows pointer to base class to point to the derived class. Note that its converse is not true”. We have also studied that where this facility can be used for an advantage.

If this is the case with pointers to objects, one will wonder what the case with objects themselves is. Can we convert implicitly object of base class into object of derived class or vice versa.

Consider our example of derived class Hero from base class Joker. Consider the following code:

Joker j1(“Clown”);  Hero h1(“Raj”);  j1=h1;

If you try to convert object of the ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.