请设计日期类Date,用于存储年月日。请设计人类Person,用于存储姓名Name和出生日期Birth和离家日期Leave。请设计人群类Persons,用于存储多个人对象的指针。 输入n+1行,第1行是正整数n(n<10),第2行至第n+1行,每行是姓名和出生日期(年月日)和离家日期(年月日)。 输出7n+2行,表达了n个对象的构造与析构的过程。 前3n行中,每3行表示了一个人类对象的构造过程。 之后的n行中,每1行是一个人类对象的输出。 之后的3n行中,每3行是一个人类对象的析构过程。
时间: 2024-02-15 14:04:27 浏览: 171
用c++设计一个日期类Date,包括年、月、日等私有数据成员。要求实现日期的基本运算,如一日期加上天数、一日期减去天数、两日期相差的天数等。
5星 · 资源好评率100%
好的,我理解了你的要求。以下是代码实现:
```c++
#include <iostream>
#include <string>
using namespace std;
class Date {
private:
int year, month, day;
public:
Date(int y, int m, int d) {
year = y;
month = m;
day = d;
cout << "Constructed a Date object with year = " << year << ", month = " << month << ", day = " << day << endl;
}
~Date() {
cout << "Destructed a Date object with year = " << year << ", month = " << month << ", day = " << day << endl;
}
};
class Name {
private:
string first, last;
public:
Name(string f, string l) {
first = f;
last = l;
cout << "Constructed a Name object with first name = " << first << ", last name = " << last << endl;
}
~Name() {
cout << "Destructed a Name object with first name = " << first << ", last name = " << last << endl;
}
};
class Person {
private:
Name *name;
Date *birth, *leave;
public:
Person(Name *n, Date *b, Date *l) {
name = n;
birth = b;
leave = l;
cout << "Constructed a Person object with name = " << name->first << " " << name->last << ", birth date = " << birth->year << "/" << birth->month << "/" << birth->day << ", leave date = " << leave->year << "/" << leave->month << "/" << leave->day << endl;
}
~Person() {
cout << "Destructed a Person object with name = " << name->first << " " << name->last << ", birth date = " << birth->year << "/" << birth->month << "/" << birth->day << ", leave date = " << leave->year << "/" << leave->month << "/" << leave->day << endl;
delete name;
delete birth;
delete leave;
}
};
class Persons {
private:
Person **people;
int size;
public:
Persons(int n) {
size = n;
people = new Person*[size];
cout << "Constructed a Persons object with size = " << size << endl;
}
~Persons() {
cout << "Destructed a Persons object with size = " << size << endl;
for (int i = 0; i < size; i++) {
delete people[i];
}
delete[] people;
}
void addPerson(Name *n, Date *b, Date *l, int index) {
people[index] = new Person(n, b, l);
}
Person* getPerson(int index) {
return people[index];
}
};
int main() {
int n;
cin >> n;
Persons *group = new Persons(n);
for (int i = 0; i < n; i++) {
string first, last;
int byear, bmonth, bday, lyear, lmonth, lday;
cin >> first >> last >> byear >> bmonth >> bday >> lyear >> lmonth >> lday;
Name *name = new Name(first, last);
Date *birth = new Date(byear, bmonth, bday);
Date *leave = new Date(lyear, lmonth, lday);
group->addPerson(name, birth, leave, i);
cout << endl;
}
for (int i = 0; i < n; i++) {
Person *p = group->getPerson(i);
cout << "Person " << i+1 << ": " << p->getFirstName() << " " << p->getLastName() << ", born on " << p->getBirthYear() << "/" << p->getBirthMonth() << "/" << p->getBirthDay() << ", left home on " << p->getLeaveYear() << "/" << p->getLeaveMonth() << "/" << p->getLeaveDay() << endl;
cout << endl;
}
delete group;
return 0;
}
```
需要注意的是,由于题目要求输出对象的构造和析构过程,因此需要在Date、Name、Person和Persons类中分别添加构造函数和析构函数,并在其中输出相关信息。另外,由于Person类中使用了指针,因此需要在析构函数中手动释放内存。
在main函数中,我们首先读入人群数量n,并创建一个Persons对象。接着,循环n次,每次读入一个人的信息,创建一个Name对象、一个Birth对象和一个Leave对象,并将这些指针传递给Person对象,将Person对象指针存储在Persons对象中。最后,循环n次,输出每个Person对象的信息。最后,释放Persons对象的内存。
希望这个代码可以帮到你!
阅读全文