In C++ update the program below to include... - member function getTime (3 value
ID: 644562 • Letter: I
Question
In C++ update the program below to include...
- member function getTime (3 values are
In C++ update the program below to include... - member function getTime (3 values are ??returned?? from the call: h,m,s) - member function compare (3 parameters) returns 0 (false) or 1 (true) - in main(), use the class - instantiate 2 objects of class Time, initialize to different times (1) - call getTime on object1, store results locally (2) - use results from getTime as arguments to call compare on object2 (3) - let the user know if the times were equal or not (4) - copy object2 into object1 and perform steps (2) through (4) againExplanation / Answer
#include <iostream>
using namespace std;
struct retValue{
int h;
int m;
int s;
};
class Time{
int h, m, s;
public:
void setClock(int hour, int min, int sec);
void display24();
void display12(string &m);
retValue getTime();
bool compare(int hour, int min, int sec);
};
void Time::setClock(int hour, int min, int sec){
h = hour;
m = min;
s = sec;
}
void Time::display24(){
cout << "The time in (24 hour) format: " << h << ":" << m << ":" << s << endl;
}
void Time::display12(string &m){
// fill it yourself
}
retValue Time::getTime(){
retValue r;
r.h = h;
r.m = m;
r.s = s;
return r;
}
bool Time::compare(int hour, int min, int sec){
return hour == h && min == m && sec == s;
}
int main(){
Time t1, t2;
t1.setClock(3, 34, 25);
t2.setClock(5, 12, 52);
t1.display24();
t2.display24();
retValue r = t1.getTime();
if(t2.compare(r.h, r.m, r.s)){
cout << "Both times are equal" << endl;
}
else{
cout << "Both times are not equal" << endl;
}
r = t2.getTime();
t1.setClock(r.h, r.m, r.s);
r = t1.getTime();
if(t2.compare(r.h, r.m, r.s)){
cout << "Both times are equal" << endl;
}
else{
cout << "Both times are not equal" << endl;
}
return 0;
}