Menu

Custom Search

C++ lab Program

/*vi) In the program with the class time, modify the driver program as:
i) Create two initialized time
ii.Create a pointer to time object
iii. Make the pointer point to a new time object (Using default constructor)
iv. Call the add ( time, time ) to store the sum in pointer to object.
v. Call display () using pointer to object and delete the pointer to object.
*/
# include
# include

class time
{
int h, m, s;
public:
time ()
{
h =0; m=0; s=0;
}
time (int x, int y, int z)
{
h=x; m=y; s=z;
}
void display ()
{
cout << endl << "Hour: " << h << " Min: " << m << " Seconds: " << s; } void addtime (time, time); }; void time :: addtime (time a, time b) { s= a.s + b.s; m = a.m + b.m; h = a.h + b.h; while (s>=60)
{
m = m +(s/60);
s = s%60;
}
while (m>=60)
{
h = h +(m/60);
m = m%60;
}
}

void main ()
{
clrscr ();
time *a;
a = new time [1];
time b (10, 15, 20);
time c (15, 45, 50);
a->addtime (b,c);
b.display ();
c.display ();
a->display ();
getch ();
}

/*
O/p:
Hour: 0 Min: 0 Seconds: 0
Hour: 10 Min: 15 Seconds: 20
Hour: 15 Min: 45 Seconds: 50
Hour: 26 Min: 1 Seconds: 10
*/

No comments:

Post a Comment