Answer to Question #31969 in C++ for saichander

Question #31969
Write a program to display today date and time using single-level inheritance
1
Expert's answer
2013-06-14T09:12:30-0400
#include <time.h>

class CTime
{
protected:
int tm_sec;
int tm_min;
int tm_hour;

void set_time(int t_hour, int t_min, int t_sec)
{
tm_hour = t_hour;
tm_min = t_min;
tm_sec = t_sec;
}

void display_time()
{
// Print time
printf("%02d:%02d:%02d", tm_hour, tm_min, tm_sec);
}
};

class CDate : public CTime
{
protected:
int tm_year;
int tm_mon;
int tm_mday;

void set_date(int t_year, int t_mon, int t_mday)
{
tm_year = t_year;
tm_mon = t_mon;
tm_mday = t_mday;
}

void display_date()
{
// Print date
printf("%04d-%02d-%02d ", tm_year, tm_mon, tm_mday);
}
};

class CDateTime : public CDate
{
public:
void display_date_time()
{
// Get date and time
time_t seconds = time(NULL);
tm* timeinfo = localtime(&seconds);

// Set time
set_time(timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);

// Set date
set_date(timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday);

// Print data
display_date();

// Print time
display_time();
}
};

int main ()
{
CDateTime dt;
dt.display_date_time();

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS