In C++, Write down the output of the program. TimeInstance::TimeInstance(int iHo
ID: 3574110 • Letter: I
Question
In C++, Write down the output of the program.
TimeInstance::TimeInstance(int iHour, int iMinute, int iSecond) {
if (iHour < 0 || iHour > 23) {
exit(-1);
}
if (iMinute < 0 || iMinute > 59) {
exit(-1);
}
if (iSecond < 0 || iSecond > 59) {
exit(-1);
}
hour = iHour;
minute = iMinute;
second = iSecond;
}
void TimeInstance::displayTime(bool system24, int gmtOffset) {
if (gmtOffset > 12 || gmtOffset < -12) {
cout << "Invalid GMT offset" << endl;
}
else {
int tmp = (hour + gmtOffset + 24) % 24;
if (system24) {
cout << tmp << ":" << minute << ":" << second << endl;
}
else if (tmp == 12) {
cout << 12 << ":" << minute << ":" << second << " PM" << endl;
}
else if (tmp > 12) {
tmp -= 12;
cout << tmp << ":" << minute << ":" << second << " PM" << endl;
}
else {
cout << tmp << ":" << minute << ":" << second << " AM" << endl;
}
}
}
int main() {
TimeInstance t(0, 0, 0);
t.displayTime(true, 0);
t.displayTime(false, 4);
t.displayTime(false, -13);
TimeInstance u(4, 6, 30);
u.displayTime(true, -1);
u.displayTime(false, -7);
u.displayTime(true, 11);
return 0;
}
Explanation / Answer
output for the program
0:0:0
4:0:0 AM
Invalid GMT offset
3:6:30
9:6:30 PM
15:6:30
: