当前位置: 首页 > 工具软件 > 2people > 使用案例 >

People类

宫瀚
2023-12-01

题目

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

相关阅读

相关阅读

完整代码

#include<bits/stdc++.h>
#define Pi 3.14;
using namespace std;

class Date{
    int year;
    int month;
    int day;
public:
    Date(){};
    Date(int year, int month, int day){
        this->year = year;
        this->month = month;
        this->day = day;
    }
    void output(){
        cout << year << " 年 " << month << " 月 " << day << " 日 " << endl;
    }
};

class People{
    string number;
    int sex;
    Date birthday;
    string id;
public:
    People(){
        cout << "People被构造!" << endl;
    };
    ~People(){
        cout << "People被析构!" << endl;
    };
    People(People &other){
        number = other.number;
        sex = other.sex;
        birthday = other.birthday;
        id = other.id;
        cout << other.number << "被拷贝!" << endl;
    }
    void output(){
        cout << "编号: " << number << endl;
        if (sex == 1)
            cout << "性别: 男" << endl;
        else if (sex == 0)
            cout << "性别: 女" << endl;
        else
            cout << "性别: 未知" << endl;
        cout << "出生日期: ";
        birthday.output();
        cout << "身份证号: " << id << endl;
    }
    inline void input(string number, int sex, Date birthday, string id);
};
inline void People::input(string number, int sex, Date birthday, string id){
    this->number = number;
    this->sex = sex;
    this->birthday = birthday;
    this->id = id;
}
int main(){
    People people;
    people.input("2018170000", 1, Date(2000, 10, 2), "666666666666666666");
    people.output();
    People other(people);
    other.output();
}
 类似资料: