Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <sstream>
- #include <ctime>
- using namespace std;
- class Date {
- protected:
- int day, month, year;
- //constructors
- public:
- Date() {
- this->day = 1;
- this->month = 1;
- this->year = 1900;
- }
- Date(int day, int month, int year) {
- this->day = day;
- this->month = month;
- this->year = year;
- }
- //getters
- public:
- int getDay() {
- return day;
- }
- int getMonth() {
- return month;
- }
- int getYear() {
- return year;
- }
- };
- class Time {
- protected:
- int second, minute, hour;
- //constructors
- public:
- Time() {
- this->second = 0;
- this->minute = 0;
- this->hour = 0;
- }
- Time(int second, int minute, int hour) {
- this->second = second;
- this->minute = minute;
- this->hour = hour;
- }
- //getters
- public:
- int getSecond() {
- return second;
- }
- int getMinute() {
- return minute;
- }
- int getHour() {
- return hour;
- }
- };
- class DateTime : public Date, public Time {
- //constructors
- public:
- DateTime() : Date(), Time() {}
- DateTime(int day, int month, int year, int second, int minute, int hour) : Date(day, month, year), Time(second, minute, hour) {}
- //methods
- public:
- string toString() {
- stringstream ss;
- ss << ((day < 10) ? "0" : "") << day << "/";
- ss << ((month < 10) ? "0" : "") << month << "/";
- ss << year << " ";
- ss << ((hour < 10) ? "0" : "") << hour << ":";
- ss << ((minute < 10) ? "0" : "") << minute << ":";
- ss << ((second < 10) ? "0" : "") << second;
- return ss.str();
- }
- };
- int main() {
- DateTime dt;
- cout << dt.toString() <<endl <<endl;
- time_t t = time(0);
- tm* now = localtime(&t);
- DateTime dtN(now->tm_mday, (now->tm_mon+1), (now->tm_year+1900), now->tm_sec, now->tm_min, now->tm_hour);
- cout << dtN.toString() <<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment