Question
C++ programming Extend the attached ClockType class to include an addClock function that adds another ClockType's time (passed as an argument) to a ClockType .
C++ programming
Extend the attached ClockType class to include an addClock function that adds another ClockType's time (passed as an argument) to a ClockType . The prototype of the function should be void addClock(clockType c);
here is the program and what i`ve done so far
header file:
class clockType { private: int hr; //variable to store the hours int min; //variable to store the minutes int sec; //variable to store the seconds
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.
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
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. /* void incrementHours(int hrs); void incrementMinutes(int mins); void incrementSeconds(int secs); */ bool equalTime(const clockType& otherClock) const; //Function to compare the two times //Postcondition: Returns true if this time is equal to // otherClock; otherwise, returns false
void decrementSeconds(); void decrementMinutes(); void decrementHours();
void copyTime(); void addClock(clockType otherClock); };
main file: #include #include \"header.h\"
using namespace std;
int main() {
int hours; int minutes; int seconds;
clockType myClock(10, 25, 10); cout myClock.printTime(); cout
myClock.incrementSeconds(); myClock.incrementMinutes(); myClock.incrementHours();
cout myClock.printTime(); cout
myClock.decrementSeconds(); myClock.decrementMinutes(); myClock.decrementHours();
cout myClock.printTime(); cout
clockType yourClock(1, 5,25); cout yourClock.printTime(); cout yourClock.incrementSeconds(); yourClock.incrementMinutes(); yourClock.incrementHours();
cout yourClock.printTime(); cout
yourClock.decrementSeconds(); yourClock.decrementMinutes(); yourClock.decrementHours();
cout yourClock.printTime(); cout // Compare myClock and yourClock if ( myClock.equalTime ( yourClock ) ) cout else cout
myClock= yourClock;
cout myClock.printTime(); cout
/* myClock.addClock(yourClock); myClock.printTime(); cout */
myClock.addClock(yourClock); cout myClock.setTime ( hours, minutes, seconds ) ; myClock.printTime(); cout /* cout cin >> hours >> minutes >> seconds ; cout //Set the time of myClock using the value of the variables hours, minutes, seconds myClock.setTime ( hours, minutes, seconds ) ; cout myClock.printTime ( ) ; cout
*/
/* myClock.setTime(12, 38, 22); myClock.printTime(); cout yourClock.setTime(13, 22,38); yourClock.printTime(); cout;*> system(\"PAUSE\"); return 0; }//end main
cpp:
#include #include \"header.h\"
using namespace std;
void clockType::setTime(int hours, int minutes, int seconds) { if (0 => hr = hours; else hr = 0;
if (0 => min = minutes; else min = 0;
if (0 => sec = seconds; else sec = 0; }
void clockType::getTime(int hours, int minutes, int seconds) const { hours = hr; minutes = min; seconds = sec; }
void clockType::printTime() const { if (hr cout cout
if (min cout cout
if (sec cout cout } clockType::clockType(int hours, int minutes, int seconds) { setTime(hours, minutes , seconds); if (0 => hr = hours; else hr = 0;
if (0 => min = minutes; else min = 0;
if (0 => sec = seconds; else sec = 0; }
clockType::clockType() //default constructor { hr = 0; min = 0; sec = 0; }
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(); } }
void clockType::decrementSeconds() { sec--; if (sec == 60) { sec=0; &clockType::decrementMinutes; } } void clockType::decrementMinutes() { min --; if ( min == 60) { min=0; &clockType::decrementHours; } } void clockType::decrementHours() { hr --; if ( hr == 24) hr=0; }
bool clockType::equalTime(const clockType& otherClock) const { return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec); } void clockType::addClock(clockType otherClock) { int hours; int minutes; int seconds;
seconds=sec+otherClock.sec; minutes=min+otherClock.min; hours=hr+otherClock.hr;
sec=seconds; min=minutes; hr=hours;
if (hr> 24) { hr-=24; }
if (min>60) { hr++; min-=60; //minutes=min; //hr=min/60; //min=min%60; }
if (sec >60) { min++; sec-=60; //seconds = sec; //min=sec/60; // sec=sec%60; }
//hours = hr; //minutes = min; //seconds = sec; }
/*void clockType::copyTime() { myClock= yourClock; }
/* void clockType::incrementHours(int hrs) { hr += hrs; hr = hr % 24; }
void clockType::incrementMinutes(int mins) { min += mins; int hrs = (int) min/60; min = min % 60; incrementHours(hrs); }
void clockType::incrementSeconds(int secs) { sec += secs; int mins = (int) sec/60; sec = sec % 60; //cout\"> incrementMinutes(mins); } */
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started