One of the most important concepts in object-oriented programming is inheritance. Inheritance allows us to define a class based on another class, which makes it easier to create and maintain an application. This also achieves the effect of reusing code functionality and improving execution time.
When you create a class, you do not need to rewrite the new data member and member functions, just specify that the newly created class inherits the members of an existing class. This existing class is called the base class , and the new class is called a derived class .
Inheritance represents the is a relationship.
Base class & derived class
A class can derive from more than one class, which means that it can inherit data and functions from multiple base classes. Define a derived class, and we use a class-derived list to specify the base class. The class-derived list is named after one or more base classes in the following form:
Class Derived-class:access-specifier Base-class
Where the access modifier access-specifier is one of public , protected , or private , Base-class is the name of a class that was previously defined. If the access modifier access-specifier is not used, the default is private.
Access control and inheritance
Derived classes can access all non-private members in the base class. Therefore, if the base class member does not want to be accessed by a member function of the derived class, it should be declared as private in the base class.
We can summarize different types of access based on access rights, as follows:
Access |
| Public
protected |
Private |
The same class |
Yes |
Yes |
Yes |
Derived classes |
Yes |
Yes |
No |
External classes |
Yes |
No |
No |
A derived class inherits all of the base class methods, with the exception of the following:
- Constructor, destructor, and copy constructors for the base class.
- The overloaded operator of the base class.
- The friend function of the base class.
Inheritance type
When a class derives from a base class, the base class can be inherited as public , protected, or private in several types. The inheritance type is specified by the access modifier Access-specifier explained above.
We rarely use protected or private inheritance, usually using public inheritance. When different types of inheritance are used, the following rules are followed:
- Public Inheritance: When a class derives from a public base class, the public member of the base class is also the public member of the derived class, and the protected member of the base class is also the protection of the derived class member, a private member of a base class cannot be accessed directly by a derived class, but can be accessed by invoking the public and protected members of the base class.
- Protection Inheritance (Protected): when a class is derived from a protected base class, the public and protected members of the base class become the protected members of the derived class.
- Private Inheritance: When a class derives from a private base class, the public and protected members of the base class become private members of the derived class.
Multiple inheritance
Multiple inheritance means that a subclass can have more than one parent class, which inherits the attributes of multiple parent classes.
C + + classes can inherit members from multiple classes with the following syntax:
Class< Derived classes name >:< inheritance 1>< base class name 1>,< inheritance mode 2>< base class name 2> ,... {< derived class class body >};
Where the access modifier is inherited by public , protected , or private , which is used to decorate each base class, separated by commas.
In addition multiple inheritance (ring inheritance), A->d, B->d, C-> (A, A, b), for example:
Class D{...}; class B:public D{...}; class A:public D{...}; class C:public B,public A{...};
This inheritance will cause D to create two objects, to solve the above problem will be used in virtual inheritance format
Format: ClassName: Virtual Inheritance Method parent class name
C + + Inheritance