Saturday, 11 January 2014

Advance Time Program in C++

Create a Class time that has separate int member data for hours,minutes and seconds.One constructor should initialize this data to 0 and another should initial;ize it to fixed values. Another member function should display it in 11:59:59 format. The final member function should add two time objects passed as arguments.

#include<iostream.h>
#include<conio.h>
class time
{private:int h;
             int m;
             int s;
 public:time()
            {h=0;m=0;s=0;}
            time(int a,int b,int c)
            {h=a;m=b;s=c;}
            void getdata();
            void showdata()
            {cout<<"\n the time -> "<<h<<":"<<m<<":"<<s<<".";
             cout<<"\n";
             getch();
            }
            void sum(time a,time b)
            {int t=0;
             h=a.h+b.h;
             m=a.m+b.m;
             s=a.s+b.s;
             if(s>60)
             {t=s/60;
              s=s-(60*t);
              m=m+t;
             }
             t=0;
             if(m>60)
             {t=m/60;
              m=m-(60*t);
              h=h+t;
             }
            }
}t1,t2,t3;
void time :: getdata()
{int c=0,t=0;
 clrscr();
 while(c==0)
 {cout<<"\n enter the parameters of time \n";
  cout<<"\n enter seconds: ";
  cin>>s;
  if(s<0)
  {cout<<"\n please enter the positive value \n";
  }
  else
  {if(s>60)
   {t=s/60;
    s=s-(t*60);
   }
   c=1;
  }
 }
 c=1;
 while(c==1)
 {cout<<"\n enter minutes: ";
  cin>>m;
  if(m<0)
  {cout<<"\n you have entered wrong value enter again";
  }
  else
  {m=m+t;
   if(m>60)
   {t=m/60;
     m=m-(t*60);
   }
   else
   {t=0;}
   c=0;
  }
 }
 c=0;
 while(c==0)
 {cout<<"\n enter hour: ";
  cin>>h;
  if(h<0)
  {cout<<"\n sorry you have entered wrong value enter again";
  }
  else
  {h=h+t;
   c=1;
  }
 }
}
void main()
{int n,i;
 clrscr();
 t1.getdata();
 t1.showdata();
 t2.getdata();
 t2.showdata();
 cout<<" sum of";
 t3.sum(t1,t2);
 t3.showdata();
 getch();
} 

No comments:

Post a Comment