Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Statement The following class Clock implements some basic operations on

ID: 3567662 • Letter: P

Question

Problem Statement
The following class Clock implements some basic operations on time. The following shows
the declaration of the class Clock. In this lab assignment, you need to complete the class
definition and write the main function.

class Clock
{
public:
Clock(); // default constructor
Clock(int hh, int mm, int ss); //constructor
//increase time by sec seconds
void incrementSeconds(int sec);
//increase time by min minutes
void incrementMinutes(int min);
//increase time by hh hours,
//if hours reach 24, simply wrap around to 0.
void incrementHours(int hh);
void addTime(Clock C); //add C into the clock
//print time in hours:minutes:seconds am(or pm) format
void printTime();

private:
// Declare data members of the class
// 0 <= hours < 24, 0 <= minutes < 60, 0 <= seconds < 60
int hours, minutes, seconds;

};
If you need to add other member functions to the class Clock, you can.Use the following instructions to write the

main function:
int main()
{
//write each statement for each operation below
(create Clock object C1 with h:m:s = 0:0:0)
(create second Clock object C2 with h:m:s=12:35:59)
(add C2 into C1)
(print C1)
(print C2)
(increase clock C1 by 1 seconds)
(print C1)
(increase clock C1 by 100 minutes)
(print C1)
(increase clock C1 by 10 hours)
(print C1)
(print C2)
}
Here is the sample output of running your program:
12 : 35 : 59 PM
12 : 35 : 59 PM
12 : 36 : 0 PM
2 : 16 : 0 PM
12 : 16 : 0 AM
12 : 35 : 59 PM
Press ENTER to continue.
(Note: if the hours of the clock reach 24, simply wrap around
to 0; if the hours of the clock is 0, it should display 12am;
if the hours of the clock is 12, it should display 12pm)

Explanation / Answer

#include <iostream>

using namespace std;

class Clock
{
private:
// Declare data members of the class
// 0 <= hours < 24, 0 <= minutes < 60, 0 <= seconds < 60
int hours, minutes, seconds;


public:
Clock(); // default constructor
Clock(int hh, int mm, int ss) //constructor
{
hours = hh;
minutes = mm;
seconds = ss;
}
//increase time by sec seconds
void incrementSeconds(int sec)
{
seconds += sec;
  
int m = seconds / 60;
seconds = seconds % 60;
  
incrementMinutes(m);
}
//increase time by min minutes
void incrementMinutes(int min)
{
minutes += min;
  
int h = minutes / 60;
minutes = minutes % 60;
  
incrementHours(h);
}
//increase time by hh hours,
//if hours reach 24, simply wrap around to 0.
void incrementHours(int hh)
{
hours += hh;
  
hours = hours % 24;
}
void addTime(Clock C) //add C into the clock
{
hours = C.hours;
minutes = C.minutes;
seconds = C.seconds;
}
//print time in hours:minutes:seconds am(or pm) format
void printTime()
{
if(hours >= 12 && minutes >= 0 && seconds >= 0)
{
if(hours == 12)
cout<<(hours)<<" : "<<minutes<<" : "<<seconds<<" pm ";
else
cout<<(hours-12)<<" : "<<minutes<<" : "<<seconds<<" pm ";
  
}
else
{
if(hours == 0)
cout<<(hours + 12)<<" : "<<minutes<<" : "<<seconds<<" am ";
else
cout<<hours<<" : "<<minutes<<" : "<<seconds<<" am ";
}
  
}

};

int main()
{
Clock C1 = Clock(0, 0, 0);
Clock C2 = Clock(12, 35, 59);

C1.addTime(C2);

C1.printTime();
C2.printTime();

C1.incrementSeconds(1);
C1.printTime();

C1.incrementMinutes(100);
C1.printTime();

C1.incrementHours(10);
C1.printTime();
C2.printTime();
}

------------

output

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote