Tuesday, 8 October 2024

Construct a class Time with data members as hours, minute and second. Implement constructors, overloaded constructors and destructors for the same.

 /*

Definition : Construct a class Time with data members as hours, minute and

second. Implement constructors, overloaded constructors and destructors for

the same. Implement a overloaded operator ++ which will increment the time

by one second. Write a function display() to print hours, minute and second.

Assume that 24 hours clock.


Tutorial Date :

Lab Date :

*/

#include<iostream.h>

#include<conio.h>

class Time

{

int hh,mm,ss;

public:

Time()

{

hh=0;

mm=0;

ss=0;

}

Time(int h,int m,int s)

{

hh=h;

mm=m;

ss=s;

}

void operator ++()

{

ss++;

if(ss>=60)

{

mm=mm+(ss/60);

ss=ss%60;

}

if(mm>=60)

{

hh=hh+(mm/60);

mm=mm%60;

}

if(hh>=24)

{

hh=0;

mm=0;

ss=0;

}

cout<<hh<<":"<<mm<<":"<<ss;


}

~Time()

{

hh=NULL;

mm=NULL;

ss=NULL;

}

};

int main()

{

int h,m,s;

clrscr();

Time t;

cout<<"Enter hour:";

cin>>h;

cout<<"Enter minute:";

cin>>m;

cout<<"Enter second:";

cin>>s;

t=Time(h,m,s);

t.operator++();//t++

getch();

return 0;

}






No comments:

Post a Comment