Site icon 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,

Inheritance

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,

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,

[crayon-673fbfc821d15715988056/]

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,

[crayon-673fbfc821d21713101297/]

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,

[crayon-673fbfc821d22494066880/]

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

[crayon-673fbfc821d23585451793/]

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.

[crayon-673fbfc821d26186616652/]

Get started building powerful apps with C++Builder!

Exit mobile version