Arsylk

Narkoman Company

Apr 18th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <sstream>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. class Date {
  9.   protected:
  10.     int day, month, year;
  11.  
  12.   //constructors
  13.   public:
  14.     Date() {
  15.       this->day = 1;
  16.       this->month = 1;
  17.       this->year = 1900;
  18.     }
  19.     Date(int day, int month, int year) {
  20.       this->day = day;
  21.       this->month = month;
  22.       this->year = year;
  23.     }
  24.  
  25.   //getters
  26.   public:
  27.     int getDay() {
  28.       return day;
  29.     }
  30.     int getMonth() {
  31.       return month;
  32.     }
  33.     int getYear() {
  34.       return year;
  35.     }
  36. };
  37.  
  38. class Time {
  39.   protected:
  40.     int second, minute, hour;
  41.  
  42.   //constructors
  43.   public:
  44.     Time() {
  45.       this->second = 0;
  46.       this->minute = 0;
  47.       this->hour = 0;
  48.     }
  49.     Time(int second, int minute, int hour) {
  50.       this->second = second;
  51.       this->minute = minute;
  52.       this->hour = hour;
  53.     }
  54.  
  55.   //getters
  56.   public:
  57.     int getSecond() {
  58.       return second;
  59.     }
  60.     int getMinute() {
  61.       return minute;
  62.     }
  63.     int getHour() {
  64.       return hour;
  65.     }
  66. };
  67.  
  68. class DateTime : public Date, public Time  {
  69.   //constructors
  70.   public:
  71.     DateTime() : Date(), Time() {}
  72.     DateTime(int day, int month, int year, int second, int minute, int hour) : Date(day, month, year), Time(second, minute, hour) {}
  73.  
  74.   //methods
  75.   public:
  76.     string toString() {
  77.       stringstream ss;
  78.       ss << ((day < 10) ? "0" : "") << day << "/";
  79.       ss << ((month < 10) ? "0" : "") << month << "/";
  80.       ss << year << " ";
  81.  
  82.       ss << ((hour < 10) ? "0" : "") << hour << ":";
  83.       ss << ((minute < 10) ? "0" : "") << minute << ":";
  84.       ss << ((second < 10) ? "0" : "") << second;
  85.       return ss.str();
  86.     }
  87. };
  88.  
  89.  
  90. int main() {
  91.   DateTime dt;
  92.   cout << dt.toString() <<endl <<endl;
  93.  
  94.   time_t t = time(0);
  95.   tm* now = localtime(&t);
  96.   DateTime dtN(now->tm_mday, (now->tm_mon+1), (now->tm_year+1900), now->tm_sec, now->tm_min, now->tm_hour);
  97.   cout << dtN.toString() <<endl;
  98.  
  99.   return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment