Please write the program in C++ language 1. Complete the implementation of the c
ID: 3750170 • Letter: P
Question
Please write the program in C++ language
1. Complete the implementation of the class time24 as declared in the program time24. cpp Do not modify the main program. You must write the four methods of the class and the two functions write) and add) Notes: The attributes of time24 are hour and minute This allows us to use the names hour and minute for the methods. Instead of naming the methods sethour, setminute, gethour and getminute we use overloading of the method names hour and minute . To print an integerExplanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <fstream>
using namespace std;
class time24{
private:
int hour_;
int minute_;
public:
int hour(void) const;
int minute(void) const;
void hour(int h);
void minute(int m);
};
void write(ostream &out, const time24 &x);
time24 add(const time24 &x, const time24 &y);
int main()
{
time24 x, y, z;
x.hour(18);
x.minute(34);
y.hour(10);
y.minute(56);
cout << "The time x is: ";
write(cout, x);
cout << " The time y is: ";
write(cout, y);
z = add(x, y);
cout << " The sum of ";
write(cout, x);
cout << " and ";
write(cout, y);
cout << " is ";
write(cout , z);
cout << endl;
return 0;
}
//////////// Method of time24 ///////////////
int time24::hour(void) const{
return hour_;
}
int time24::minute(void) const{
return minute_;
}
void time24::hour(int h){
hour_ = h;
}
void time24::minute(int m){
minute_ = m;
}
/////////// time24 functions///////////////
void write(ostream &out, const time24 &x){
out << x.hour() / 10 << x.hour() % 10;
out << ":";
out << x.minute() / 10 << x.minute() % 10;
}
time24 add(const time24 &x, const time24 &y){
time24 z;
int h = x.hour() + y.hour();
int m = x.minute() + y.minute();
if(m >= 60){
m -= 60;
h++;
}
if(h >= 24)
h -= 24;
z.hour(h);
z.minute(m);
return z;
}
output
-----
The time x is: 18:34
The time y is: 10:56
The sum of 18:34 and 10:56 is 05:30