“公共”的使用在派生类声明中?

发布于 2024-08-07 21:14:50 字数 519 浏览 8 评论 0原文

给定这个基类:

class Employee
{
     char* name;
     int age;

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

对于“public”,this:

class Manager : public Employee
{
   EmployeeList employees;

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

和 this:之间有什么区别?

class Manager : Employee
{
   EmployeeList employees;

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

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();
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趁年轻赶紧闹 2024-08-14 21:14:50

默认是私有继承。举个例子:

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

使用私有继承作为默认继承。这意味着 D 获取 B 拥有的所有受保护和公共字段和方法(如果我们实际上声明了任何),但不能转换为 B。因此,此代码失败:

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

如果 D 公开继承自 B,则 D可以转换为 B,并且这个函数调用就可以了。

第二个区别是 B 中的所有受保护和公共成员都成为 D 中的私有成员。

这实际上意味着什么?公共继承意味着 D IS_A B,但私有继承意味着“根据...实现”。从 B 继承 D 意味着您想要利用 B 中的某些功能,但不是因为 D IS_A B 或因为 B 和 D 之间存在任何概念上的联系。:D

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

弱骨蛰伏 2024-08-14 21:14:50

如果没有这个“公共”,“员工”将成为“经理”的私有基类。

使用关键字“class”声明的类默认其成员为私有,其基类默认为私有。

使用关键字“struct”声明的类默认其成员是公共的,并且其基类默认是公共的。

Without that 'public' 'Employee' would become a private base class of 'Manager'.

Classes declared with keyword 'class' have their members private by default, and have their base classes private by default.

Classes declared with keyword 'struct' have their members public by default, and have their base classes public by default.

风渺 2024-08-14 21:14:50

在C++中,继承默认是私有的。然而,对于使用 Manager 类的任何代码来说,似乎几乎没有区别,因为它们具有相同的方法。

但是,您无法将 Manager 对象强制转换为 Employee。您也无法从 Manager 类中访问 employees 变量。

In C++, inheritance is private by default. However, to any code using the Manager class, there appears to be almost no difference, since they have the same methods.

You won't be able to cast the Manager object to an Employee, though. You also won't be able to access the employees variable from within the Manager class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文