题目是这样的:
我写的是下面这样的,想知道为什么在Manage类的成员函数里使用Employee类的私有成员数据会报错呢?哪里不对吗
#include<iostream>
using namespace std;
class Employee {
private:
//1)
string name;
float salary;
public:
Employee(const string new_name, float new_salary = 10000);//1.1)
Employee(Employee& emp);//1.2)
void raiseSalary(float percent);//1.3)
void show();//1.4)
//1.5)
string getName() const
{
return name;
}
float getSalary() const
{
return salary;
}
};
//1.1)
Employee::Employee(const string new_name, float new_salary):name(new_name),salary(new_salary)
{}
//1.2)
Employee::Employee(Employee& emp):name(emp.name),salary(emp.salary)
{}
//1.3)
void Employee::raiseSalary(float percent)
{
salary = salary * (1 + percent);
}
//1.4)
void Employee::show()
{
cout << "His or Her name is:" << name << endl;
cout << "the salary of this employee:" << salary << endl;
}
class Manager:public Employee {
private:
//2)
string position;
float bonus;
Employee emplo;
public:
Manager(const string &new_name, string position, float bonus = 0.1, float new_salary=20000);//2.1)
//2.2)为新添加的成员编写合适的访问函数
string getPosition() const
{
return position;
}
float getBonus() const
{
return bonus;
}
void raiseSalary(float percent);//2.3)
void show();//2.4)
};
//2.1)
Manager::Manager(const string& new_name, string position, float bonus, float new_salary):Employee(new_name,new_salary),position(position),bonus(bonus)
{}
//2.3)
void Manager::raiseSalary(float percent)
{
salary = salary * (1 + percent + bonus);
}
//2.4)
void Manager::show()
{
cout << "His or Her name is:" << name << endl;
cout << "Employee Type: Manager," << " Name = " << name << " ,Position = " << position << " ,Salary =" << salary << endl;
cout << "the salary of this employee:" << salary << endl;
}
int main() {
Employee tom("Tom");
tom.show();
tom.raiseSalary(0.1);
tom.show();
Manager ceo("Peter", "CEO", 30000, 0.1);
ceo.show();
ceo.raiseSalary(0.15);
ceo.show();
return 0;
}