Wednesday, October 1, 2014

WAP that has class to represent time. The class should have constructor to initialize data members hours, minute and second to 0 and to initialize them to values passed as arguments. The class should have member function to add time objects and return the result as time object . there should be another function to display the result is 24 hour format.

#include
#include
class time
{
            private:
            int hrs, min,sec;
   public:
            time(int h=0,int m=0,int s=0)
      {
            hrs =h;
         min =m;
         sec =s;
      }
      void display()
      {
            cout<
      }
      time add(time t);
};

time time::add(time t)
{
            time s;
            s.hrs=hrs+t.hrs;
   s.min=min+t.min;
   s.sec=sec+t.sec;
   s.min+=sec/60;
   s.sec=sec%60;
   s.hrs+=min/60;
   s.min=s.min%60;

   return s;
}

void main()
{
            clrscr();
   time t1(12,34,40),t2(1,20);
   time t3=t1.add(t2);
   t1.display();cout<<" + ";
   t2.display();cout<<" = ";
   t3.display();
   getch();

}

No comments:

Post a Comment