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 (methods, functions 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 (attributes, methods) 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,
- 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,
1 2 3 4 5 |
class shape // this will be Base Class { protected: int width, height; }; |
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,
1 2 3 4 5 6 7 8 9 10 11 12 |
class shape // this is a Base Class for all shapes { protected: int width, height; public: void set_dimension(float w, float h) { width = w; height = h; } }; |
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,
1 2 3 |
class derived_class: public base_class { }; |
As same in that form, we can define a new rectangle class (Derived Class) including our main shape class (Base Class) as below,
1 2 3 4 5 6 7 8 |
class rectangle: public shape { public: float area() { return width*height; // A = a*b } }; |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <iostream> using namespace std; class shape { protected: int width, height; public: void set_dimension(float w, float h) { width = w; height = h; } }; class rectangle: public shape { public: float area() { return width*height; // A = a*b } }; int main () { rectangle rect; // Derived Class rect.set_dimension(5.0, 8.0); // Inherited method from Base Class cout << "Area of Rectangle:" << rect.area() << '\n'; // Method used from the Derived Class getchar(); return 0; } |