C++构造一个people类

设计一个用于人事管理的 People(人员)类。考虑到通用性,这里只抽象出所有
类型 人员都具有的属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份
证号)等。
其中"出生日期"定义为一个"日期"类内嵌子对象。
用成员函数实现对人员信息 的录入和显示。
要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、带缺省形参值的成员函数

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

class Date {
public:
	Date(int newY, int newM, int newD);
	Date() {
		year = 2000;
		mouth = 0;
		day = 0;
	};
	Date(Date& p);
	~Date(){}
	void setdate(int newY, int newM,int newD);
	void showdate();
private:
	int year, mouth, day;
};
Date::Date(int newY, int newM, int newD) {
	 year=newY ;//!!!!!!!!!!!!注意这里的顺序,参数在右边
	 mouth= newM;
	 day= newD;
}

Date::Date(Date& p) {
	year = p.year;
	mouth = p.mouth;
	day = p.mouth;
}
void Date::setdate(int newY, int newM,int newD) {
	year = newY;
	mouth = newM;
	day = newD;
}
void Date::showdate() {
	cout << year << " " << mouth << " " << day<<endl;
}
//注意身份证号用字符串类型
class People {
public:
	People(int nb, char s, Date bd, string idnb);
	People() {
		number = 0;
		sex = 'm';
		Date x(2000, 0, 0);
		birthday = x;
		id = "";
	};
	People(People& p);
	~People(){}
	void setifm();
	void showifm();

private:
	int number; char sex; Date birthday; string id;
};
//people::people(date bd,long int nb=0,long int idnb=0,char sx='m'):birthday(bd)
People::People(int nb, char s, Date bd, string idnb) :birthday(bd) {									//bd是birthday的形参,birthday才是类的数据成员
	nb = number;
	s = sex;
	bd = birthday;
	idnb = id;
}

//people::people(people&p):birthday(p.birthday)
People::People(People& p):birthday(p.birthday){
	number = p.number;
	sex = p.sex;
	birthday = p.birthday;
	id = p.id;
}
void People::setifm() {
	cout << "请输入编号:";
	int nb;
	cin >> nb;
	cout << "请输入性别:";
	char s;
	cin >> s;
	cout << "请输入出生日期(形如:2000 4 22):";
	int a, b, c;
	cin >> a >> b >> c;
	Date bd(a,b,c);//!!!!!!!!!!!!1这里直接Date bd(a,b,c) 和bd.setdate(a,b,c)都可
	cout << "请输入身份证号:";
	string idnb;
	cin >> idnb;

	number = nb;
	sex = s;
	birthday = bd;
	id = idnb;
}
//这里要调用date的成员函数
void People::showifm() {
	cout << "编号:" << number<<endl;
	cout << "性别;" << sex << endl;
	cout << "出生日期:"; 
	birthday.showdate();
	cout << "身份证号:" << id;
}
//不能直接读取一个类的对象
int main() {
	People zhangsan;
	zhangsan.setifm();
	zhangsan.showifm();
}
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值