C++C++11C++14C++17Introduction to C++Learn C++

Learn about Polymorphism :: Pointers to Base Classes in C++

Generally we try to prepare our posts for a first time C++ readers or we assume reader read some posts about that topic before. At least we try to simplify and clarify the post as much as possible. This post requires a good knowledge about Functions, Classes, Objects and Pointers. These topics are the main stones of C++. So If you feel hard to understand all below, or if you are not really sure of the meaning of any of the following expressions below this is very normal. Please read our previous posts about Functions, Classes, Objects and Pointers. Reading also is not enough, try to run some codes make your own specific codes for specific variables, functions. So you may be ready to understand all below.

Let’s remember that, Object Oriented Programming is a way to integrate with objects which can contain data in the form (attributes or properties of objects), and code blocks in the form of procedures (methodsfunctions of objects). These attributes and methods are variables and functions that belong to the class, they are generally referred to as class members. In C++, classes have members (attributesmethods) and we should protect each member inside this class.

One of the main features of class inheritance is that a pointer to a derived class is type-compatible with a pointer to its base class. It is the best way to do this professionally with Polymorphism which is a powerful and versatile feature of C++ programming language.

Polymorphism in C++ uses inherit attributes and methods from another class methods to perform different tasks. Polymorphism means multiple forms, and this method is used when we have many classes that are related to each other by inheritance. We explained Inheritance in C++ posts before.

For example, a human at the same time can have different skills. For example a worker is a man at the same time is a father, a husband, an employee. So the same person posses different behavior in different situations. This is called polymorphism. Polymorphism is considered as one of the important features of Object Oriented Programming.

Base Classes, Derived Classes and Pointers to Base Class

The Base Class, also known as the Parent Class or the Super Class is a class, from which other classes are derived. In other term it is a base class for other derived classes. That means if a derived class which inherits the base class has all members of a base class as well as can also have some additional properties. The Base class members and member functions are inherited to object of the derived class.

The Derived Class, also known as Child Class or SubClass, is a class that is created from an existing class. The derived class inherits all members and member functions of a base class. The derived class can have more functionality with respect to the Base class and can easily access the Base class.

Let’s explain this with an example, here we will create a 2D shape class that holds protected width and height dimensions of any shape, so this class and these protected parameters can be defined as below,

Now, let’s extend this with a new set_dimension() method which sets width and height of any shape. We should modify this shape class as below,

In C++, we can define Derived Class with a Base Class. To do this we should use : after the name of class and we should add The type of inheritance is specified by the access-specifier and the name of base class. In general, we can define a public derived class as below,

As same in that form, we can define a new rectangle and ellipse classes (Derived Class) including our main shape class (Base Class) as below,

If you want you can add more shapes too. Now let’s see how we use all these, Now let’s combine all together and use them in a main form.


In C++ polymorphism is mainly divided into two types:

  • Compile time Polymorphism
  • Runtime Polymorphism

Get started building powerful apps with C++Builder!

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?