We can use pointers not only to the base objects but also to the objects of derived classes. A single pointer variable can be made to point to objects belonging to different classes.
For example :
B *ptr; // pointer to class B type variable
B b; // base object
D d; // derived object
ptr = &b; // ptr points to object b
- In above, example B is base class and D is a derived class from B , then a pointer declared as a pointer to B and point to the object b.
- We can make ptr to point to the object d as follow
ptr = &d;
- We can access those members of derived class which are inherited from base class by base class pointer.
- But we cannot access original member of derived class which are not inherited by base class pointer.
- We can access original member of derived class which are not inherited by using pointer of derived class.