Pointers to derived classes

It is possible to declare a pointer that points to the base class as well as the derived class. One pointer can point to different classes. For example, X is a base class and Y is a derived class. The pointer pointing to X can also point to Y.

13.15 Write a program to declare a pointer to the base class and access the member ­variable of base and derived class.
// Pointer to base object //
#include<iostream.h>
#include<conio.h>
class A
{
public :
int b;
void display()
{
cout<<“b = ” <<b <<“\n”;
}
};
class B : public A
{
public :
int d;
void display()
{
cout<<“b= ” <<b <<“\n” <<“ d=”<<d <<“\n”;
}
};
main()
{
clrscr();
A *cp;
A base;
cp=&base;
cp->b=100;
// cp->d=200; Not Accessible
cout<<“\n cp points to the base object \n”;
cp->display();
B b;
cout<<“\n cp points to the derived class \n”;
cp=&b;
cp->b=150;
// cp->d=300; Not accessible
cp->display();
return 0;
}
OUTPUT
cp points to the base object
b = 100
cp points to the derived class
b = 150

Explanation: In the above program, A and B are two classes with a one-integer member variable and member function. The class B is derived from class A. The pointer cp points to the class A. The variable base is an object of the class A. The address of the object base is assigned to pointercp. The pointer cp can access the member variable b, a member of the base class, but cannot access the variable d, a member of the derived class. Thus, the following statement is invalid:
 
a) cp->d = 200;
 
The variable b is an object of class B. The address of object b is assigned to the pointer cp. However, b is an object of the derived class; access to the member variable d is not possible. Thus, the following statement is invalid:
 
b) cp->d = 300;
 
Here, cp is a pointer of the base class, and it cannot access the members of the derived class.
 
13.16 Write a program to declare a pointer to the derived class and access the member variable of base and derived class.
// Pointer to derived object //
#include<iostream.h>
#include<conio.h>
class A
{
public :
int b;
void display()
{
cout<<“b = ” <<b <<“\n”;
}
};
class B : public A
{
public:
int d;
void display()
{
cout<<“\tb= ” <<b <<“\n” <<“\td= ”<<d <<“\n”;
}
};
main()
{
clrscr();
B *cp;
B b;
cp=&b;
cp->b=100;
cp->d=350;
cout<<“\n cp points to the derived object \n”;
cp->display();
return 0;
}
OUTPUT
cp points to the derived object
b= 100
d= 350

Explanation: 
The above program is similar to the previous one. The only difference is that the pointer cp points to the object of the derived class. The pointer cp is a pointer of the class B. The variable b is an object of the class B. The address of b is assigned to the pointer cp. The pointer cp can access the member variables of both base and derived classes. The output of the program is shown above.
 
13.17 Write a program to declare pointer to class. Invoke constructor to initialize data member. Call member function using pointer.
#include<iostream.h>
#include<conio.h>
class A
{
private :
int a;
public:
A()
{
a=20;
}
void show()
{
cout<<“\n a = ”<<this->a ;
}
};
int main()
{
clrscr();
A *p,*q;
q=p->A::A();
p->show();
q->show();
return 0;
}
OUTPUT
a = 20
a = 20

Explanation:
 In the above program, *p and *q are pointers of class A. The constructor is not executed for pointers. The constructor of class A is explicitly called pointer p. Data members of both pointer objects p and q are initialized to 20. The statements p->show() and q->show() are used to invoke the member function using pointers. The output of the program is shown above.
 
13.18 Write a program to declare object and pointer to class. Assign value of object to pointer to object. Display their values. Also carry out conversion from basic type to class type.
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
class A
{
private :
int a;
public:
A()
{
a=30;
}
void show() { cout<<“\n a = ”<<a ;}
A (int x)
{
this->a=x;
}
};
int main()
{
clrscr();
A k,*b,a;
*b=50;
k=*b;
b->show();
a.show();
k.show();
return 0;
}
OUTPUT
a = 50
a = 30
a = 50

Explanation:
 In the above program, a and k are objects of class A. The *b is a pointer object. The objects a and k are initialized to 30 by the constructor. The statement *b = 50 assigns 50 to data member a. This is carried out by the conversion constructor A (int). The value of *b is assigned to object k. The member function show() is called by all the three objects, and the output of the program is as given above.