The difference between C++ public inheritance, protected inheritance, and private inheritance

1. Public inheritance-public

During public inheritance, the access attributes to the public members and protected members of the base class remain unchanged. New members of the derived class can access the public members and protected members of the base class, but cannot access the private members of the base class. Objects of a derived class can only access public members of the derived class (including inherited public members), but cannot access protected and private members.

#include <iostream>
using namespace std;

class Base         
{
public: 
    Base(int nId) {mId = nId;}
    int GetId() {mId++;cout<< mId<<endl;return mId;}
protected:
    int GetNum() {cout<< 0 <<endl;return 0;}
private: 
    int mId; 
};

class Child : public Base
{
public:
    Child() : Base(7) {;}
    int GetCId() {
   
   return GetId();}    //新增成员可以访问公有成员
    int GetCNum() {
   
   return GetNum();}  //新增成员可以访问保护成员
                                      //无法访问基类的私有成员
protected:
    int y;
private:
    int x;
};

int main() 
{ 
    Child child;
    child.GetId();        //派生类的对象可以访问派生类继承下来的公有成员
    //child.GetNum();     //无法访问继承下来的保护成员GetNum()
    child.GetCId();   
    child.GetCNum();      //派生类对象可以访问派生类的公有成员
    //child.x;
    //child.y;            //无法访问派生类的保护成员y和私有成员x
    return 0;
}

2. Protected inheritance-protected

In protected inheritance, the public members and protected members of the base class become protected members after being inherited by the derived class. New members of the derived class can access the public members and protected members of the base class, but cannot access the private members of the base class. Objects of the derived class cannot access the public members of the derived class inherited from the base class, protected members and private members.

class Child : protected Base
{
public:
    Child() : Base(7) {;}
    int GetCId() {
   
   return GetId();}   //可以访问基类的公有成员和保护成员
    int GetCNum() {
   
   return GetNum();}
protected:
    int y;
private:
    int x;
};

int main() 
{ 
    Child child;
    //child.GetId();//派生类对象访问不了继承的公有成员,因为此时保护继承时GetId()已经为          protected类型
    //child.GetNum(); //这个也访问不了
    child.GetCId();
    child.GetCNum();
    return 0;
}

3. Private inheritance-private

In private inheritance, the public members and protected members of the base class become private members after being inherited by the derived class. New members of the derived class can access the public members and protected members of the base class, but cannot access the private members of the base class. Objects of the derived class cannot access the public members of the derived class inherited from the base class, protected members and private members.

class Child : private Base
{
public:
    Child() : Base(7) {;}
    int GetCId() {
   
   return GetId();}   //可以访问基类的公有成员和保护成员
    int GetCNum() {
   
   return GetNum();}
protected:
    int y;
private:
    int x;
};

int main() 
{ 
    Child child;
    //child.GetId();//派生类对象访问不了继承的公有成员,因为此时私有继承时GetId()已经为          private类型
    //child.GetNum(); //派生类对象访问不了继承的保护成员,而且此时私有继承时GetNum()已经为          private类型
    child.GetCId();
    child.GetCNum();
    return 0;
}

4. Summary

Regardless of the inheritance method, new members in the derived class can access the public and protected members of the base class, but cannot access the private members. The way of inheritance affects the access properties of the derived class inherited members, and the use of friends (friend) can access protected members and private members.
Access permissions for derived objects
Write picture description here

Guess you like

Origin blog.csdn.net/sxtdzj/article/details/81906504