[C++] Inheritance ⑤ (public public inheritance – example analysis | protected protected inheritance – example analysis | private private inheritance – example analysis)

Article directory

  • 1. Public public inheritance – example analysis
    • 1. public public inheritance
    • 2. Code example – public public inheritance
  • 2. protected inheritance – example analysis
    • 1. protected inheritance
    • 2. Code example – protected protected inheritance
  • 3. Private private inheritance – example analysis
    • 1. private private inheritance
    • 2. Code example – private private inheritance

The access attributes of members need to be determined according to the following logic:

  • Call location: See where it is called, inside the class, a derived class (subclass), or outside the class;
  • Subclass inheritance method:
    • Public inheritance: public
    • protected inheritance : protected
    • Private inheritance: private
  • Access level in parent class:
    • public members : public
    • protected member : protected
    • private member : private

For example: When called outside the class, the subclass protection inherits the public members of the parent class. The call fails because the public members of the parent class are inherited by the subclass protection and become protected members of the subclass. They can only be used in It can be called inside a subclass or a grandchild class, but cannot be called outside the class;

Special Note: Private inheritance, the public members and protected members of the base class become private members of the derived class. These two members are accessible inside the derived class but not accessible outside the class. ;

1. public public inheritance – example analysis

1. public public inheritance

public public inheritance: The members of the parent class are in the subclass, access control permissions remain unchanged, shared and protected members can be accessed in subclasses, private members cannot be accessed in subclasses;

The access control permissions in the parent class change as follows:

  • Public members in the parent class are still public members;
  • Protected members in the parent class are still protected members;
  • Private members in the parent class are still private members;

2. Code example – public public inheritance

In the code below,

In the base class (parent class), public variable a, protected variable b, and private variable c are defined;

In the derived class (subclass), the above three variables are accessed respectively;

  • Accessing public variables a: Public inherited base class public variables are still common variables in subclasses. This variable can be accessed. Public members of the parent class can be accessed at any location. Any location here refers to Inside the class, inside the subclass, outside the class;
 // Accessible: public members of the parent class can be accessed anywhere
        // Any location: inside the class, inside the subclass, outside the class
        a = 0;
  • Access protected variable b: Public inheritance base class protected variable is still a protected variable in the subclass. This variable is a protected member in the subclass and can be accessed inside the base class and within the subclass;
 // Accessible: parent class protected members can be accessed inside the class and within the subclass
        b = 0;
  • Access private variables c: Publicly inherited base class private variables are still private variables in the subclass. This variable is a private member in the subclass and can only be accessed in the base class;
 // Error: "Parent::c": Unable to access private member (declared in "Parent" class)
        //Private members can only be accessed within this class, and cannot be accessed outside subclasses and classes.
        //c = 0;

External access to the class 3 variables inherited by the derived class (subclass);

  • Access public variable a: A public member in the base class, which is still a public member in the subclass after public inheritance, and can be accessed outside the class;
 //Public members in the base class
    //Public inheritance is still a public member in the subclass and can be accessed outside the class
    child.a;
  • Access protected variable b: A protected member in the base class, public inheritance, still a protected member in the subclass, and cannot be accessed outside the class;
 // Inaccessible: protected member in base class
    // Public inheritance is still a protected member in the subclass and cannot be accessed outside the class
    //child.b;
  • Access private variables c: Private members in the base class, public inheritance, still private members in the subclass, cannot be accessed outside the class;
 // Inaccessible: private member in base class
    //Public inheritance is still a private member in the subclass and cannot be accessed outside the class
    //child.c;

Code example:

#include "iostream"
using namespace std;

class Parent {<!-- -->
public:
    int a;

protected:
    int b;

private:
    int c;
};

// Subclass public inheritance parent class
class Child : public Parent {<!-- -->
public:
    void changeVar() {<!-- -->
        // Accessible: public members of the parent class can be accessed anywhere
        // Any location: inside the class, inside the subclass, outside the class
        a = 0;

        // Accessible: parent class protected members can be accessed within the class and within the subclass
        b = 0;

        // Error: "Parent::c": Unable to access private member (declared in "Parent" class)
        //Private members can only be accessed within this class, and cannot be accessed outside subclasses and classes.
        //c = 0;
    }
};

int main() {<!-- -->

    Child child;
    child.changeVar();

    // Accessible: public members in the base class
    //Public inheritance is still a public member in the subclass and can be accessed outside the class
    child.a;

    // Inaccessible: protected member in base class
    // Public inheritance is still a protected member in the subclass and cannot be accessed outside the class
    //child.b;

    // Inaccessible: private member in base class
    //Public inheritance is still a private member in the subclass and cannot be accessed outside the class
    //child.c;
    

// The console pauses, press any key to continue execution backwards
system("pause");

return 0;
}

2. protected protected inheritance – example analysis

1. protected inheritance

protected protected inheritance: The parent class members are in the subclass, access control permissions become protected, Public members and protected members of the base class can be accessed in the subclass, private members cannot be accessed in the subclass;

The access control permissions in the parent class change as follows:

  • Public members in the parent class become protected members in the subclass;
  • Protected members in the parent class are still protected members;
  • Private members in the parent class are still private members;

2. Code example – protected protected inheritance

In the code below,

In the base class (parent class), public variable a, protected variable b, and private variable c are defined;

In the derived class (subclass), the above three variables are accessed respectively;

  • Access public variable a: Protect the public variable of the inherited base class to become a protected variable in the subclass, and the variable can be accessed in the derived class;
 // Accessible: public members of the parent class become protected members
        // This member can be accessed inside the class and within the subclass
        a = 0;
  • Access protected variable b: Protected inheritance base class protected variable is still a protected variable in the subclass. This variable is a protected member in the subclass and can be accessed inside the base class and within the subclass;
 // Accessible: parent class protected members can be accessed inside the class and within the subclass
        b = 0;
  • Accessing private variables c: Protecting inherited base class private variables is still a private variable in the subclass. This variable is a private member in the subclass and can only be accessed in the base class.
 // Error: "Parent::c": Unable to access private member (declared in "Parent" class)
        //Private members can only be accessed within this class, and cannot be accessed outside subclasses and classes.
        //c = 0;

External access to the class 3 variables inherited by the derived class (subclass);

  • Access public variable a: Public members in the base class, protected inheritance, subclasses become protected members, which can be accessed outside the class;
 // Inaccessible: public members in the base class
    // Protect inheritance. Public members are still public in subclasses and can be accessed outside the class.
    //child.a;
  • Access protected variable b: A protected member in the base class, protected inheritance is still a protected member in the subclass, and cannot be accessed outside the class;
 // Inaccessible: protected member in base class
    //Protect inheritance. Subclasses are still protected members and cannot be accessed outside the class.
    //child.b;
  • Access private variable c: Private members in the base class, protected inheritance, are still private members in the subclass, and cannot be accessed outside the class;
 // Inaccessible: private member in base class
    // Protect inheritance. The subclass is still a private member and cannot be accessed outside the class.
    //child.c;

Code example:

#include "iostream"
using namespace std;

class Parent {<!-- -->
public:
    int a;

protected:
    int b;

private:
    int c;
};

// Subclass protected inheritance parent class
class Child : protected Parent {<!-- -->
public:
    void changeVar() {<!-- -->
        // Accessible: public members of the parent class become protected members
        // This member can be accessed inside the class and within the subclass
        a = 0;

        // Accessible: parent class protected members can be accessed within the class and within the subclass
        b = 0;

        // Error: "Parent::c": Unable to access private member (declared in "Parent" class)
        //Private members can only be accessed within this class, and cannot be accessed outside subclasses and classes.
        //c = 0;
    }
};

int main() {<!-- -->

    Child child;
    child.changeVar();

    // Inaccessible: public members in the base class
    // Protect inheritance. Public members are still public in subclasses and can be accessed outside the class.
    //child.a;

    // Inaccessible: protected member in base class
    //Protect inheritance. Subclasses are still protected members and cannot be accessed outside the class.
    //child.b;

    // Inaccessible: private member in base class
    // Protect inheritance. The subclass is still a private member and cannot be accessed outside the class.
    //child.c;
    

//The console pauses, press any key to continue execution backwards
system("pause");

return 0;
}

3. private private inheritance – example analysis

1. private inheritance

private private inheritance: The parent class members are in the child class, the access control permissions of all members become private, All members of the base class are not accessible in subclasses;

The access control permissions in the parent class change as follows:

  • Public members in the parent class become private members in the subclass;
  • Protected members in the parent class become private members in the subclass;
  • Private members in the parent class are still private members;

2. Code example – private private inheritance

In the code below,

In the base class (parent class), public variable a, protected variable b, and private variable c are defined;

In the derived class (subclass), the above three variables are accessed respectively;

  • Access public variable a: Private inheritance The public variable of the base class becomes a private variable of the subclass in the subclass. It is a public member in the parent class. This variable can be used in the derived class Visited;
 // Accessible: public members of the parent class become private members of the subclass
        // This member can be accessed inside the subclass
        a = 0;
  • Access protected variable b: A privately inherited base class protected variable is still a protected variable in the subclass. The variable is a private member in the subclass and a protected member in the parent class. It can be accessed within the base class and Subclass internal access;
 // Accessible: parent class protected members become subclass private members
        // Subclass private members can be accessed within the subclass
        b = 0;
  • Access private variables c: Private inheritance base class private variables are still private variables in the subclass. This variable is a private member in the subclass and can only be accessed in the base class.
 // Error: "Parent::c": Unable to access private member (declared in "Parent" class)
        //Private members can only be accessed within this class, and cannot be accessed outside subclasses and classes.
        //c = 0;

External access to the class 3 variables inherited by the derived class (subclass);

  • Access public variable a: A public member in the base class, private inheritance in the subclass becomes a private member of the subclass, and cannot be accessed outside the class;
 // Inaccessible: public members in the base class become private members in the derived class
    //Private inheritance: Private members in subclasses cannot be accessed outside the class
    //child.a;
  • Access protected variable b: Protected members in the base class, private inheritance, protected members in the subclass that become subclasses, cannot be accessed outside the class;
 // Inaccessible: protected members in the base class become private members in the derived class
    //Private inheritance: Private members in subclasses cannot be accessed outside the class
    //child.b;
  • Access private variable c: Private members in the base class, private inheritance, are still private members of the parent class in the subclass, and cannot be accessed outside the class;
 // Inaccessible: private member in base class
    // Private inheritance is still a private member in the subclass and cannot be accessed outside the class.
    //child.c;

Code example:

#include "iostream"
using namespace std;

class Parent {<!-- -->
public:
    int a;

protected:
    int b;

private:
    int c;
};

// Subclass private inheritance parent class
class Child : private Parent {<!-- -->
public:
    void changeVar() {<!-- -->
        // Accessible: public members of the parent class become private members of the subclass
        // This member can be accessed inside the subclass
        a = 0;

        // Accessible: parent class protected members become subclass private members
        // Subclass private members can be accessed within the subclass
        b = 0;

        // Error: "Parent::c": Unable to access private member (declared in "Parent" class)
        //Private members can only be accessed within this class, and cannot be accessed outside subclasses and classes.
        //c = 0;
    }
};

int main() {<!-- -->

    Child child;
    child.changeVar();

    // Inaccessible: public members in the base class become private members in the derived class
    //Private inheritance: Private members in subclasses cannot be accessed outside the class
    //child.a;

    // Inaccessible: protected members in the base class become private members in the derived class
    //Private inheritance: Private members in subclasses cannot be accessed outside the class
    //child.b;

    // Inaccessible: private member in base class
    // Private inheritance is still a private member in the subclass and cannot be accessed outside the class.
    //child.c;
    

//The console pauses, press any key to continue execution backwards
system("pause");

return 0;
}