C ++派生类函数重定义

时间:2014-05-02 05:01:37

标签: c++

成员函数void readPayInfo()在类BonusEmployee中重新定义。它现在返回数据成员基本工资的值加上数据成员奖金的值

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

如何从父类访问basePay?

1 个答案:

答案 0 :(得分:0)

由于您选择将Employee::basePay设为私有,因此无法从子类直接访问它。我认为你有两个选择:

  1. 将吸气剂添加到Employee
  2. Employee::basePay的可见度更改为protected