Different ways of inheritance in C++

Different ways of inheritance in C++

Inheritance is an important concept in object-oriented programming that allows a class (called a derived class or subclass) to inherit properties and behavior from another class (called a base class or parent class). C++ provides different ways to implement inheritance, including public inheritance, private inheritance, and protected inheritance. These methods are described below, along with related source code examples.

  1. Public inheritance:
    Public inheritance is the most common way of inheritance, through which derived classes can inherit the public and protected members of the base class, but cannot inherit the private members of the base class. In public inheritance, public members of the base class remain public in the derived class, and protected members become protected in the derived class.
#include <iostream>

// 基类
class Base {
   
    
    
public:
    void publicMethod

Guess you like

Origin blog.csdn.net/wellcoder/article/details/132374596