0% found this document useful (0 votes)
22 views3 pages

Q3 Solution

The document defines a time class with hours, minutes and seconds data members. It overloads the + operator to add two time objects and returns a new time object. It also has a check method to handle overflow when seconds or minutes exceed 59.

Uploaded by

johnyrock05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Q3 Solution

The document defines a time class with hours, minutes and seconds data members. It overloads the + operator to add two time objects and returns a new time object. It also has a check method to handle overflow when seconds or minutes exceed 59.

Uploaded by

johnyrock05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Q3 solution:

#include<iostream>
#include<conio.h>
using namespace std;

class time
{
int hours, minutes, seconds;
public:
time(): hours(0), minutes(0), seconds(0) {}
time(int h, int m, int s): hours(h), minutes(m), seconds(s) {}
void display()
{
cout<<hours<<":"<<minutes<<":"<<seconds;
}
void check()
{
if(seconds>59) {seconds-=59; minutes++;}
if(minutes>59) {minutes-=59; hours++;}
}
time operator + (time);
};

time time::operator +(time t2)

{
int i= hours+ t2.hours;
int j= minutes+ t2.minutes;
int k= seconds+ t2.seconds;
return time(i,j,k);
}
void main(void)
{
time t0(12, 34, 4), t1(3, 34, 45);
time t3;
do
{
t3 = t1+t0;
t3.check();
t3.display();
cout<<"\n\n !Press c to continue or any key to
exit."<<endl<<endl;
}
While
(getch()=='c');
}

Output:

You might also like