Polymorphism: Using Base Class Pointers to Derived Class Objects
What is it?
Polymorphism in C++ is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation. In particular, polymorphism is made possible in C++ with the use of base class pointers or references to derived class objects. The same base class pointer can point to objects of any derived class, enabling the execution of derived class methods through a base pointer.
Syntax
The syntax of using base class pointers to derived class objects is:
BaseClass *base_pointer = &derived_object;
Where,
BaseClass
: This is the name of the base class.base_pointer
: This is a pointer of the base class type.&derived_object
: This is the address of the derived class object.
How to use it
To use polymorphism with base class pointers to derived class objects, follow the steps:
- Declare a base class with at least one virtual function.
- Define derived classes that override the base class's virtual function.
- Create objects of the derived classes.
- Declare a base class pointer and assign it the address of the derived class object.
- Use the base class pointer to call the overridden function.
Program code snippet example
#include <iostream>
using namespace std;
// Base class
class BaseClass {
public:
virtual void show() {
cout << "Show BaseClass" << endl;
}
};
// Derived class
class DerivedClass: public BaseClass {
public:
void show() {
cout << "Show DerivedClass" << endl;
}
};
int main() {
BaseClass *base_ptr; // base class pointer
DerivedClass derived_obj; // derived class object
base_ptr = &derived_obj; // assign the address of derived object to base pointer
// Call the function show() which is in derived class through base pointer
base_ptr->show();
return 0;
}
Output:
Show DerivedClass
Important to know
- In C++, polymorphism is achieved by using virtual functions. These are functions that can be redefined in derived classes while keeping a base class interface to call the function.
- A base class pointer can point to objects of any classes derived from it. This allows us to use a base class pointer to call methods of any derived class.
- It's important to note that this only works if the methods are declared in the base class and overridden in the derived class.
Best practices
When using polymorphism in C++, keep the following best practices in mind:
- Always declare functions that you plan to override in derived classes as
virtual
in the base class. This will enable you to use a base class pointer to call these functions. - Avoid slicing: When a derived class object is assigned to a base class object, only the base class portion of the derived class object is assigned, and the derived class portion is 'sliced' off. This is usually not what you want, so use pointers or references to avoid slicing.
- Use a base class pointer to call the methods of a derived class when you want to write more generic code that can handle any class derived from the base class.
Instantiate the Boss
object on the heap and create a pointer to the base class, Enemy
, and name the pointer boss
. Then call the attack()
member function.