Pointer to the Base Class in C++

Hello, Coders! In this tutorial, we will discuss the concept of the base class pointer in C++ and its implementation in the program.

Before going to the main topic let’s cover some topics quickly,

  • Pointer
  • Base class and Derived class

 

What is a pointer?

A pointer is a type of variable or a memory location that contain the address of other variables or object.

For example,

int var = 1; //variable declaration 
int *ptr;   //Declaration of pointer variable

ptr = &var;	//Store the address of the variable to the pointer

cout << ptr << endl; //Print the address of the variable var

cout << *ptr; //Print the value of the variable var

Output:

0x7ffe6e3d237c
1

What is Base class and Derived Class?

InĀ  Object Oriented Programming language the base class is defined as a class whose data members and functionality can be inherited by other classes.

On the other hands a derived class is defined as a class that inherits all the properties from an existing or base class.

For example,

class Base {
    // Data members
    // Member functions
}
class Derive : public Base {

    //Data members
    // Member function
   //It can use the Data members 
   //and Member Funtion of the Base calss
}

Pointer to Base Class in C++

We will use the pointer to access the data member and the member function of the base class as well as the derived class.

Step1: Declare the Base class:

class Base {
    public:
        int base_data_member;
        
        //Display function to print the base class members.
        void show()
        {
            cout << "Base Class:" << endl <<
                 << "Data members of Base Class: " << base_data_member << endl;
        }
};

Step2: Declare the Derived class:

class Derived : public Base {
     public:
        int derived_data_member;
        
        //Display function to print the derived and 
        //base class members.
        
        void show()
        {
            cout << "Derived Class:" << endl 
                 << "Data members of Derived Class: " << derived_data_member << endl;
            cout << "Base Class:" << endl
                 << "Data members of Base Class: " << base_data_member << endl;
        }
};

Step3:

Create a pointer to the base class and access the member function of that base class:

Base* bPointer; 
Base bObject; //Creating a object of the Base class

bPointer = &bObject; //pointing to the base class object

bPointer->base_data_member = 100; 

bPointer->show(); //callling to the base class member function

Output:

Base Class:
Data member of Base Class: 100

Create a pointer to the derived class to access the data members and member function of the derived class and the base class:

Derived* dPointer; 
Derived dObject; //Creating a object of the Derived class

dPointer = &dObject;

dPointer->derived_data_member = 99;
dPointer->base_data_member = 1;
dPointer->show();

Output:

Derived class:
Data member of Derived Class: 99
Base Class:
Data member of Base Class: 1

Here is the complete program,

#include <iostream>

using namespace std;

class Base {
  public:
    int base_data_member;

  //Display function to print the base class members.
  void show() {
    cout << "Base Class:" << endl <<
      "Data members of Base Class: " << base_data_member << endl;
  }
};

class Derived: public Base {
  public: int derived_data_member;

  //Display function to print the derived and 
  //base class members.

  void show() {
    cout << "Derived Class:" << endl <<
      "Data members of Derived Class: " << derived_data_member << endl;
    cout << "Base Class:" << endl <<
      "Data members of Base Class: " << base_data_member << endl;
  }
};
int main() {

  Base * bPointer;
  Base bObject; //Creating a object of the Base class
  bPointer = & bObject; //pointing to the base class object
  bPointer -> base_data_member = 100;
  bPointer -> show(); //callling to the base class member function

  Derived * dPointer;
  Derived dObject; //Creating a object of the Derived class
  dPointer = & dObject;
  dPointer -> derived_data_member = 99;
  dPointer -> base_data_member = 1;
  dPointer -> show();

  return 0;
}

Output:

Base Class:
Data members of Base Class: 100
Derived Class:
Data members of Derived Class: 99
Base Class:
Data members of Base Class: 1

Hope this article has helped you to understand the concept of pointers and the use of the pointers to the base class and the derived class.

Keep coding!!!

You can also read, Inheritance in C++

Leave a Reply

Your email address will not be published. Required fields are marked *