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.
#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;
}
Comments
Leave a comment