C and C++ comparison of public inheritance protected inheritance and private inheritance

  • 2020-05-17 05:57:56
  • OfStack

C/C++ the difference between public, protected, and private inheritance

In the inheritance control of c++, there are three different control permissions, public, protected, and private. When defining a derived class, if the three keywords are not displayed, the default mode will be used. The class defined by struct is the default public inheritance, and the class defined by class is the default private inheritance. This is quite different from Java, which USES public inheritance by default and has only public inheritance.

1. With public inheritance, members of the base class public and protected can be accessed from within derived classes, but members of the base class public can only be accessed from outside the class by objects of the derived class.

(1) the public members of the base class are still public members in the derived class.

(2) the protected members in the base class are still protected members in the derived class.

(3) the private members in the base class are not accessible in the derived class.

2. With protected inheritance, members of public and protected in the base class can be accessed from within the derived class, and members of the base class cannot be accessed from outside the class through the objects of the derived class (public member function interfaces can be added to the derived class to indirectly access public and protected members in the base class).

(1) the public member of the base class becomes an protected member in the derived class.

(2) the protected member of the base class is still an protected member in the derived class.

(3) the private members in the base class are not accessible in the derived class.

3. With private inheritance, members of public and protected in the base class can be accessed from within the derived class, and members of the base class cannot be accessed from outside the class through the objects of the derived class (public member function interfaces can be added to the derived class to indirectly access public and protected members of the base class).

(1) the public member of the base class becomes an private member in the derived class.

(2) the protected member of the base class becomes an private member in the derived class.

(3) the private members of the base class are not accessible in the derived class.

For the sake of understanding, we use a table to illustrate the use of these controllers:

派 生 方 式  基类的public成员 基类的protected成员 基类的private成员
public派生 还是public成员 变为protected成员 不可见
protected派生 变成protected成员    还是protected成员 不可见
private派生 变为private成员 变成private成员 不可见

The following is a simple description in code


#include <iostream> 

class Base { 
public: 
  int public_a; 
  virtual void test() = 0; 

protected: 
  int protected_a; 

private: 
  int private_a; 
}; 

// Public inheritance  
class PublicDerived : public Base { 
public: 
  virtual void test() { 
    public_a = 1;          //public_a public After inheritance or public type  
    protected_a = 2;        //protected_a  or protected type  
    //private_a = 3;         // Derived classes cannot access private members of the base class  
  } 
}; 

// Protect the inheritance  
class ProtectedDerived : protected Base { 
public: 
  virtual void test() { 
    public_a = 1;          //public_a protected Inherit into protected type  
    protected_a = 2;        //protected_a  or protected type  
    //private_a = 3;        // Derived classes cannot access private members of the base class  
  } 
}; 

// Private inheritance  
class PrivateDerived : private Base { 
public: 
  virtual void test() { 
    public_a = 1;          //public_a private Inherit into private type  
    protected_a = 2;        //protected_a private Inherit into private type  
    //private_a = 3;        // Derived classes cannot access private members of the base class  
  } 
}; 

In c++, public inheritance is the relationship of is-a. That is to say, the 1 that applies to a base class also applies to a derived class, because every derived class object is also a base class object. Derived class objects can be converted to base class objects automatically when needed.

In fact, the derived class that is derived from protected and private is not a subclass of the base class, because this derived class cannot do all the things that the base class can do. Let's look at the code below


#include <iostream> 

class Person { 
public: 
  Person(){}; 

  void eat() {std::cout << "eat\n";} 
}; 

// Public inheritance  
class PublicStudent : public Person { 
public: 
  PublicStudent() {} 

  void study() {std::cout << "study\n";} 
}; 

// Protect the inheritance  
class ProtectedStudent : protected Person { 
public: 
  ProtectedStudent() {} 

  void study() {std::cout << "study\n";} 
}; 

// Private inheritance  
class PrivateStudent : private Person { 
public: 
  PrivateStudent() {} 

  void study() {std::cout << "study\n";} 
}; 

 

void func_test(Person &p) { 
  p.eat(); 
} 

int main() { 

  PublicStudent public_s; 
  ProtectedStudent protected_s; 
  PrivateStudent private_s; 

  func_test(public_s);          // True, derived classes can do everything the base class does when it comes to public inheritance  
  func_test(protected_s);         // Error protecting inheritance when derived classes cannot do everything the base class does  
  func_test(private_s);          // Error. When private inheritance occurs, derived classes cannot do everything the base class does  

  system("pause"); 

  return 0; 
} 

func_test() requires an object of type Person, and when calling func_test(public_s), it actually passes the PublicStudent object. Since PublicStudent shares the Person class, the PublicStudent object can use all the public members of the Person class. Both ProtectedStudent and PrivateStudent are non-public and have inheritance, and their objects cannot directly access members of the Person class. But in the object space of their derived classes are the objects that contain the base class, just not publicly accessible. As you can see from the code above, the derived classes that protected inherits and private inherits are no longer subclasses of the base class.

So much for writing about non-public inheritance.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: