程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> derived class-一道簡單的C++習題,求標准答案!

derived class-一道簡單的C++習題,求標准答案!

編輯:編程綜合問答
一道簡單的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;
}

最佳回答:


 #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;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved