The pointer of Base Class pointing different object of derived class:
- A derived class is a class which takes some properties from its base class.
- It is true that a pointer of one class can point to other class, but classes must be a base and derived class, then it is possible.
- To access the variable of the base class, base class pointer will be used.
- So, a pointer is type of base class, and it can access all, public function and variables of base class since pointer is of base class, this is known as binding pointer.
- In this pointer base class is owned by base class but points to derived class object.
- Same works with derived class pointer, values is changed.
Below is the C++ program to illustrate the implementation of the base class pointer pointing to the derived class:
Displaying Base class variable var_base: 34 Displaying Base class variable var_base: 3400 Displaying Base classvariable var_base: 9448 Displaying Derived class variable var_derived: 98
- A pointer to derived class is a pointer of base class pointing to derived class, but it will hold its aspect.
- This pointer of base class will be able to temper functions and variables of its own class and can still point to derived class object.