Skip to content

Instantly share code, notes, and snippets.

@tdkn
Created September 1, 2012 18:28
Show Gist options
  • Save tdkn/3582516 to your computer and use it in GitHub Desktop.
Save tdkn/3582516 to your computer and use it in GitHub Desktop.
C++の宿題 年月日を表示します
#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(&current);
local = localtime(&current);
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