Skip to content

Instantly share code, notes, and snippets.

@tdkn
Created September 2, 2012 14:36
Show Gist options
  • Save tdkn/3599655 to your computer and use it in GitHub Desktop.
Save tdkn/3599655 to your computer and use it in GitHub Desktop.
C++の宿題 現在の年齢を返すメンバ関数Age
#include <iostream>
#include <iomanip>
#include <string.h>
#include <time.h>
using namespace std;
class Date {
private:
int year;
int month;
int day;
public:
Date(int y = 1, int m = 1, int d = 1) {
year = y; month = m; day = d;
}
int Year(void) const { return (year); }
int Month(void) const { return (month); }
int Day(void) const { return (day); }
};
class Build {
private:
char name[20];
int height;
int weight;
Date birth;
public:
Build(char* n, int y, int m, int d, int h, int w);
char* Name(void) { return (name); }
int Height(void) { return (height); }
int Weight(void) { return (weight); }
int Age(void);
};
Build::Build (char* n, int y, int m, int d, int h, int w) : birth(y, m, d)
{
strncpy(name, n, 20);
height = h;
weight = w;
}
int Build::Age (void)
{
time_t now;
struct tm* local;
time(&now);
local = localtime(&now);
long int age = 0;
age += (local->tm_year + 1900 - birth.Year()) * 10000;
age += (local->tm_mon + 1 - birth.Month()) * 100;
age += local->tm_mday - birth.Day();
return (age/10000);
}
int main(void)
{
char name[20] = "tdkn";
Build man(name, 1992, 9, 2, 173, 65);
cout << man.Age() << "歳\n";
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment