一道简单的C++习题,求标准答案!

Define a class, DATE, including current year, month and day. You may search in MSDN with keywords "tm" to know how to get the current date. Define a derived class BIRTHDAY of DATE, saving someone's birthday with year, month and day. The programmer can call the member function AGE to get someone's age as follows:

int main
{
BIRTHDAY Zhang(1988, 5, 2);
cout << Zhang.AGE() << endl; // Now Zhang's age is 26.
return 0;
}

4个回答

 #include <iostream>
#include <time.h>

using namespace std;

class BIRTHDAY
{
private:
    int _year;
    int _month;
    int _day;
public:
    BIRTHDAY(int year, int month, int day);
    int AGE();
};

BIRTHDAY::BIRTHDAY(int year, int month, int day)
{
    _year = year;
    _month = month;
    _day = day;
}

int BIRTHDAY::AGE()
{
    time_t tt = time(NULL);
    tm* t = localtime(&tt);
    return t->tm_year - _year + 1900;
}
int main()
{
    BIRTHDAY Zhang(1988, 5, 2);
    cout << Zhang.AGE() << endl; // Now Zhang's age is 26.

    getchar();
    return 0;
}

di实现BIRTHDAY类,构造函数,然后成员函数AGE返回年的差值

能不能给完整代码,我太渣了!

定义一个BIRTHDAY类,然后定义AGE函数,计算当前时间和指定时间的时间差算年纪

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问