Loading....
Coupon Accepted Successfully!

 

Pointer To Base and Derived Class Objects

In inheritance, the properties of existing classes are extended to the new classes. The new classes that can be created from the existing base class are called as derived classes. The inheritance provides the hierarchical organization of classes. It also provides the hierarchical relationship between two objects and indicates the shared properties between them. All derived classes inherit properties from the common base class. Pointers can be declared to the point base or derived class. Pointers to objects of the base class are type compatible with pointers to objects of the derived class. A base class pointer can point to objects of both the base and derived class. In other words, a pointer to the object of the base class can point to the object of the derived class; whereas a pointer to the object of the derived class cannot point to the object of the base class, as shown in Figure.
 
Fig: Type compatibility of base and derived class pointers
 

15.4 Write a program to access members of base and derived class using pointer objects of both classes.

#include<iostream.h>

#include<constream.h>

class W

{

protected:

int w;

public:

W (int k) { w=k; }

void show()

{

cout<<“\n In base class W”;

cout<<“\n W=”<<w; };

};

class X: public W

{

protected:

int x;

public:

X (int j, int k): W( j)

{

x=k;

}

void show()

{

cout<<“\n In class X”;

cout<<“\n w=”<<w;

cout<<“\n x=”<<x;

}

};
class Y: public X
{

public:
int y;

};
void main()
{

clrscr();
W *b;
b = new W(20); // pointer to class W
b->show();
delete b;
b = new X(5,2); // pointer to class X
b->show();
((X*)b)->show();
delete b;
X x(3,4);
X *d=&x;
d->show();

}

OUTPUT
In base class W
W=20
In base class W
W=5
In class X
w=5
x=2
In class X
w=3
x=4
 
Explanation: In the above program, the class W is a base class. The X is derived from W, and class Y is derived from class X. Here, the type of inheritance is multilevel inheritance. The variable *b is a pointer object of the class W. The statement b = new W (20); creates a nameless object and returns its address to the pointer b. The show() function is invoked by the pointer object b. Here, the show() function of base class W is invoked. Using delete operator, the pointer b is deleted.
 
The statement b = new X (5,2); creates a nameless object of class X and assigns its address to the base class pointer b. Here, it should be noted that the object b is a pointer object of the base class, and it is initialized with the address of the derived class object. Again, the pointer b invokes the function show() of the base class and not the function of class X (derived class.). To invoke the function of the derived class X, the following statement is used:
((X*)b)->show();
 
In the above statement, typecasting (upcasting) is used. The upcasting forces the object of class W to behave as if it were the object of class X. This time, the function show() of class X (derived class) is invoked. The process of obtaining the address of a derived class object, and treating it as the address of a base class object is known as upcasting.
 
The statement X x(3,4); creates object x of class X. The statement X *d = &x; declares the pointer object d of the derived class X and assigns the address of x to it. The pointer object d invokes the derived class function show(). Figure shows a pictorial representation of the above explanation.
 
Fig: Type compatibility of base and derived class pointers
 
Here, the pointer of class W points to its own class as well as its derived class. The pointer of derived class X can point to its own class but cannot point to its base class.




Test Your Skills Now!
Take a Quiz now
Reviewer Name