Implement time class using constructors as user defined data
type in the format. HH:MM:SS with following member functions:
a. To add two time objects
b. To calculate the difference between two time objects and
results should be in seconds.
c. To return second equivalent of time
d. To return output in the format HHLMM:SS which is
equivalent to input argument seconds.
e. Post and pre increment time object
f. Reading time object with cin and displaying with cout
SOLUTION:
#include<iostream.h>
#include<conio.h>
class time
{
int hr, min, sec;
public:
time()
{
hr=min=sec=0;
}
time(int h, int m, int s)
{
hr=h;
min=m;
sec=s;
}
void show()
{
cout<<"time: "<<hr<<":"<<min<<":"<<sec<<endl;
int total;
total=hr*3600+min*60+sec;
cout<<"seconds equivalent of entered time: "<<total<<endl;
}
void showdiff()
{
int total=hr+min+sec;
cout<<"showing differnce between 2 time object in seconds: "<<total<<" seconds";
}
time operator + (time d)
{
time t;
t.hr=hr+d.hr;
t.min=min+d.min;
t.sec=sec+d.sec;
cout<<"addition of 2 time objects"<<endl;
return t;
}
time operator -(time d)
{
time t;
t.hr=(hr-d.hr)*3600;
t.min=(min-d.min)*60;
t.sec=sec-d.sec;
return t;
}
};
void main()
{
clrscr();
time a(3,00,00),b(4,00,00),c,e;
a.show();
b.show();
c=a+b;
c.show();
e=b-a;
e.showdiff();
getch();
}
OUTPUT:
No comments:
Post a Comment