Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Given this base class:

class Employee
{
     char* name;
     int age;

  public:
     Employee(char* name);
     void print();
};

With regards to the "public", what's the difference between this:

class Manager : public Employee
{
   EmployeeList employees;

   public:
     Manager(char* name, Employee* people);
     void print();
};

and this:

class Manager : Employee
{
   EmployeeList employees;

  public:
     Manager(char* name, Employee* people);
     void print();
};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
162 views
Welcome To Ask or Share your Answers For Others

1 Answer

The default is private inheritance. take this example:

class B { };
class D: B { };

uses private inheritance as its the default. This means that D gets all the protected and public fields and methods that B has (if we actually declared any), but can't be cast to a B. Therefore, this code fails:

void foo(B* argument) {}
foo(new D);                   //not allowed

If D publicly inherited from B, then a D could be cast to a B, and this function call would be fine.

The second difference is that all the protected and public members in B become private members in D.

What does this actually mean? Public inheritance means D IS_A B, but private inheritance means "is implemented in terms of". Inheriting D from B means you want to take advantage of some of the features in B, but not because D IS_A B or because there's any conceptual connection between B and D. :D


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...