Tuesday 21 April 2015

Pointers to Derived Class

Pointers can be used with the objects of base classes or derived classes. Pointer to objects of a base class are type -compatible with pointers to objects of a derived class, thus allowing a single pointer variable to be used as pointer to objects of a base class and its derived classes.

C++ makes polymorphism possible through a rule that one should memorize: a base class pointer may address an object of its own class or an object of any class derived from the base class.

The use of a pointer to the objects of a base class with the objects of its derived class raises a new problem. It does not allow access even to public members of a derived class. That is, it allows access only to those members inherited from the base class but not to the members which are defined in the derived class.

No comments:

Post a Comment