Friday 20 May 2016

Pointing towards the objects of Derived Class using pointer of Base Class in C++

We can use the pointers of base class not only for pointing to the base class objects as usual but we can use the base class pointers for pointing towards the objects of derived class also. As an example, *mptr as a pointer of base class manager which can point towards the data members and member functions of the derived class. Consider the following statements:

Manager *mptr;                     // Pointer to object of Base Class Manager
Manager m1;                         // Base Class Manager’s object
City c1;                                    // Object of derived class City
mptr = &m1;                          // mptr points to object m1 of Base class
                                               // which is the usual behavior in C++

We can directly make mptr to point towards the object of derived class City say c1 as shown below:
                       
mptr = &c1;

This will perfectly work in C++ because c1 is the object of the class ‘City’ which is derived from the base class ‘Manager’.

Howsoever, there remains a problem in using ‘mptr’ to use the public data members and functions of the derived class ‘City’. mptr can use only those members of the derived class which are derived from Base class ‘Manager’ but not the original members of the Derived class ‘City’. If any member of derived class has the same name as that of base class, then any reference to that members will lead to the use of base class member always. Remember, a base pointer cannot use the members of derived class directly. But a base pointer can use the members of derived class by using pointer type casting as shown in the below example:


#include<iostream.h>
#include<conio.h>

class Manager
{
public:
                        int emp_id;
                        char *name;
                        void disp()
                        {
                                    cout<< "Manager's ID = "<<emp_id<<endl;
                                    cout<< "Manager's Name = "<<name<<endl;
                        }
};

class City : public Manager
{
public:
                        int city_code;
                        char *city_name;
                        void disp()
                        {
                              cout<< "Manager's ID = "<<emp_id<<endl;
                              cout<< "Manager's Name = "<<name<<endl;
                              cout<< "City Code = "<<city_code<<endl;
                              cout<< "Manager's City Name = "<<city_name<<endl;
                        }
};

void main()
{
clrscr();
manager *mptr;
manager m1;
mptr =            &m1;                           // Base class pointer pointing to the base class object   

mptr -> emp_id = 101;
mptr -> name = "Amit Arora";

cout<<"Base class pointer pointing towards base class object: \n";
mptr -> disp();                                                           // will call the disp() from the base class

city c1;
mptr =            &c1;                            // Base class pointer pointing to the derived class object

mptr -> emp_id = 201;
mptr -> name = "Amit";
/* mptr -> city_code = 1001;           */                   // will not work
/* mptr -> city_name = "New Delhi"; */      // will not work


cout<<"\nBase class pointer now pointing towards derived class object: \n";
mptr -> disp();                                                           // will call the disp() from the base class


cout<<"\nWe can made base class pointer pointing towards the derived class object by using type casting: \n";
((city *)mptr) -> city_code = 1001;                         /* Type casting */
((city *)mptr) -> city_name = "New Delhi";                        /* Type casting */

((city *)mptr) -> disp();                    // will call the disp() from the derived class

getch();
}

Output:
Base class pointer pointing towards base class object:
Manager’s ID = 101
Manager’s Name = Amit Arora

Base class pointer now pointing towards derived class object:
Manager’s ID = 201
Manager’s Name = Amit

We can made base class pointer pointing towards the derived class object by using type casting:
Manager’s ID = 201
Manager’s Name = Amit
City Code = 1001
Manager’s City Name = New Delhi


Note: Point to note that we have used the statement ‘mptr -> disp();’ twice but both the times only base class version of disp() function get executed which proves that a base class pointer can point to any numbers of derived class objects but it cannot directly use the original members of the derived class. For accessing derived class members we need to use type casting.


Mr. Deepak Sharma
Assistant Professor
Deptt. of Information Technology 

No comments:

Post a Comment