简体   繁体   English

C ++派生类函数的重新定义

[英]C++ Derived class function redefinition

member function void readPayInfo() is redefined in class BonusEmployee. 成员函数void readPayInfo()在BonusEmployee类中重新定义。 It now returns the value of the data member base pay plus the value of the data member bonus 现在,它返回数据成员基本工资的值加上数据成员奖金的值

class Employee
{
public:
      //constructors here
      void readPayInfo()
      {cin >> basePay;}
private:
      double basePay;
};


class BonusEmployee : public Employee
{
public:
      //constructors here
      void readPayInfo()
      {cin >> basePay >> bonus;} // NULL!
private:
      bonus;
};

how do I access basePay from parent class? 如何从父类访问basePay?

Since you've opted to make Employee::basePay private there is no way to directly access it from a subclass. 由于您已选择将Employee::basePay私有,因此无法从子类直接访问它。 I think you have two options: 我认为您有两种选择:

  1. Add a getter to Employee , or Employee添加一个getter,或者
  2. Change the visibility of Employee::basePay to protected . Employee::basePay的可见性更改为protected

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM