C++ pointer to derived class

C++ pointer to derived class

Pointer to objects of the base class are type compatible with the pointers to objects of a derived class. Therefore a single pointer variable can be made to point to the objects belonging to different classes lets see in Example if B is a base class and D is a derived class from B , Then a pointer declare as a pointer to B can also be a pointer to D. consider the following declarations

B *cptr; // here is pointer to class B type variable
B b; // base object
D d; // derived object
cptr=&b; // cptr points to object b

we can make in the above example cptr to point to the object d as follows
cptr=&d; // cptr points to object d
This is perfectly valid with c++ language because d is an object derived from the class B

however these is a problem in using cptr to access the public of the derived class D. using cptr we can access only those members which are inherited from B and not the member that originally belongs to D. in case a member of D class has the same name as one of the members of class B, then any reference to that member by cptr variable will always access the base class member.

Althought c++ language permits a base pointer to point to any object derived from the base the pointer cannot be directly used to access all the members of the derived class. we may have to use another pointer declared as pointer to the derived type.

let us take example of how pointers to a derived object are used

POINTER TO DERIVED OBJECTS #include<iostream>
using namespace std;
class BC
{
public:
int b;
void show()
{
cout << "b=" << b << "\n";
}
};
class DC: public BC
{
public:
int d;
void show()
{
cout << "b=" << b << "\n";
cout << "d=" << d << "\n";
}
};
int main()
{
BC *bptr; // base pointer
BC base;
bptr=&base; //base address
bptr->b=100; // access BC via base pointer<
cout << "bptr points to base object \n";
bptr->show();
//derived class
DC derived;
bptr=&derived; // address of derived object
bptr->b=200; // access DC via base pointer
/* bptr->d=300; */ // won't work
cout << "bptr now points to derived object \n";
bptr->show(); //bptr now points to derived object
/* accessing d using a pointer of type derived class DC*/
DC *dptr; // derived type pointer
dptr=&derived;
dptr->d=300;
cout << "dptr is derived type pointer \n";
dptr->show();
cout << "using ((DC*)bptr)\n";
((DC*)bptr)->d=400;
((DC*)bptr)->show();
}

output:
bptr points base object
b=100
bptr now points to derived object
b=200
dptr is derived type pointer
b=200
d=300
using((DC?)bptr)
b=200
d=400

Note: we have used the statement
bptr->show();
so two times first when bptr points to the base object, and second when bptr is made to point to the derived object. but both the times, it executed BC::show() function and displayed the content of the base object. How ever the statements

dptr->show();
((DC *)bptr)->show(); // cast bptr to DC type
Display the contents of the derived object. This shows that, although a base pointer can be made to point to any number of derived objects, it cannot directly access the members defined by a derived class.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise