Create a class time24, each object is a value represented the time of day in the form hours, minutes and seconds.Provide a constructor that enables an object of this class to be initialized when it is instantiated. The constructor should contain default values in case no initializes are provided.Provide Public member functions for each of the following operations: set the time, print the time, increment the time by one second,increment the time by one hour,compare two times for equality, determine if one time is “less than” (comes before) another time, and final print the time in format 12 hours.Include any additional operations that you think would be useful for your class.Design,implement, and test your class. Using C++
Create a class time24, each object is a value represented the time of day in the form hours, minutes and seconds.Provide a constructor that enables an object of this class to be initialized when it is instantiated. The constructor should contain default values in case no initializes are provided.Provide Public member functions for each of the following operations: set the time, print the time, increment the time by one second,increment the time by one hour,compare two times for equality, determine if one time is “less than” (comes before) another time, and final print the time in format 12 hours.Include any additional operations that you think would be useful for your class.Design,implement, and test your class.
Using C++
Program:
#include <iostream>
using namespace std;
class time24 {
private:
/* data */
int hours;
int minutes;
int seconds;
public:
/* functions */
time24()
{
hours = 0;
minutes = 0;
seconds = 0;
}
time24(int hours, int minutes, int seconds)
{
this->hours = hours;
this->minutes = minutes;
this->seconds = seconds;
}
void setTime(int h, int m, int s)
{
hours = h;
minutes = m;
if (minutes >= 60)
{
hours = hours + 1;
minutes = minutes - 60;
}
seconds = s;
if (seconds > 59)
{
minutes = minutes + 1;
seconds = seconds - 60;
}
}
void printTime()
{
cout << "\n Printing time in 12-hour format only";
if (hours <= 12)
{
cout << "\n hours:minutes:seconds \n";
cout << " " << hours << "\t" << minutes << "\t" << seconds;
}
else if (hours > 12)
{
cout << "\n hours:minutes:seconds \n";
cout << " " << (hours - 12) << "\t" << minutes << "\t" << seconds;
}
}
int compare(time24& t)
{
if (hours < t.hours)
return -1;
else if (hours > t.hours)
return 1;
if (minutes < t.minutes)
return -1;
else if (minutes > t.minutes)
return 1;
if (seconds < t.seconds)
return 1;
else if (seconds > t.seconds)
return -1;
else
return 0;
}
void incrementSeconds()
{
if (seconds < 60)
{
seconds = seconds + 1;
}
if (seconds >= 60)
{
minutes = minutes + 1;
seconds = 0;
}
if (minutes >= 60)
{
if (seconds >= 60)
{
minutes = minutes + 1;
seconds = 0;
}
else
{
hours = hours + 1;
minutes = 0;
seconds = seconds;
}
}
}
void incrementHours()
{
hours = hours + 1;
}
void incrementMinutes()
{
if (minutes < 60)
{
minutes = minutes + 1;
}
else if (minutes >= 60)
{
if (seconds >= 60)
{
minutes = minutes + 1;
seconds = 0;
}
else
{
hours = hours + 1;
minutes = 0;
seconds = seconds;
}
}
}
};
int main()
{
time24 t1;
time24 t2(12, 10, 18);
int res;
cout << "\n Initial printing of time from 3 distinct objects";
t1.printTime();
t2.printTime();
cout << "\n \n Setting time for object 1 as following: \n";
cout << " Hours: 18 \n Minutes: 64 \n Seconds: 32 \n";
t1.setTime(18, 64, 32);
t1.printTime();
cout << "\n \n Comparing time between object 1 and object 2";
cout << " and printing the greater time \n";
res = t1.compare(t2);
if (res == -1)
t2.printTime();
else if (res == 1)
t1.printTime();
cout << "\n Incrementing minutes and seconds for object 2 \n";
t2.incrementMinutes();
t2.incrementSeconds();
// Printing time for object 2
t2.printTime();
cout << "\n Incrementing minutes and seconds for object 1 \n";
t1.incrementHours();
// Printing time for object 1
t1.printTime();
return -1;
}
Step by step
Solved in 2 steps with 1 images