Inheritance is a procedural process in which the scope of the parent class is greater, the scope of the subclass is smaller, and the parent class is a broader concept, and the subclass is a more specific concept.
Note: There are three ways to inherit, public, protected, private.
The way public is inherited, what the member's access to the parent class is, and what it's like in the subclass,
1 classBase2 {3 Public:4 intX//Public in base, in derived class or public5 protected:6 intY//In base is protected, in the derived class or protected7 Private:8 intZ//In base is private, in the derived class or private9 };Ten One classDerived: PublicBase A { - - the};
Inheritance of protected, public and protected in the base class, inherited into subclasses protected, private members inheriting into subclasses or private
1 1 classBase2 2 {3 3 Public:4 4 intX//is public in base, or protected in the derived class5 5 protected:6 6 intY//In base is protected, in the derived class or protected7 7 Private:8 8 intZ//In base is private, in the derived class or private9 9 };Ten Ten One One classDerived:protectedBase A A { - - - - the the};
Private inheritance, which is private in derived classes, regardless of the access rights of the base class members
1 classBase2 { 3 Public: 4 intX//is public in base, or private in the derived class 5 protected: 6 intY//In base is protected, in the derived class or private 7 Private: 8 intZ//In base is private, in the derived class or private 9 };Ten One classDerived: PublicBase A { - - the};
Class interface: A common (public) member function in a class called an interface
In public inheritance, the public member function of a base class is inherited to a derived class, or to a common one, in other words, the interface of the base class is inherited to the derived class and becomes the interface of the derived class, and so the common inheritance is also known as interface inheritance.
In non- public inheritance, the interface of the base class is inherited to the derived class, but the access permission in the derived class is either protected, or private, which is no longer the interface of the class, and the derived class simply wants to reuse the implementation of the base class, so non-public inheritance is called implementation inheritance.
In inheritance, a hidden, or a function in a subclass, hides a function with the same name in the base class, but notice that a function of the same name is hidden from the base class in the child class. When a function in a base class is not a virtual function, you can hide a function in the base class by modifying the parameter in the subclass with a function of the same name, or by returning a value, but the function in the base class is a virtual function, you cannot just modify the return value and modify the parameter.
#include <iostream>#include<string>using namespacestd;classbase{ Public: intm_x; voidPrint ()Const{cout<<"base::m_x:"<< m_x <<Endl; }};classDerived: Publicbase{ Public: intm_x; voidPrintint)Const{cout<<"derived::m_x:"<< m_x <<Endl; } intPrintint,int) { } intPrint ()Const//It is important to note here that the return value in the base class is void, and that the function in the subclass only changes the return value of the base class, because void print () const in the base class; The function is not a virtual function, so there is no error if the void print () in the base class The const function is a virtual function, and this will cause an error. Of course, if you do not want the program error, the parameters are also modified { }};intMain () {Derived D; intA=0; D.m_x=Ten; //D.print (); //error: This will cause errors, Derived::p rint () function does not accept 0 parameters//Note that the print (int) function in the derived class hides the print () from the base class in the accumulation. D.print (a); D.base::m_x= -;//because the subclass contains member variables and member functions with the same name as the base class, the member variables and member functions in the base class are hidden.D.base::p rint ();//However, member variables and member functions in the base class are also inherited into subclasses, so special method functions can access the base class's//member variables and member functions return 0;}
Inheritance (i)