yanjusong1949 2015-04-21 13:33 采纳率: 66.7%
浏览 1653
已采纳

一道简单的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条回答 默认 最新

  • 知常曰明 2015-04-22 03:47
    关注
     #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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器