Create a class Clock that contains integer data members as hours, minutes and seconds. Write a C++ program to perform following member functions: void setclock(int, int, int ) to set the initial time of clock object. void showclock() to display the time in hh:min:sec format. Write a function tick( ) which by default increment the value of second by 1 or according to user specified second. The clock uses 24 hours format.

//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;

#include<iostream>
#include<conio.h>
class time
{
   int h,m,s; //h=hours , m=minutes , s=seconds
   public:
   void read()
   {
      cout<<"Enter hours: ";
      cin>>h;
      cout<<"Enter minutes: ";
      cin>>m;
      cout<<"Enter seconds: ";
      cin>>s;
      cout<<"Time is: "<<h<<" hours "<<m<<" minutes "<<s<<" seconds "<<endl;
   }
   void disp()
   {
    cout<<"Time -\t"<<h<<":"<<m<<":"<<s<<endl;
   }
   void add(time t)
   {
    s=s+t.s;
      if(s>=60)
      {
      m=m+1;
         s=s-60;
      }
      m=m+t.m;
      if(m>=60)
      {
      h=h+1;
         m=m-60;
      }
      h=h+t.h;
      if(h>24)
      {
      h=h-24;
      cout<<"1 day";
      }
      cout<<"Addition -\t"<<h<<":"<<m<<":"<<s;
   }
};
int main()
{
int ch;
   time t1,t2;
    do
    {
    cout<<"\n1.read \n2.display\n3.Add\n4.Exit\nEnter your choice: ";
      cin>>ch;
      switch(ch)
      {
     case 1:      t1.read();
                      t2.read();break;
      case 2:     t1.disp();
                      t2.disp();break;
      case 3:     t1.add(t2);break;
      }
    }while(ch!=4);
  return(0);
}

0 Comments