8.3 人事管理类的设计与实现-类组合

类组合练习
要求在Date类基础上采用类组合的思想,设计一个人事管理类并测试之,该类包括编号、性别、出生日期(即出生日期是一个日期类的对象)、姓名等。涉及带参构造函数,能提供显示人员的信息的函数。

前置代码::

#include <iostream>
#include <string>
using namespace std;
class Date//日期类定义
{
private:
	int year,month,day;
public:
	Date(int y=0,int m=0,int d=0)//带默认参数的构造函数,无参和有参合二为一
	{
		year=y;
		month=m;
		day=d;
	}
	void Show()
	{
		cout<<year<<"-"<<month<<"-"<<day<<endl;
	}
};

后置代码::

int main()
{
   Person x(1,0,1980,12,31,"wangming");//定义一个雇员对象,带参数
   x.Show();//输出雇员信息,注意该Show函数中调用日期对象的Show函数
   return 0;
}

期待度输出::

1,female↵
1980-12-31↵
wangming↵

Person类的设计::

class Person
{
	private:
		Date x;
		int num;
		int sex;
		string name;
	public:
		Person(int a,int b,int y,int m,int d,const string &c) :x(y,m,d)
		{
			num=a;
			sex=b;
			name=c;
		}
		void Show()
		{
			cout<<num<<",";
			if(sex==0) cout<<"female"<<endl;else cout<<"male"<<endl;
			x.Show();
			cout<<name<<endl;
		}
};

猜你喜欢

转载自blog.csdn.net/q767410241/article/details/84065806
8.3