C++ Learning Manual – Object-Oriented Programming (OOP) 32 – Inheritance (class Derived : public Base)

1. The Concept and Function of Inheritance

Inheritance is one of the three main features of object-oriented programming (encapsulation, inheritance, polymorphism). It allows us to create new classes based on existing classes, achieving code reuse and extension. Through inheritance, derived classes can automatically acquire the properties and methods of the base class while adding new functionalities or modifying existing behaviors.

2. Basic Syntax of Inheritance

The basic syntax of inheritance in C++ is as follows:

class DerivedClassName : AccessModifier BaseClassName {
    // Members and methods added by the derived class
};

Here, the access modifier can be public, protected, or private, determining the visibility of the base class members in the derived class.

3. Detailed Explanation of Three Inheritance Types

Public Inheritance (Most Common)
  • The public members of the base class remain public in the derived class.
  • The protected members of the base class remain protected in the derived class.
  • The private members of the base class cannot be accessed directly.
Protected Inheritance
  • The public and protected members of the base class become protected in the derived class.
  • This is suitable for situations where the exposure of the base class interface needs to be restricted.
Private Inheritance
  • The public and protected members of the base class become private in the derived class.
  • This is commonly used to implement inheritance rather than interface inheritance.

4. Order of Constructors and Destructors

When creating a derived class object:

  1. The base class constructor is called first.
  2. Then the derived class constructor is called.

The order is reversed when the object is destroyed:

  1. The derived class destructor is called first.
  2. Then the base class destructor is called.

5. Method Overriding and the Override Keyword

The derived class can override the member functions of the base class to achieve polymorphic behavior. The override keyword introduced in C++11 can explicitly indicate that this is an override of a base class method, helping the compiler check for correct overriding.

6. Multiple Inheritance and Virtual Inheritance

C++ supports a class inheriting from multiple base classes, which provides greater flexibility but may also lead to the “diamond inheritance” problem. Virtual inheritance can solve this problem, ensuring that the common base class is inherited only once.

7. Recommendations for Using Inheritance

  • Prefer public inheritance to implement “is-a” relationships.
  • Use multiple inheritance cautiously.
  • Use protected and private inheritance judiciously.
  • Consider using the final keyword to prevent a class from being inherited.

The inheritance mechanism greatly enhances C++’s object-oriented capabilities. When used correctly, it can build a clear and maintainable class hierarchy. Understanding the characteristics of various inheritance types, mastering the order of constructors/destructors, and effectively using method overriding can fully leverage the advantages of inheritance.

Java Learning Materials Available

C Language Learning Materials Available

Frontend Learning Materials Available

C++ Learning Materials Available

PHP Learning Materials Available

Leave a Comment