C++ basic syntax: common inheritance
- 2020-04-02 01:44:45
- OfStack
1. First, let's talk about the three properties of the member function or member variable of the class:
Public:
Both objects and member functions are visible, that is, can be referenced
Protected:
Visible to member functions but invisible to objects, this declaration is mainly used in inheritance, as discussed below
Private:
Visible to member functions, invisible to objects
2. Then look at inheritance:
Base class member keyword
Derived classes inherit
A derived class has access to a base class member
public
public
This is equivalent to using the public keyword
protected
The procted keyword is used
private
The equivalent of using the private keyword
protected
protected
The procted keyword is used
protected
The procted keyword is used
private
The equivalent of using the private keyword
private
public
The equivalent of using the private keyword
protected
The equivalent of using the private keyword
private
The equivalent of using the private keyword
A few things to note:
Private members of base class members are invisible not only to objects, but also to derived classes, and can only be accessed by base class members or friends; Procted members are actually private to an object, but unlike private when inheriting, derived class members can access procted members of the base class; Protected and private inheritance are rarely used in actual programming and are mainly used in theoretical analysis.
PS: The object in question is actually a variable declared by the class, and the class defined inside the class has access to private members of the external class. And the variables defined inside the class can also access the private members of the class;