Question: Given the following header and source codes: header code: #include using namespace std; class clockType { public: void setTime(int hours, int minutes, int seconds); //Function
Given the following header and source codes:
header code:
#includeusing namespace std; class clockType { public: void setTime(int hours, int minutes, int seconds); //Function to set the time //The time is set according to the parameters //Postcondition: hr = hours; min = minutes; sec = seconds // The function checks whether the values of hours, // minutes, and seconds are valid. If a value is invalid, // the default value 0 is assigned. void getTime(int& hours, int& minutes, int& seconds) const; //Function to return the time //Postcondition: hours = hr; minutes = min; seconds = sec void printTime() const; //Function to print the time //Postcondition: Time is printed in the form hh:mm:ss. void incrementSeconds(); //Function to increment the time by one second //Postcondition: The time is incremented by one second. // If the before-increment time is 23:59:59, the time // is reset to 00:00:00. void incrementMinutes(); //Function to increment the time by one minute //Postcondition: The time is incremented by one minute. // If the before-increment time is 23:59:53, the time // is reset to 00:00:53. void incrementHours(); //Function to increment the time by one hour //Postcondition: The time is incremented by one hour. // If the before-increment time is 23:45:53, the time // is reset to 00:45:53. clockType(int hours, int minutes, int seconds); //Constructor with parameters //The time is set according to the parameters. //Postconditions: hr = hours; min = minutes; sec = seconds // The constructor checks whether the values of hours, // minutes, and seconds are valid. If a value is invalid, // the default value 0 is assigned. clockType(); //Default constructor with parameters //The time is set to 00:00:00. //Postcondition: hr = 0; min = 0; sec = 0 private: int hr; //variable to store the hours int min; //variable to store the minutes int sec; //variable to store the seconds };
Source Codes
#include#include "clockType.h" using namespace std; clockType::clockType() //default constructor { hr = 0; min = 0; sec = 0; } clockType::clockType(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if (0 <= seconds && seconds < 60) sec = seconds; else sec = 0; } void clockType::printTime() const { if (hr < 10) cout << "0"; cout << hr << ":"; if (min < 10) cout << "0"; cout << min << ":"; if (sec < 10) cout << "0"; cout << sec; } void clockType::setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if (0 <= seconds && seconds < 60) sec = seconds; else sec = 0; } void clockType::getTime(int& hours, int& minutes, int& seconds) const { hours = hr; minutes = min; seconds = sec; } void clockType::incrementHours() { hr++; if(hr > 23) hr = 0; } void clockType::incrementMinutes() { min++; if (min > 59) { min = 0; incrementHours(); } } void clockType::incrementSeconds() { sec++; if (sec > 59) { sec = 0; incrementMinutes(); } }
#include#include "clockType.h" using namespace std; int main() { clockType myClock(); clockType yourClock(26, 72, 1); yourClock.printTime();//printTime(yourClock) cout << endl; /*myClock.addClock(thirdClock); myClock.printTime(); cout << endl; myClock.addClock(someClock); myClock.printTime(); cout << endl; cout< modify the code such that when the user enters the time 26, 72, and 1 (or 26:72:01) it automatically returns it with an output of 02:12:01, essentially making it go the next day.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
