You are on page 1of 6

RIPHAH INTERNATIONAL

UNIVERSITY LAHORE
SUBMITTED BY: SYED MUHAMMAD
FAIZAN BUKHARI
SUBMITTED TO: MAM ZARMINA
JAHANGIR
SAP ID # 21532
ASSIGNMENT # 2
Write a class Time which represents time. The class should
have three fields for hours, minutes and seconds.
1. It should have constructor to initialize the hours, minutes
and seconds to zero and another to initialize it to a fixed
value.
2. A method print Time () to print the current time in the
format for example: 11:45:56.
3. Overload the following operators:
 plus, operator (+) (add two-time objects based on 24-hour
clock)
 and < (compare two-time objects)

#include<iostream>
#include<conio.h>
using namespace std;
class time {
int H, M, S;
public:
time() {
H = 0, M = 0, S = 0;
}
time(int H1, int M1, int S1) {
H = H1;
M = M1;
S = S1;
}

void format() {
if (S > 60) {
M++;
S -= 60;
}
if (M > 60) {
++H;
M -= 60;
}

if (H > 24) {
H -= 24;
}
}
time operator +(time t) {
time imag;
imag.H = H + t.H;
imag.M = M + t.M;
imag.S = S + t.S;
return imag;
}
int operator <(time t) {
if (H < t.H && M < t.M && S < t.S) {
t.print_time();
cout << " is > then " << endl;
print_time();
}
else {
print_time();
cout << " is > then " << endl;
t.print_time();
}
return 0;
getch ();

}
void print_time() {
cout << "Time: " << H << ":" << M << ":" << S << endl;
}

};
int main()
{
int H1, M1, S1;
cout << "Enter hours :";
cin >> H1;
cout << "Enter min :";
cin >> M1;
cout << "Enter sec :";
cin >> S1;
time t(H1, M1, S1);
t.print_time();
cout << "Enter hours :";
cin >> H1;
cout << "Enter min :";
cin >> M1;
cout << "Enter sec :";
cin >> S1;
time t2(H1, M1, S1);
t2.print_time();
time t3;
t3 = t.operator+(t2);
t3.format();
t3.print_time();
t.operator<(t2);
system("pause");
return 0;
getch ();
}

OUTPUT:

You might also like