Answer to Question #86145 in C++ for Arslan Habib

Question #86145
Create a class called time that has separate int member data for hours, minutes, and seconds. One constructor should initialize this data to 0, and another should initialize it to fixed values. Another member function should display it, in 11:59:59 format. The final member function should add two objects of type time passed as arguments.

A main( ) program should create two initialized time objects and one that isn’t initialized. Then it should add the two initialized values together, leaving the result in the third time variable. Finally it should display the value of this third variable.
1
Expert's answer
2019-03-15T02:38:45-0400
#include <iostream>

using namespace std;

class time {
private:
  int hrs; //HOURS
  int mins; //MINUTES
  int secs; //SECONDS
public:
  time(): hrs(0), mins(0), secs(0)
  { }
  time(int h, int m, int s): hrs(h), mins(m), secs(s)
  { }
  void showtime();
  time sumtime(time, time);
};
void time::showtime() {
  cout << hrs <<":"<< mins <<":"<< secs << endl;
}

time time::sumtime(time tt1, time tt2) {
time tt;
  tt.secs = (tt1.secs + tt2.secs) % 60;
int temp;
  temp = (tt1.secs + tt2.secs) / 60;
tt.mins = 0;
  tt.mins = (tt1.mins + tt2.mins + temp) % 60;
tt.hrs = 0;
  temp = (tt1.mins + tt2.mins + temp) / 60;
  tt.hrs = (tt1.hrs + tt2.hrs) + temp;
return tt;
}

int main() {
time t1(1,23,46);
time t2(23,45,34);
time t3;
  t3 = t3.sumtime(t1, t2);
  t3.showtime();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS