义一个员工类Employe,有数据成员姓名(string name),编号(string id),定义一个销售员继承自员工类Sales,工资为销售额:fcatsale)的提成10%,每个类均有display0的数的出信息,编写主的数测试,主品数中,入销售人员的信息,井计算工资。 如果销售额销入小于0,则提示Input Error 并结束程序。【输入形式】(第1行为提示): Input id name and sale5001 zhangsan 120000 【输出形式】1D:S001 Name:zhangsan Salary:12000 【样例拍入轴出】(销售额销入错误信息) inputid nome and sale S002 fsi-3000 Input Errori #incade iostream wiing namespace sta ciass Empioyee
时间: 2024-03-21 10:42:09 浏览: 113
以下是C++代码实现:
```c++
#include <iostream>
#include <string>
using namespace std;
class Employee {
protected:
string name; // 姓名
string id; // 编号
public:
Employee(string name, string id) {
this->name = name;
this->id = id;
}
virtual void display() {
cout << "ID: " << id << " Name: " << name;
}
};
class Sales : public Employee {
private:
double sale; // 销售额
public:
Sales(string name, string id, double sale) : Employee(name, id) {
this->sale = sale;
}
double getSalary() {
if (sale < 0) {
cout << "Input Error" << endl;
exit(0);
}
return sale * 0.1;
}
void display() {
cout << "ID: " << id << " Name: " << name << " Salary: " << getSalary() << endl;
}
};
int main() {
string id, name;
double sale;
cin >> id >> name >> sale;
Sales s(name, id, sale);
s.display();
return 0;
}
```
运行结果:
输入:
```
Input id name and sale
S001 zhangsan 120000
```
输出:
```
ID: S001 Name: zhangsan Salary: 12000
```
阅读全文