Create a class called date and a child called time in c++
Where to begin?
Easy: FIND SUITABLE LIBRARY
answer is easy, but finding is hard enough.
Hopefully c++ standart has time.h that has functions you need.
Without any talking source code below:
I created 2 files: data.h which has classes and functions and main.cpp :)
-----------------data.h-----------------
#ifndef _DATA_H_
#define _DATA_H_
#include <time.h>
class Data
{
public:
time_t rawtime; // time.h requres this
struct tm *timeinfo; // and this also
public:
Data(); // constructor
int getMonth();
int getDay();
int getYear();
char *getAll();
};
class Time : public Data // child funqcion
{
public:
int getH();
int getM();
int getS();
};
Data::Data() // copy of constructor
{
time(&rawtime);
timeinfo = localtime(&rawtime);
}
int Data::getMonth()
{
return timeinfo->tm_mon;
}
int Data::getDay()
{
return timeinfo->tm_wday;
}
int Data::getYear()
{
return timeinfo->tm_year;
}
char *Data::getAll()
{
return asctime(timeinfo);
}
int Time::getH()
{
return timeinfo->tm_hour;
}
int Time::getM()
{
return timeinfo->tm_min;
}
int Time::getS()
{
return timeinfo->tm_sec;
}
#endif
----------------------END OF FILE--------------------
I hope you know include file rule when working with multiple files. I mean you understand #ifndef and #define...
and the main.cpp:
#include "data.h"
#include <iostream>
//I hope you understand all this...
using namespace std;
Data myData;
Time myTime;
const char *months[12] =
{
"January",
"Febryary",
"March",
"April",
"May",
"June",
"Jule",
"August",
"September",
"October",
"November",
"December",
};
const char *days[7] =
{
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
};
int gmonth;
int gday;
int gyear;
int ghour;
int gmin;
int gsec;
char *compact; // why char *compact and not char compact? because Data.getAll returns *char
int main(int argc, char *argv[])
{
cout << "Getting month...\n";
gmonth = myData.getMonth();
cout << "Current month: " << months[gmonth];
cout << endl;
cout << "Getting week day...\n";
gday = myData.getDay();
cout << "Current week day: " << days[gday];
cout << endl;
cout << "Getting year...\n";
gyear = myData.getYear() + 1900; // +1900 is necessary :) because year is calculating after 1900
cout << "Current year: " << gyear;
cout << endl;
cout << "Getting hour...\n";
ghour = myTime.getH();
cout << "Current hour: " << ghour;
cout << endl;
cout << "Getting minutes...\n";
gmin = myTime.getM();
cout << "Current minutes: " << gmin;
cout << endl;
cout << "Getting seconds...\n";
gsec = myTime.getS();
cout << "Current seconds: " << gsec;
cout << endl;
cout << endl;
compact = myData.getAll(); // Coolest one :)
cout << compact << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
----------------END OF FILE--------------------
I clearly don't understand what set hours, minutes, secs mean:'
Tested and working.
If you have any questions write comment.
I compiled this project in DevCpp, but it will work for Visual Studio too.
This might be different what you expected but solve any problem AS EASY AS POSSIBLE in programming. Solve problem i mean everything is working.
Aah, I've written a lot :)
Please rate my solution...
And Successful Programming!
×