Description
定义Date类,表示由年、月、日构成的日期,定义其构造函数和析构函数,产生如样例所示的输出。
定义Person类,包括一个Date类的对象成员表示其生日,一个string的数据成员表示其名字。定义其构造函数和析构函数,产生如样例所示的输出。
Input
第一行是一个合法的日期;第二行是一个无空白符的字符串和一个合法的日期。
Output
见样例。
Sample Input
1997 10 1
tom 1997 11 12
Sample Output
Date 1997-10-1 is created.
Date 1997-11-12 is created.
Person tom was born at 1997-11-12 is created.
Person tom was born at 1997-11-12 is erased.
Date 1997-11-12 is erased.
Date 1997-10-1 is erased.
Append Code
int main()
{
string name;
int year, month, day;
cin>>year>>month>>day;
Date date(year, month, day);
cin>>name>>year>>month>>day;
Person person(year, month, day, name);
return 0;
}
AC代码
#include <iostream>
#include <string>
using namespace std;
class Date {
friend class Person;
private:
int year, month, day;
public:
Date(int y, int m, int d): year(y), month(m), day(d) {
cout << "Date " << year << "-" << month << "-" << day << " is created.\n";
}
~Date() {
cout << "Date " << year << "-" << month << "-" << day << " is erased.\n";
}
};
class Person {
friend class Date;
private:
Date date;
string name;
public:
Person(int y, int m, int d, string n) :date(y, m, d), name(std::move(n)) {cout << "Person " << name << " was born at " << date.year << "-" << date.month << "-" << date.day << " is created.\n";}
~Person() {cout << "Person " << name << " was born at " << date.year << "-" << date.month << "-" << date.day << " is erased.\n";}
};
int main()
{
string name;
int year, month, day;
cin>>year>>month>>day;
Date date(year, month, day);
cin>>name>>year>>month>>day;
Person person(year, month, day, name);
return 0;
}