Base Class Pointer Pointing to Derived Class Object in C++
Last Updated :
20 Mar, 2023
Prerequisite: Pointers in C++
A pointer is a data type that stores the address of other data types. Pointers can be used for base objects as well as objects of derived classes. A pointer to the object of the derived class and a pointer to the object of the base class are type-compatible (may be used in different ways).
The pointer of Base Class pointing different objects of the derived class
Base class pointer to Derived class object Approach:
- A derived class is a class that takes some properties from its base class.
- It is true that a pointer of one class can point to another class, but classes must be a base and derived class, then it is possible.
- To access the variable of the base class, a base class pointer will be used.
- So, a pointer is a type of base class, and it can access all, public function and variables of the base class since the pointer is of the base class, this is known as a binding pointer.
- In this pointer base class is owned by the base class but points to the derived class object.
- The same works with derived class pointer, values are changed.
Example:
C++
// C++ program to Demonstrate the
// implementation of the base class
// pointer pointing to derived class
#include <iostream>
using namespace std;
// Base Class
class BaseClass {
public:
int var_base;
// Function to display the base
// class members
void display()
{
cout << "Displaying Base class"
<< " variable var_base: " << var_base << endl;
}
};
// Class derived from the Base Class
class DerivedClass : public BaseClass {
public:
int var_derived;
// Function to display the base
// and derived class members
void display()
{
cout << "Displaying Base class"
<< "variable var_base: " << var_base << endl;
cout << "Displaying Derived "
<< " class variable var_derived: "
<< var_derived << endl;
}
};
// Driver Code
int main()
{
// Pointer to base class
BaseClass* base_class_pointer;
BaseClass obj_base;
DerivedClass obj_derived;
// Pointing to derived class
base_class_pointer = &obj_derived;
base_class_pointer->var_base = 34;
// If you uncomment this line of code this will cause
// the following error As base-class pointer cannot
// access the derived class variable.Â
// base_class_pointer->var_derived = 98;
// output: error: ‘class BaseClass’ has no member named
// ‘var_derived’
// Calling base class member function
base_class_pointer->display();
base_class_pointer->var_base = 3400;
base_class_pointer->display();
DerivedClass* derived_class_pointer;
derived_class_pointer = &obj_derived;
derived_class_pointer->var_base = 9448;
derived_class_pointer->var_derived = 98;
derived_class_pointer->display();
return 0;
}
OutputDisplaying Base class variable var_base: 34
Displaying Base class variable var_base: 3400
Displaying Base classvariable var_base: 9448
Displaying Derived class variable var_derived: 98
Time Complexity: O(1).
Space Complexity: O(1).
How is Upcasting related to Base class pointer pointing to derived class object?
A derived-class reference or pointer is Upcast to a base class. To put it another way, upcasting enables us to handle derived types as if they were their base types.
Without an explicit typecast, public inheritance is always permitted. This is a result of the base and derived classes having an is-a relationship.
Although C++ permits a base pointer to point to any object derived from that 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 a pointer to the derived type.
Conclusion:
- A pointer to a derived class is a pointer of a base class pointing to a derived class, but it will hold its aspect.
- This pointer of the base class will be able to temper functions and variables of its own class and can still point to the derived class object.
Similar Reads
How to Override a Base Class Method in a Derived Class in C++? Function overriding is a concept in object-oriented programming languages, in which a function/method of a parent class is redefined in its child class to change its behavior for the objects of the child class. In this article, we are going to learn how to override a base class function in a derived
2 min read
How to Create a Derived Class from a Base Class in C++? In C++, one of the fundamental concepts of Object Oriented Programming (OOPS) is inheritance, allowing users to create new classes based on existing classes. The class that inherits another class is called derived class and the class that is inherited is called base class. In this article, we will l
2 min read
How to Implement a Copy Constructor in a Derived Class in C++ In object-oriented programming, a copy constructor is a special member function that initializes a new object as a copy of an existing object. In this article, we will learn how to implement a copy constructor in a derived class. Implementing Copy Constructor in a Derived Class in C++ In C++, when w
3 min read
How to Call a Virtual Function From a Derived Class in C++? In C++, virtual functions play an important role because they allow the users to perform run-time polymorphism. While dealing with inheritance and virtual functions, it is very crucial to understand how to call a virtual function from a derived class. In this article, we will learn how to call a vir
2 min read
How to convert a class to another class type in C++? Pre-requisite: Type Conversion in C++, Advanced C++ | Conversion OperatorsThrough class conversion, one can assign data that belongs to a particular class type to an object that belongs to another class type. Example: Let there be two classes 'A' and 'B'. If we want to allocate the details that belo
5 min read