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

Learn C++ Inheritance :: Base Classes and Derived Classes

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. These topics are the main stones of C++ programming. 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. 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 (OOP) 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.

If you are asking what is Inheritance?, How I can define a Base Class? or How I can create Derived Classes with Bases Classes? All the answers are summarized below,


The Inheritance is one of the most important concept in object-oriented C++ programming as in other features of Classes. Inheritance allows us to define a class in terms of another class, and it makes easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. Inheritance implements the relationship between classes. For example, a rectangle is a kind of shape and ellipse is a kind of shape etc.

When creating classes, if we have same members, we can list all these in base classes. Instead of writing completely new data members and member functions, we can designate that the new class should inherit the members of an parent class. This parent class is called as the Base Class, and the new class is called as Derived Class.

Base Classes, Derived Classes

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.

The base class may be inherited through public, protected or private inheritance when deriving a class from a base class. The type of inheritance is specified by the access-specifier as explained before in Access Specifiers in Classes.

Generaly we use public Inheritance in C++, sometimes we need to use protected or private inheritance . We can summarize each of these type of inheritance as below,

  • Public Inheritance : Public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class when deriving a class from a public base class. Private members of a base class can not be accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
  • Private Inheritance : Public and protected members of the base class become private members of the derived class when deriving from a private base class.
  • Protected Inheritance : Public and protected members of the base class become protected members of the derived class when deriving from a protected 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 class (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.

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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Learn C++

Learn about Declaration of a Copy Constructor

C++Game DevelopmentLearn C++

Learn to Develop a Text Based Simple Adventure Game in C++ on Windows

C++C++11C++14C++17Learn C++

Learn what is Trivial Default Constructor in C++

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

Learn to use Deleted Implicitly-Declared Default Constructor