Answer to Question #277016 in C++ for Rao

Question #277016

*Write a C++ program having class time that has 3 private data members (h, m, s) to store, hours, minutes and seconds; 3 public member functions to (i) set time, (ii) print universal time, and (iii) print standard time.*



Note: Initialize object to zero, using member initializers.

1
Expert's answer
2021-12-08T03:06:45-0500
#include<iostream>
#include<ctime>
#include<iomanip>
using namespace std;

class Time
{
	int hours;
	int minutes;
	int seconds;
public:
	Time():hours(0),minutes(0),seconds(0){}
	void setTime()
	{
		time_t rawtime;
		struct tm* timeinfo;
		time(&rawtime);
		timeinfo=localtime(&rawtime);
		hours=timeinfo->tm_hour;
		minutes=timeinfo->tm_min;
		seconds=timeinfo->tm_sec;
	}
	void printUniversalTime()
	{
		cout<<"\nUniversal time:\t";
		cout<<setfill('0')<<setw(2)<<hours<<":"
		<<setw(2)<<minutes<<":"<<setw(2)<<seconds;
	}
	
	void printStandartTime()
	{
		cout<<"\nStandart time:\t";
		cout<<((hours==0||hours==12) ? 12 : hours%12)<<":"<<setfill('0')
		<<setw(2)<<minutes<<":"<<setw(2)<<seconds<<(hours<12 ? "AM":"PM");
	}
};

int main()
{
	Time a;
	a.setTime();
	a.printUniversalTime();
	a.printStandartTime();
}

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