设计person类,包括私有数据成员:姓名、性别和年龄;设计student类继承person类,新增私有数据成员:班级;设计类studentworker继承student类,新增私有数据成员:工资;要求每个类中的数据通过构造函数进行初始化。在主函数中分别定义一个studentworker类的对象,并输出这该对象的各项信息。
时间: 2023-11-27 20:51:06 浏览: 54
以下是设计的代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string name;
string gender;
int age;
public:
Person(string n, string g, int a) : name(n), gender(g), age(a) {}
string getName() { return name; }
string getGender() { return gender; }
int getAge() { return age; }
};
class Student : public Person {
private:
string className;
public:
Student(string n, string g, int a, string c) : Person(n, g, a), className(c) {}
string getClassName() { return className; }
};
class StudentWorker : public Student {
private:
double salary;
public:
StudentWorker(string n, string g, int a, string c, double s) : Student(n, g, a, c), salary(s) {}
double getSalary() { return salary; }
};
int main() {
StudentWorker sw("张三", "男", 20, "计算机科学与技术1班", 3000.0);
cout << "姓名:" << sw.getName() << endl;
cout << "性别:" << sw.getGender() << endl;
cout << "年龄:" << sw.getAge() << endl;
cout << "班级:" << sw.getClassName() << endl;
cout << "工资:" << sw.getSalary() << endl;
return 0;
}
```
输出结果:
```
姓名:张三
性别:男
年龄:20
班级:计算机科学与技术1班
工资:3000
```