请设计日期类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 13:04:27 浏览: 22
好的,我理解了你的要求。以下是代码实现: ```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对象的内存。 希望这个代码可以帮到你!

相关推荐

最新推荐

recommend-type

C++实现日期类(Date类)的方法

下面小编就为大家带来一篇C++实现日期类(Date类)的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

js用Date对象的setDate()函数对日期进行加减操作

在某个日期上加减天数来说,其实只要调用Date对象的setDate()函数就可以了,具体方法如下
recommend-type

java日期操作工具类(获取指定日期、日期转换、相隔天数)

主要为大家详细介绍了java日期操作工具类,包括获取指定日期、日期转换、相隔天数等操作,感兴趣的小伙伴们可以参考一下
recommend-type

日期类 Date 日期的基本运算

设计一个日期类Date,包括年、月、日等私有数据成员。要求实现日期的基本运算,如一日期加上天数、一日期减去天数、两日期相差的天数
recommend-type

解析php时间戳与日期的转换

strtotime能将任何英文文本的日期时间描述解析为Unix时间戳,我们结合mktime()或date()格式化日期时间获取指定的时间戳,实现所需要的日期时间。 strtotime 将任何英文文本的日期时间描述解析为Unix时间戳[将系统...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用matlab绘制高斯色噪声情况下的频率估计CRLB,其中w(n)是零均值高斯色噪声,w(n)=0.8*w(n-1)+e(n),e(n)服从零均值方差为se的高斯分布

以下是用matlab绘制高斯色噪声情况下频率估计CRLB的代码: ```matlab % 参数设置 N = 100; % 信号长度 se = 0.5; % 噪声方差 w = zeros(N,1); % 高斯色噪声 w(1) = randn(1)*sqrt(se); for n = 2:N w(n) = 0.8*w(n-1) + randn(1)*sqrt(se); end % 计算频率估计CRLB fs = 1; % 采样频率 df = 0.01; % 频率分辨率 f = 0:df:fs/2; % 频率范围 M = length(f); CRLB = zeros(M,1); for
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。