定义类CPerson,它有以下属性:姓名、身份证号、性别、年龄、和相应的成员函数(无构造函数)。再利用CPerson派生出类CEmployee,派生类CEmployee增加了两个表示部门和薪水的数据成员,根据需要为派生类增加新的成员函数
时间: 2024-04-10 07:33:01 浏览: 12
根据您的要求,以下是类CPerson和派生类CEmployee的代码示例:
```cpp
#include <iostream>
#include <string>
using namespace std;
class CPerson {
protected:
string name;
string id;
string gender;
int age;
public:
void setData(string n, string i, string g, int a) {
name = n;
id = i;
gender = g;
age = a;
}
void display() {
cout << "姓名:" << name << endl;
cout << "身份证号:" << id << endl;
cout << "性别:" << gender << endl;
cout << "年龄:" << age << endl;
}
};
class CEmployee : public CPerson {
private:
string department;
double salary;
public:
void setEmployeeData(string n, string i, string g, int a, string d, double s) {
setData(n, i, g, a);
department = d;
salary = s;
}
void display() {
CPerson::display();
cout << "部门:" << department << endl;
cout << "薪水:" << salary << endl;
}
};
int main() {
CEmployee emp;
emp.setEmployeeData("张三", "1234567890", "男", 30, "技术部", 5000);
emp.display();
return 0;
}
```
在这段代码中,类CPerson具有姓名、身份证号、性别和年龄这些属性,并且具有setData函数用于设置这些属性的值,以及display函数用于输出这些属性的值。
类CEmployee是由类CPerson派生而来的,增加了部门和薪水这两个数据成员,并且根据需要添加了setEmployeeData函数来设置员工的属性值。在setEmployeeData函数中,调用了基类CPerson的setData函数来设置基类中的属性值。
CEmployee类也重写了基类CPerson的display函数,以便输出员工的所有属性,包括基类中的属性和派生类中新增的属性。
在main函数中,创建了一个CEmployee对象emp,并使用setEmployeeData函数设置员工的属性值,然后调用display函数输出员工的所有属性值。