Concept: Class inheritance-public inheritance, private inheritance, protection inheritance

Source: Internet
Author: User

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:

    1. Private members of the base class: not accessible in both derived and external classes.
    2. A protected member of a base class: it can be accessed in a derived class and cannot be accessed externally.
    3. 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 &Lt;< 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:

    1. Private members of the base class: not accessible in both derived and external classes.
    2. The public member of the base class: can be accessed in a derived class and cannot be accessed externally.
    3. 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:

    1. Private members of the base class: not accessible in both derived and external classes.
    2. The public member of the base class: can be accessed in a derived class and cannot be accessed externally.
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.