Create a class called 'TIME' that has
a. three integer data members for hours, minutes and seconds
b. constructor to initialize the object to zero
c. constructor to initialize the object to some constant value
d. member function to add two TIME objects
e. 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.


Program Code:
#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();
cout<<"\nTime 1 + Time 2 is :";
t3.display();
getch();
return(0);
}





Output of the above program is:
Time 1 is :10:48:30
Time 2 is :2:22:35
Time 1 + Time 2 is :13:11:5


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: