I. Public (pulic) inheritance
"Inheritance" is an important feature of a class. Class A inherits Class B, which we call the Class B as the "base class", also called the "parent class", and the Class A is referred to as the "derived class" or "subclass". After Class A inherits Class B, Class A has some Members of Class B. So, what are the members from the base class, which is determined by the 2, ① inheritance, ② the access rights of the base class members (that is, public/private/protected).
There are three ways to inherit, that is, publicly (public) inheritance, private inheritance, and protection (Protected) inheritance. Let's first discuss the most common public inheritance. A public inheritance is a public member of a base class that becomes its own public member, and the protected member of the base class becomes its own protected member.
#include <iostream>
#include<string>using namespacestd;classCBase {stringname; intAge ; Public: stringGetName () {returnname; } intGetage () {returnAge ; }protected: voidSetName (strings) {name=s; } voidSetage (inti) { age=i; }};classCderive: PublicCBase {//To specify public inheritance with "publicly" Public: voidSetBase (stringSinti) {setName (s); //calling a protected member of a base classSetage (i);//calling a protected member of a base class//calling a private member of the base class//cout << name << "<< age << Endl; //compilation Error }};intMain () {cderive D; D.setbase ("ABC", -); //calling a private member of the base class//cout << d.name << "<< d.age << Endl; //compilation Error//calling the public member of the base classcout << d.getname () <<" "<< d.getage () <<Endl; //calling a protected member of a base class//d.setname ("xyz"); //compilation Error//D.setage (20); //compilation Error return 0;}
As can be seen from the above example, for public inheritance, the members of the base class can access the following characteristics:
- Private members of the base class: not accessible in both derived and external classes.
- A protected member of a base class: it can be accessed in a derived class and cannot be accessed externally.
- Public members of the base class: accessible from both derived and external classes.
Ii. Private (privately) inheritance
Private inheritance is the private member of the base class, and the private member of the base class cannot be accessed by itself in the derived class.
#include <iostream> #include <string>using namespace Std;class CBase {string name; int age;public:string GetName () {return name; } int Getage () {return age; }protected:void SetName (string s) {name = S; } void Setage (int i) {age = i; }};class cderive:private CBase {//Specify private inheritance with "private", private can omit Public:void setbase (string s, int i) {SETN Ame (s); Call the protected member of the base class Setage (i); Call a protected member of the base class//Call a private member of the base class//cout << name << "<< age << Endl; Compilation Error} string Getbasename () {return getName (); Call the base class's public member} int getbaseage () {return getage (); Call the public member of the base class}};int main () {cderive D; D.setbase ("abc", 100); Call the private member of the base class//cout << d.name << "<< d.age << Endl; Compile error//Call public member of base class//cout << D.getname () << "" << d.getage () << Endl; Compilation Error cout ≪< d.getbasename () << "" << d.getbaseage () << Endl; Call the protected member of the base class//d.setname ("xyz"); Compilation error//d.setage (20); Compile error return 0;}
As can be seen from the above example, for private inheritance, the members of the base class can access the following characteristics:
- Private members of the base class: not accessible in both derived and external classes.
- The public member of the base class: can be accessed in a derived class and cannot be accessed externally.
- A protected member of a base class: it can be accessed in a derived class and cannot be accessed externally.
Iii. Protection (Protected) inheritance
Protecting inheritance is to turn the public and protected members of the base class into their own protected members, and the private members of the base class cannot be accessed by themselves in the derived class.
#include <iostream> #include <string>using namespace Std;class CBase {string name; int age;public:string GetName () {return name; } int Getage () {return age; }protected:void SetName (string s) {name = S; } void Setage (int i) {age = i; }};class cderive:protected CBase {//Specify private inheritance Public:void setbase (string s, int i) with "private" {setName (s); Call the protected member of the base class Setage (i); Call a protected member of the base class//Call a private member of the base class//cout << name << "<< age << Endl; Compilation Error} string Getbasename () {return getName (); Call the base class's public member} int getbaseage () {return getage (); Call the public member of the base class}};int main () {cderive D; D.setbase ("abc", 100); Call the private member of the base class//cout << d.name << "<< d.age << Endl; Compile error//Call public member of base class//cout << D.getname () << "" << d.getage () << Endl; Compilation Error cout << D.GEtbasename () << "" << d.getbaseage () << Endl; Call the protected member of the base class//d.setname ("xyz"); Compilation error//d.setage (20); Compile error return 0;}
As can be seen from the above example, for private inheritance, the members of the base class can access the following characteristics:
- Private members of the base class: not accessible in both derived and external classes.
- The public member of the base class: can be accessed in a derived class and cannot be accessed externally.
- A protected member of a base class: it can be accessed in a derived class and cannot be accessed externally.
Comparison of four or three kinds of inheritance methods
From the above results, private inheritance and protection inheritance are exactly the same. There is still a difference, but the difference is that if the derived class again derives another class, the derived class will not get any members for the private inheritance just now. As for the protection inheritance, we can still get the public and protected members of the base class.
Concept: Class inheritance-public inheritance, private inheritance, protection inheritance