实验一类与对象

 设计一个用于人事管理的“人类”类。由于考虑到通用性,这里只抽象出所有类型人员都具有的属性:编号,姓名,性别,出生日期,身份证号等。其中“出生日期”定义为一个“日期”类内嵌子对象,用成员函数实现对人员信息的初始化和显示。要求包括:构造函数和析构函数、复制构造函数、内联成员函数、带默认形参值的成员函数、类的组合。

#include<iostream>
using namespace std;
#include<iomanip>//限制输入格式
class date
{
public:
	date(const char* Newd);//c++11,采用const限定,否则报错
	//date(string a);//用字符串也可以
	date(int y, int m, int  d);
	date();
	int Gety() { return y; };//获取属性——年
	int Getm() { return m; };//获取属性——月
	int Getd() { return d; };//获取属性——日
	void Show();//展示日期
private:
	int y, m, d;
};
date::date(const char* Newd)
{
	this->y = 1000 * (Newd[0] - '0') + 100 * (Newd[1] - '0') + 10 * (Newd[2] - '0') + Newd[3] - '0';
	this->m = 10 * (Newd[5] - '0') + Newd[6] - '0';
	this->d = 10 * (Newd[8] - '0') + Newd[9] - '0';
}
//date::date(string a)
//{
//	this->y= atol(a.substr(0, a.find("-")).c_str());
//	this->m= atol(a.substr(a.find("-") + 1, a.rfind("-") - a.find("-") - 1).c_str());
//	this->d= atol(a.substr(a.rfind("-") + 1).c_str());
//}
date::date(int y, int m, int  d)
{
	this->y = y;
	this->m = m;
	this->d = d;
}
date::date() :date(1970, 1, 1) {}//委托构造函数,可以赋值
void date::Show()
{
	cout << "日期为:" << this->y 
		<< "/" << setw(2) << setfill('0') << this->m << "/" 
		<< setw(2) << setfill('0') << this->d << endl;
}

class Person
{
public:
	Person(const char num[10]="000", const char name[20]="张三", bool sex=true, int y=2022, int m=1, int d=1, const char id[18]= "6666666666666666");
	Person(const char num[10] , const char name[20] , bool sex,const char* d, const char id[18]);
	Person(const Person& p);//拷贝构造函数
	char* GetNum() { return num; };//获取属性——编号
	char* GetName() { return name; };//获取属性——姓名
	char* GetId() { return id; };//获取属性——身份证号
	void ShowInfo();//展示信息
	string GetSex();//获取——性别
	~Person() {};
private:
	char num[10];
	char name[20];
	bool sex;
	date bithday;
	char id[18];
};

string Person:: GetSex()
{
	if (this->sex)
		return "男";
	return "女";
}

Person::Person(const char num[10], const char name[20],bool sex,int y,int m,int d, const char id[18])
{
	strcpy_s(this->num,strlen(num)+1, num);
	strcpy_s(this->name,strlen(name)+1, name);
	this->sex = sex;
	this->bithday = date(y,m,d);
	strcpy_s(this->id, strlen(id) + 1, id);
}
Person::Person(const char num[10], const char name[20], bool sex, const char* d, const char id[18])
{
	strcpy_s(this->num, strlen(num) + 1, num);
	strcpy_s(this->name, strlen(name) + 1, name);
	this->sex = sex;
	this->bithday = date(d);
	strcpy_s(this->id, strlen(id) + 1, id);
}
Person::Person(const Person& p)//也可以说初始化列表赋值,省略了
{
	strcpy_s(this->num,strlen(p.num)+1, p.num);
	strcpy_s(this->name, strlen(p.name)+1,p.name);
	this->sex = p.sex;
	this->bithday = p.bithday;
	strcpy_s(this->id,strlen(p.id)+1, p.id);
}
void Person::ShowInfo()
{
    cout << "编号为:" << this->num << " 姓名为:" << this->name<<" 性别为:" <<this->GetSex()<<
		" 出生日期为:" <<this->bithday.Gety()<<"/"<<setw(2)<<setfill('0')<<this->bithday.Getm() 
		<< "/"<<setw(2)<<setfill('0')<< this->bithday.Getd() <<" 身份证号为:" << this->id << endl;
}

int main()
{
	date d1; d1.Show();
	date d2(2002, 12, 12); d2.Show();
	date d3("2003-11-17"); d3.Show();
	Person p1; p1.ShowInfo();
	Person p2("001", "李华", true, "2022-01-01", "3719218981299812");
	p2.ShowInfo();
	Person p3("002", "小红", false, 2022, 3, 28, "2468665718149191");
	p3.ShowInfo();
	Person p4(p1);//拷贝构造函数输出信息p1一样
	p4.ShowInfo();
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值