Created
September 2, 2012 14:36
-
-
Save tdkn/3599655 to your computer and use it in GitHub Desktop.
C++の宿題 現在の年齢を返すメンバ関数Age
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 <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