Answer to Question #185098 in Programming & Computer Science for khubaib

Question #185098

Create a time class having data members hour, minute and second. Make a constructor to set the members zero, make a display function that outputs the time in given format: 00:00:00. Make a function called ticktick() that increments the time in such a way that hour do not change till the minutes and seconds ends at 60. (01:01:01…. 01:59:59 -> 02:00:00). Design the main function, create an object and call the functions accordingly. 



1
Expert's answer
2021-04-26T02:53:30-0400
#include <iostream>
#include <iomanip>
using namespace std;


class Time{
private:
	// data members hour, minute and second. 
	int hour;
	int minute;
	int second;
public:
	// a constructor to set the members zero
	Time(){
		this->hour=0;
		this->minute=0;
		this->second=0;
	}
	//make a display function that outputs the time in given format: 00:00:00. 
	void display(){
		cout<<setw(2) << setfill('0')<<this->hour<<":"<<setw(2) << setfill('0')<<this->minute<<":"<<setw(2) << setfill('0')<<this->second<<"\n";
	}
	//Make a function called ticktick() that increments the time in such a way that 
	//hour do not change till the minutes and seconds ends at 60. (01:01:01…. 01:59:59 -> 02:00:00).
	void ticktick(){
		this->second++;
		if(this->second>=60){
			this->second=0;
			this->minute++;
			if(this->minute>=60){
				this->minute=0;
				this->hour++;
				if(this->hour>=12){
					this->second=0;
					this->minute=0;
					this->hour=1;
				}
			}
		}
	}
	
};


int main(){  
	//Design the main function, create an object and call the functions accordingly. 
	Time time;
	time.display();
	for(int i=0;i<20000;i++){
		time.ticktick();	
	}
	time.display();


	system("pause");
	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