The relationship between inheritance mode and access qualifier
Three ways of inheriting:
Public inheritance
Private inheritance
Protect inheritance
Relationship: The member function of the destructors class is private, and the derived class is not visible to the private members of the base class, and the other criteria are small in scope for the final access qualification.
Protect Member Qualifiers: Some base class members do not want to be accessed directly by the base class's objects, but need to be accessible in a derived class, and are defined as protected members. The protection member qualifier occurs because of inheritance.
Understanding hidden
Shadowing refers to a function of a derived class that masks a base class function with the same name. The rules are as follows:
If a function of a derived class has the same name as a function of the base class, but the parameters are different, the function of the base class is hidden, regardless of the virtual keyword.
If a function of a derived class has the same name as a function of the base class, and the parameters are the same, but the base class function does not have the virtual keyword, the function of the base class is hidden.
Default member functions for derived classes
In a derived class, if no six default member functions are displayed, the compilation system will default to the six member functions.
constructor function
Copy constructor
Destructors
Assignment operator overloading
Fetch address operator overload
Const-Modified FETCH address operator overloading
#include <iostream>using namespace Std;class person{Public:person (const char* name): _name (name) { cout<< "person ()" <<endl; } person (const person& p) {cout<< ' person (const person& p) ' <<endl; } person& operator= (const person& p) {cout<< "person& operator= (const person& p)" <<endl ; if (this!=&p) {_name=p._name; } return *this; } ~person () {cout<< "~person ()" <<endl; } protected:string _name;}; Class Student:public person{public:student (const char* name,int num):p erson (name), _num (num) {CO ut<< "Student ()" <<endl; } student (const student& s):p Erson (s), _num (s._num) {cout<< ' student (const student& s ) "<<endl; } student& operator= (const student& s) {cout<< "student& operator= (const student& p)" << Endl if (This!=& s) {person::operator= (s);//must specify domain, otherwise it will die loop _num=s._num; } return *this; } ~student () {//Refactor the subclass first, then clean up the parent class, and do not need to display the call cout<< "~student ()" <<endl; } private:int _num;}; int main () {student S1 ("Jack", 18); Student S2 (S1); Student S3 ("Rose", 16); S1=S3;}
Diamond Inheritance
There is ambiguity in diamond inheritance and the problem of data redundancy.
example, the data in the inheritance is two copies, each of which is different:
Workaround: Virtual inheritance
Add virtual to the second layer of the diamond
Cases:
Related articles:
C # Supplements's trivia (iv): inheritance
Basic knowledge of C #: Basic Knowledge (2) class
Related videos:
Thousand Front C language Basic video tutorial