阿俊

00后工科男,热衷社会文化心理研究,重点大学计算机在读。

C++类与对象小程序

设计要求:

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

解决方案代码:

#include<iostream>

#include<string.h>

using namespace std;


class date{

public:

    date(int Year=2000,int Month=1,int Day=1);

date(date &Date);

//析构函数 

~date(){}

//声明定义均在类内,为内联函数

getdate(){

cin>>year;

cin>>month;

cin>>day;

}

putdate(){

cout<<"出生日期:"<<year<<"."<<month<<"."<<day<<endl;

}

private:

int year;

int month;

int day; 

};

//定义构造函数 

date::date(int Year,int Month,int Day){

year=Year;

month=Month;

day=Day;

}

//定义复制构造函数 

date::date(date &Date){

year=Date.year;

month=Date.month;

day=Date.day;

}


class person{

public:

person(date Birthday);

person(person &Person);

//析构函数

~person(){}

//声明定义均在类内,为内联函数 

void getnumber(){

cin>>number;

}

void getgender(){

cin>>gender;

}

void getbirthday(){

birthday.getdate();

}

void getid(){

cin>>id;

}

void putnumber(){

cout<<"编号:"<<number<<endl;

}

void putgender(){

cout<<"性别:"<<gender<<endl;

}

void putbirthday(){

birthday.putdate();

}

void putid(){

cout<<"身份证号:"<<id<<endl;

}

private:

        string number;

    string gender;

    date birthday;

    string id;

}; 

//定义构造函数 

person::person(date Birthday):birthday(Birthday){

cout << "Calling constructor of person" << endl;

//birthday.getdate();

}

//定义复制构造函数 

person::person(person &Person):birthday(Person.birthday){

cout << "Calling the copy constructor of person" << endl;

birthday=Person.birthday;

}


int main()

{

date BIRTHDAY1;

person p1(BIRTHDAY1);

cout<<"请录入人员信息"<<endl;

cout<<"编号"<<endl;

p1.getnumber(); 

cout<<"性别"<<endl;

p1.getgender();

cout<<"出生日期(按年月日依次填写)"<<endl;

p1.getbirthday();

cout<<"身份证号"<<endl;

p1.getid();

cout<<"该人员的信息如下"<<endl; 

p1.putnumber();

p1.putgender();

p1.putbirthday();

p1.putid();

system("pause"); 

return 0;

}

运行结果图:



评论