Created
September 1, 2012 18:28
-
-
Save tdkn/3582516 to your computer and use it in GitHub Desktop.
C++の宿題 年月日を表示します
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <time.h> | |
using namespace std; | |
class Date { | |
private: | |
int year, month, day; | |
public: | |
Date(void); | |
Date(int y, int m = 1, int d = 1); | |
int Year(void) const { return year; } | |
int Month(void) const { return month; } | |
int Day(void) const { return day; } | |
}; | |
Date::Date(void) | |
{ | |
time_t current; | |
struct tm* local; | |
time(¤t); | |
local = localtime(¤t); | |
year = local->tm_year + 1900; | |
month = local->tm_mon + 1; | |
day = local->tm_mday; | |
} | |
Date::Date(int y, int m, int d) | |
{ | |
year = y; month = m; day = d; | |
} | |
ostream& operator<<(ostream& s, const Date& x) | |
{ | |
return (s << setfill('0') | |
<< setw(4) << x.Year() << "年" | |
<< setw(2) << x.Month() << "月" | |
<< setw(2) << x.Day() << "日"); | |
} | |
int main(void) | |
{ | |
Date MyBirthday(1992, 1, 4); | |
cout << "MyBirthday = " << MyBirthday << '\n'; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment