Problem I: 今年多少岁
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1112 Solved: 369
[ Submit][ Status][ Web Board]
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 2Tom2014 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.
HINT
Append Code
#include <bits/stdc++.h>
using namespace std;
class Date
{
public:
int yy,mm,dd;
Date(int y,int m,int d):yy(y),mm(m),dd(d){cout<<"Date "<<yy<<"-"<<mm<<"-"<<dd<<" is created."<<endl;}
virtual ~Date(){cout<<"Date "<<yy<<"-"<<mm<<"-"<<dd<<" is erased."<<endl;}
};
class Person:public Date
{
public:
string Name;
Person(int y,int m,int d,string name):Date(y,m,d),Name(name){cout<<"Person "<<Name<<" is created."<<endl;}
~Person(){cout<<"Person "<<Name<<" is erased."<<endl;}
int getAge(Date& now)
{
int f;
f=now.yy-yy;
return f;
}
void show(){cout<<Name<<"'s birthday is "<<yy<<"-"<<mm<<"-"<<dd<<"."<<endl;}
string getName(){return Name;}
};
int main()
{
int y, m, d;
string name;
cin>>y>>m>>d>>name;
Person person(y, m, d, name);
person.show();
cin>>y>>m>>d;
Date now(y,m,d);
cout<<"Now, "<<person.getName()<<" is "<<person.getAge(now)<<"."<<endl;
return 0;
}