Create a class
called 'TIME' that has
-
three
integer data members for hours, minutes and seconds
-
constructor
to initialize the object to zero
-
constructor
to initialize the object to some constant value
-
member
function to add two TIME objects
-
member
function to display time in HH:MM:SS format
Write a main function to create two TIME objects, add
them and display the result in HH:MM:SS format.
#include<iostream.h>
#include<conio.h>
class Time
{
int hrs,min,sec;
public: Time()
{
hrs=0;min=0;sec=0;
}
Time(int a,int b,int
c)
{
hrs=a; min=b;sec=c;
}
void addTime(Time tt1,Time
tt2)
{
sec=tt1.sec+tt2.sec;
min=sec/60;
sec=sec%60;
min=min+tt1.min+tt2.min;
hrs=min/60;
min=min%60;
hrs=hrs+tt1.hrs+tt2.hrs;
}
void display()
{
cout<<hrs<<":"<<min<<":"<<sec<<endl;
}
};
int main()
{
clrscr();
Time t1(10,48,30);
Time t2(2,22,35); Time t3; t3.addTime(t1,t2); cout<<"\nTime
1 is :"; t1.display();
cout<<"\nTime 2 is :"; t2.display();
t3.display();
getch();
return(0);
}
***********************OUTPUT******************************
Time 1 is :10:48:30
Time 2 is :2:22:35
Time 1 + Time 2 is :13:11:5