Pointer to the derived class and base class
For Example: Write a program to declare a pointer
to the derived class and access the member variable of base and derived class.
#include<iostream.h>
#include<conio.h>
class A
{
public:
int b;
void display()
{
cout<<"Value B is="<<b<<endl;
}
};
class B : public A
{
public:
int d;
void display()
{
cout<<"Value of B is
="<<b<<endl<<"Value of D is
="<<d<<endl;
}
};
void main()
{
B *cp; // here cp is pointer object derived class
B b; // here b is
object of B class
cp=&b;
//initialized simple object to pointer object
cp->b=200; //Assign value to pointer object
cout<<"CP points to the derived
object"<<endl;
cp->display(); //member function calling with the help of
pointer object
getch();
}
Output:
Comments
Post a Comment