Description
定义类Date,用于表示日期,拥有:
1. 3个int类型属性,分别表示年、月、日。
2. 构造函数、析构函数。要输出一些样例所示的信息。
3. void show方法,用于输出日期值。格式见样例。
定义类Person,包括
1. 一个Date类的对象用于表示生日,一个字符串表示其姓名(假定不含有空白符)。
2. 构造函数和析构函数。要输出一些样例所示的信息。
3. int getAge(Date& now)方法,求在当前日期now时的周岁年龄。日期now一定大于birthday。
4. void show()方法,用于显示Person的信息,格式见样例。
5. getName()方法,用于返回其名字。
Input
输入3行。第1行包括1个合法的日期的年、月、日三个数据。
第2行是一个不含空白符的字符串,是一个人的名字。
第3行是当前日期,包括年、月、日三个数据。
Output
见样例。
Sample Input
1999 2 2
Tom
2014 5 8
Sample Output
Date 1999-2-2 is created.
Person Tom is created.
Tom's birthday is 1999-2-2.
Date 2014-5-8 is created.
Now, Tom is 15.
Date 2014-5-8 is erased.
Person Tom is erased.
Date 1999-2-2 is erased.
#include<bits/stdc++.h>
using namespace std;
class Date
{
friend class Person;
private:
int y,m,d;
public:
Date(int year,int month,int day):y(year),m(month),d(day)
{
cout << "Date " << y << "-" << m << "-" << d << " is created." << endl;
}
~Date()
{
cout << "Date " << y << "-" << m << "-" << d << " is erased." << endl;
}
void show()
{
}
};
class Person
{
friend class Date;
private:
Date b;
string n;
public:
Person(int yy,int mm,int dd,string nn):b(yy,mm,dd),n(nn)
{
cout << "Person " << n << " is created." << endl;
}
~Person()
{
cout << "Person " << n << " is erased." << endl;
}
void show()
{
cout << getName() << "'s birthday is " << b.y << "-" << b.m << "-" << b.d << "." << endl;
}
string getName()
{
return n;
}
int getAge(Date& p)
{
int a;
a=p.y-b.y;
return a;
}
};