定义类CPerson,它有一下属性:姓名、身份证号、性别、年龄和相应的成员函数(无构造函数)。在利用类CPerson派生出类CEmployee,派生类CEmployee增加了两个表示部门和薪水的数据成

定义类CPerson,它有一下属性:姓名、身份证号、性别、年龄和相应的成员函数(无构造函数)。在利用类CPerson派生出类CEmployee,派生类CEmployee增加了两个表示部门和薪水的数据成员,根据需要为派生类增加新的成员函数

//HW_CH04_36
#include<iostream>
#include<string>
using namespace std;

class CPerson {
private:
	string name;
	string ID;
	bool gender;
	int age;
public:
	void set(string name, string ID, bool gender, int age) {
		this->name = name;
		this->ID = ID;
		this->gender = gender;
		this->age = age;
	}

	string getname() {
		return name;
	}
	string getID() {
		return ID;
	}
	bool getgender() {
		return gender;
	}
	int getage() {
		return age;
	}
};

class CEmployee :public CPerson {
private:
	string department;
	int salary;
public:
	void sete(string department, float salary) {
		this->department = department;
		this->salary = salary;
	}

	void show(CEmployee& e) {
		cout << e.getname() << "is in " << department<<endl;
		cout << "he is " << e.getage() << " years old" << endl;
		cout << "the pay of work is:" << salary << "¥" << endl;
	}
};

int main() {
	CEmployee e;
	e.set("liulu", "202124033", 1, 43);
	e.sete("计算机系", 1000000);
	e.show(e);
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据您的要求,以下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函数输出员工的所有属性值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值