Thursday 26 September 2013

classes and object (Q2.)

Q WAP to define a class Time , which contains the data member , hh,mm and ss and functions ,
          * readTime();
          * showTime();
          * increament();
          * decreament();
Valid range :
     hh= 0-23
     mm=0-59
     ss=0-59
#include<iostream.h>
#include<conio.h>
class Time
{
     int hh,mm,ss;

     public:
          void readTime()
          {
              cout<<endl<<"Enter the value of hh , mm, and ss :";
              cin>>hh>>mm>>ss;
          }

          void showTime()
          {
               cout<<endl<<hh<<":"<<mm<<":"<<ss;
          }

          void decreament();
    
          void increament();

};
     void Time::increament()
          {
          //increament the second
              ss=ss+1;
//check for the seconds valid range
              if(ss>59)
              {
                   //set seconds to 0
                   ss=0;
                   //increament the minutes

                   mm=mm+1;

                   //check for the minutes valid range

                   if(mm>59)
                   {
                        //set the minutes to 0

                        mm=0;

                        //increament the hours

                        hh=hh+1;

                        //check the hours valid range

                        if(hh>23)
                             hh=0;
                   }
               }
          }
    
     void Time::decreament()
     {
          //decrease the seconds by 1

          ss=ss-1;

          //check for the valid range

          if(ss<0)
          {
              //set seconds to maximum

              ss=59;

     //decreament the minutes by 1

              mm=mm-1;

              //check for the valid range
             
              if(mm<0)
              {
                   //set mm to maximum

                   mm=59;

                   //decreament the hour by 1

                   hh=hh-1;

                   //check for the valid range

                   if(hh<0)
                        hh=23;
              }
          }
     }


void main()
{
     Time t;

     t.readTime();

     t.showTime();

     t.increament();

     t.showTime();

     t.decreament();

     t.showTime();

     getch();

}

No comments:

Post a Comment