

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Q1. (Marks 5) Create a class called time that has separate int member data for hours, minutes, and seconds. One constructor should initialize this data to 0, and another constructor should initialize it to fixed values. Make void print() to display time in 23:59:59 format. Make void setTime(int,int,int) to set hour, minute, second. include a tick() member function that increments the time stored in a Time object by one second. Make an add member function that should add two objects of type time passed as arguments. Be sure to test the following cases: Incrementing into the next minute. Incrementing into the next hour. Incrementing into the next day (i.e., 23:59:59 to 00:00:00). Make 1000 times loop in a main function. Call tick and print functions in that loop for an object. Also make two objects and add them to a third object and print their values. Answer #include
void Time::setSecond(int s) { second = (s >= 0 && s < 60)? s : 0; } int Time::getHour() { return hour; } int Time::getMinute() { return minute; } int Time::getSecond() { return second; } void Time::print() { if(hour<10) cout<<"0"; cout<<hour<<":"; if(minute<10) cout<<"0"; cout<<minute<<":"; if(second<10) cout<<"0"; cout<<second<<endl; } void Time::Tick() { second++; if (second == 60) { second = 0; minute++; } if (minute >= 60) { minute = 0; hour++; } } int main () { Time t; t.setTime(11,59,59); t.print(); for(int i=0; i<=1000; i++) { t.Tick(); t.print(); }