C++ 学生在册人员类 Person 设计与实现
#include <iostream>
#include <string>
using namespace std;
class Date {
public:
Date(int y, int m, int d) : year(y), month(m), day(d) {}
void display() const {
cout << year << '-' << month << '-' << day << endl;
}
private:
int year, month, day;
};
class Person {
public:
Person() : idPerson(""), name(""), sex(""), birthday(nullptr), homeAddress("") {}
Person(string id, string n, string s, Date* b, string h) : idPerson(id), name(n), sex(s), birthday(b), homeAddress(h) {}
Person(const Person& p) : idPerson(p.idPerson), name(p.name), sex(p.sex), birthday(new Date(*p.birthday)), homeAddress(p.homeAddress) {}
~Person() {
delete birthday;
}
void input() {
cout << "请输入身份证号:";
cin >> idPerson;
cout << "请输入姓名:";
cin >> name;
cout << "请输入性别:";
cin >> sex;
cout << "请输入生日(年月日):";
int year, month, day;
cin >> year >> month >> day;
birthday = new Date(year, month, day);
cout << "请输入家庭住址:";
cin >> homeAddress;
}
void display() const {
cout << "身份证号:" << idPerson << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << sex << endl;
cout << "生日:";
birthday->display();
cout << "家庭住址:" << homeAddress << endl;
}
private:
string idPerson; //身份证号
string name; //姓名
string sex; //性别
Date* birthday; //生日
string homeAddress; //家庭住址
};
int main() {
Person p1;
p1.input();
p1.display();
Person p2 = p1;
p2.display();
return 0;
}
原文地址: https://www.cveoy.top/t/topic/lvIs 著作权归作者所有。请勿转载和采集!