.设计person类,包括私有数据成员:姓名、性别和年龄;设计student类继承person类,新增私有数据成员:班级;设计类studentworker继承student类,新增私有数据成员:工资;要求每个类中的数据通过构造函数进行初始化。在主函数中分别定义一个studentworker类的对象,并输出这该对象的各项信息。
时间: 2023-11-27 08:51:22 浏览: 167
下面是代码实现:
```c++
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
Person(string name, char gender, int age): m_name(name), m_gender(gender), m_age(age) {}
virtual ~Person() {}
string getName() const { return m_name; }
char getGender() const { return m_gender; }
int getAge() const { return m_age; }
private:
string m_name;
char m_gender;
int m_age;
};
class Student : public Person {
public:
Student(string name, char gender, int age, string cls): Person(name, gender, age), m_class(cls) {}
virtual ~Student() {}
string getClass() const { return m_class; }
private:
string m_class;
};
class StudentWorker : public Student {
public:
StudentWorker(string name, char gender, int age, string cls, double salary): Student(name, gender, age, cls), m_salary(salary) {}
virtual ~StudentWorker() {}
double getSalary() const { return m_salary; }
private:
double m_salary;
};
int main() {
StudentWorker sw("Tom", 'M', 20, "Class 1", 10000.0);
cout << "Name: " << sw.getName() << endl;
cout << "Gender: " << sw.getGender() << endl;
cout << "Age: " << sw.getAge() << endl;
cout << "Class: " << sw.getClass() << endl;
cout << "Salary: " << sw.getSalary() << endl;
return 0;
}
```
输出结果:
```
Name: Tom
Gender: M
Age: 20
Class: Class 1
Salary: 10000
```
阅读全文