Question
Write a c++ program that extends the Time class from the lab (that means all the functionality from the lab exercise should remain). - extends
Write a c++ program that extends the Time class from the lab (that means all the functionality from the lab exercise should remain).
- extends Time class to include:
- parameterized constructor
- getter for each of the data members
- member function compare (3 arguments) returns 0 (false) or 1 (true)
- member function compare (1 argument) returns 0 (false) or 1 (true)
- overload operator == to return 1 if Time objects represent the same time
- in main(), use the class
- instantiate 2 objects of class Time, initialize to different times (1)
- make use of parameterized constructor
- no prompting necessary
- use overloaded operator== to display if objects are the same or not
- use getters on object1, store results locally (2)
- use results step (2) as arguments to call compare using object2 (3)
- display if the times were equal or not (4)
- call compare using object1 with object 2 as an argument (5)
- verify and display if times were equal or not (6)
- assign object2 to object1 and perform steps (2) through (6) again
And this is the codes from the Time lab
#include
using namespace std;
class Time
{
private:
int hours;
int minutes;
int seconds;
public:
Time();
void setHours(int hrs);
void setMinutes(int mnts);
void setSeconds(int secs);
void display24format();
void display12format();
};
Time::Time()
{
hours = minutes = seconds = 0;
}
void Time::setHours(int hrs)
{
hours = hrs;
};
void Time::setMinutes(int mnts)
{
minutes = mnts;
}
void Time::setSeconds(int secs)
{
seconds = secs;
}
void Time::display24format()
{
cout << hours << " : " << minutes << " : " << seconds << " ";
}
void Time::display12format()
{
if (hours > 12)
{
hours = hours - 12;
cout << hours << " : " << minutes << " : " << seconds << "pm" << endl;
}
else if (hours == 12)
{
hours = 12;
cout << hours << " : " << minutes << " : " << seconds << "pm" << endl;
}
else if (hours == 0)
{
hours = 12;
cout << hours << " : " << minutes << " : " << seconds << "AM" << endl;
}
else
cout << hours << " : " << minutes << " : " << seconds << "am" << endl;
}
int main()
{
Time t;
int hours;
int minutes;
int seconds;
cin >> hours;
cin >> minutes;
cin >> seconds;
t.setHours(hours);
t.setMinutes(minutes);
t.setSeconds(seconds);
t.display24format();
t.display12format();
return 0;
}
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