If anyone has the time to help me i need help with this code
If anyone has the time to help me, i need help with this code, i dont know how to start, Please Help.
In c++. you will be creating a Time class that can display time as a digital clock in both 12-hour mode and 24 -hour mode. Your class should have the following properties:
private integer field for seconds.
private integer field for minutes.
private integer field for hours.
private field for clock mode; that is, a variable to tells if the time will be displayed as a 12-hour clock or a 24-hour clock. private static constant string array field for time periods (am and pm).
public default constructor that sets the time to midnight as a 12-hour clock.
public overloaded constructor that takes three integer parameters for hour, minute and second respectively of a 24-hour clock.
public copy constructor.
public overloaded assignment operator.
public get accessor method for seconds.
public get accessor method for minute.
public get accessor method for hour. It should return hour in 24-hour clock format.
public set accessor method for second. Only accept values between 0 and 59 inclusively; otherwise, throw an exception.
public set accessor method for minute. Only accpet values between 0 and 59 inclusively; otherwise, throw an exception.
public set accessor method for hour. Only accpet values between 0 and 23 inclusively; otherwise, throw an exception.
public set accessor method for clock mode. public void method, tick(), that takes no parameters. It increments the time by a second. Remember time is cyclic.
public overloaded ostream operator. It displays the time as follows:
< hour >:< minute >:< second > if in 24-hour mode
< hour >:< minute >:< second > < time-period > if in 12-hour mode
Note: hour, minute and second have to be represented as a two digit number.
In the main, create two Time objects, one of which must be created by using either the copy constructor or assignment operator. Afterwards, simulate the time passing from a clock for a full day in both clock modes simultaneously on the same line.
Solution
#include<iostream>
using namespace std;
class Time
{
private:
int sec,min,hr;
int mode;
string time_period[2];
public:
Time()
{
sec = 0;
min = 0;
hr = 0;
mode = 24;
time_period[0] = \"am\";
time_period[1] = \"pm\";
}
Time(int sec,int min,int hr)
{
this->sec = sec;
this->min = min;
this->hr = hr;
mode = 24;
time_period[0] = \"am\";
time_period[1] = \"pm\";
}
Time(const Time &time)
{
this->sec = time.sec;
this->min = time.min;
this->hr = time.hr;
}
int getSec()
{
return this->sec;
}
int getMin()
{
return this->min;
}
int getHr()
{
return this->hr;
}
int setSec(int s)
{
if( s>0 && s<=59)
this->sec = s;
else
throw \"Out of bounds Exception!\";
}
int setMin(int m)
{
if( m>0 && m<=59)
this->min = m;
else
throw \"Out of bounds Exception!\";
}
int setHr(int h)
{
if(h>0 && h<=23)
this->hr = h;
else
throw \"Out of bounds Exception!\";
}
int setMode(int mode)
{
if( mode == 12 || mode == 24)
this->mode = mode;
else
throw \"Out of bounds Exception!\";
}
void tick()
{
if(this->sec<59)
this->sec++;
else
{
this->sec = 0;
if(this->min<59)
this->min++;
else
{
this->min = 0;
if(this->hr<23)
this->hr++;
else
this->hr = 0;
}
}
}
friend ostream &operator<<( ostream &output, Time &t)
{
if(t.mode == 24)
output << t.hr << \":\" << t.min << \":\" << t.sec << \" |\";
else
{
if(t.hr<12)
output << t.hr << \":\" << t.min << \":\" << t.sec << \" \" << t.time_period[0]<<\" | \";
else
output << t.hr-12 << \":\" << t.min << \":\" << t.sec << \" \" << t.time_period[1]<<\" |\";
}
return output;
}
};
int main() {
Time t1(11, 10 , 40), t2(5, 11, 56);
while(1)
{
try
{
t1.tick();
t2.tick();
t2.setMode(12);
cout << t1 << t2 << endl;
}
catch(const char* msg)
{
cerr<<msg<<endl;
}
}
return 0;
}


