Monday 8 April 2013

C++ Program To Add Two Times



#include<iostream.h>
#include<conio.h>
class time
{
int hr;
int min;
int sec;
public:time()
{
hr=0;
min=0;
sec=0;
}
time(int hh,int mm,int ss)
{
hr=hh;
min=mm;
sec=ss;
}
void display()
{
cout<<hr<<":"<<min<<":"<<sec<<endl;
}
void gettime()
{
cout<<"ENTER THE TIME IN HH-MM-SS FORMAT\n";
cin>>hr>>min>>sec;
}
time addtime(time t1,time t2)
{
time t3;
t3.hr=t1.hr+t2.hr;
t3.min=t1.min+t2.min;
t3.sec=t1.sec+t2.sec;
if(t3.sec>=60)
{
t3.sec=t3.sec-60;
t3.min++;
}
if(t3.min>=60)
{
t3.min-=60;
t3.hr++;
}
return t3;
}
};
void main()
{
time t1,t2,t3;
cout<<"ENTER THE FIRST TIME\n";
t1.gettime();
cout<<"ENTER THE SECOND TIME\n";
t2.gettime();
cout<<"THE FIRST TIME IS:";
t1.display();
cout<<"THE SECOND TIME IS:";
t2.display();
cout<<"ADDITION OF TWO TIMES IS:";
t3=t3.addtime(t1,t2);
t3.display();
getch();
}


No comments:

Post a Comment