C++面向对象的编程-练习5(person类继承/2019年期中考试)

【题目】1. 设计如下类CPerson实现对人的封装,包括私有数据成员:身份证号ID、姓名Name和年龄Age。定义含三个参数的构造函数、拷贝构造函数;定义函数set(),提示键盘输入ID、Name和Age,以修改身份证号、姓名和年龄;定义输出函数display()。

#include<iostream>
using namespace std;
class CPerson
{
private:
	int ID;
	string Name;
	int age;
public:
	CPerson(int i=0,string n = " ", int a=0)
	{
		ID = a;
		Name = n;
		age = a;
	}
	CPerson(const CPerson& cp)
	{
		ID = cp.ID;
		Name = cp.Name;
		age = cp.age;
	}
	void set(int i, string n, int a)
	{
		ID = i;
		Name = n;
		age = a;
	}
	void display()
	{
		cout << "ID:" << ID << "\t" << "Name:" << Name << "\t" << "age:" << age << endl;
	}
};
int main()
{
	CPerson obj1(1002, "LiMing", 22);
	obj1.display();
	CPerson obj2(obj1);
	obj2.set(1222,"liu",34);         //修改身份证号、姓名和年龄
	obj2.display();
	return 0;
}

2. 在上一题基础上,设计类CEmployee公有继承CPerson,新定义私有数据成员工号EmpNo和薪资Salary。定义带参数的构造函数,并通过成员初始化列表向其基类传递参数;重定义函数set(),提示通过键盘输入Salary,以修改薪资;重定义函数display(),并在其内部访问基类中的同名函数。主函数如下:

#include<iostream>
using namespace std;
class CPerson
{
private:
	int ID;
	string Name;
	int age;
public:
	CPerson(int i = 0, string n = " ", int a = 0)
	{
		ID = a;
		Name = n;
		age = a;
	}
	CPerson(const CPerson& cp)
	{
		ID = cp.ID;
		Name = cp.Name;
		age = cp.age;
	}
	void set()
	{
		cin>>ID;
		cin>>Name;
		cin>>age;
	}
	void display()
	{
		cout << "ID:" << ID << "\t" << "Name:" << Name << "\t" << "age:" << age ;
	}
};
class CEmployee :public CPerson
{
private:
	string EmpNo;
	double Salary;
public:
	CEmployee(string EN, double Sa, int i, string n, int a) :CPerson(i, n, a)
	{
		EmpNo = EN;
		Salary = Sa;
	}
	void set()
	{
		CPerson::set();
		cin >> EmpNo;
		cin >> Salary;
	}
	void display()
	{
		CPerson::display();
		cout << "EmpNo:" << EmpNo <<"\t"<< "Salary:" << Salary << endl;
	}
};
int main()
{
	CEmployee e1( "LiMing",82.22, 22, "A101", 5500);
	e1.display();    //输出为:ID,Name,Age,EmpNo,Salary
	e1.set();        //修改薪资
	e1.display();
	return 0;
}

3·在上一题类CEmployee中,新增静态成员count和sum,以统计员工总人数和总薪资。定义静态成员函数display_sum(),以显示count和平均薪资;并在主函数中实现对上述相关成员的访问。

#include<iostream>
using namespace std;
class CPerson
{
private:
	int ID;
	string Name;
	int age;
public:
	CPerson(int i = 0, string n = " ", int a = 0)
	{
		ID = a;
		Name = n;
		age = a;
	}
	CPerson(const CPerson& cp)
	{
		ID = cp.ID;
		Name = cp.Name;
		age = cp.age;
	}
	void set()
	{
		cin >> ID;
		cin >> Name;
		cin >> age;
	}
	void display()
	{
		cout << "ID:" << ID << "\t" << "Name:" << Name << "\t" << "age:" << age;
	}
};
class CEmployee :public CPerson
{
private:
	string EmpNo;
	double Salary;
	int static sum;
	double static count;
public:
	CEmployee( int i, string n, int a,string EN, double Sa) :CPerson(i, n, a)
	{
		EmpNo = EN;
		Salary = Sa;
		sum++;
		count = count + Salary;
	}
	void set()
	{
		CPerson::set();
		cin >> EmpNo;
		cin >> Salary;
		sum++;
		count = count + Salary;
	}
	void display()
	{
		CPerson::display();
		cout << "EmpNo:" << EmpNo << "\t" << "Salary:" << Salary << endl;
	}
	static void display_sum()
	{
		cout << "总薪资为:" << count << "\t" << "平均薪资为:" << count / sum << endl;
	}
};
double CEmployee::count = 0;
int CEmployee::sum = 0;
int main()
{
	CEmployee e1(22, "A101", 5500,"LiMing", 82.22);
	e1.display();    //输出为:ID,Name,Age,EmpNo,Salary
	e1.set();        //修改薪资
	e1.display();
	e1.display_sum();
	return 0;
}
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值