Saturday, September 27, 2014

Create a class time which stores time in hours, minutes and seconds. Use necessary member functions to get and display the information. Finally define a member function sum that takes the two input time as hrs:min:sec and add the two time and assign to third time i.e. t3=t1+t2. Note that 60sec=1 min and 60mins= 1 hr.

ALGORITHM:
1.      Declare class time
2.      get(int,int,int)
i.                    set hrs =h, min=m,secs=s
3.      put()
ii.                  display hrs:min:secs
4.      sum(time,time)
sec=t1.sec+t2.sec;
                        min=t1.min+t2.min;
                        if (sec>=60)
                        {
                                    min++;
                        }

                        hr=t1.hr+t2.hr;
                        if (min>=60)
                        {
                                    hr++;

                        }
                        sec=sec%60;
                        min=min%60;
                        hr=hr%12;
5.      main()
                                i.            t1.get(h,m,s)
                              ii.            t2.get(h,m,s)
                            iii.            t3.sum(t1,t2)
                            iv.            t1.put()
                              v.            t2.put()
                            vi.            t3.put()
6.      end

CODE:
#include
#include
class time
{
            int hr,min,sec;
            public:
            void gettime()
            {
            cout<
            cin>>hr>>min>>sec;
            }
            void puttime()
            {
                        cout<
            }
            void timesum(time t1,time t2)
            {
                        sec=t1.sec+t2.sec;
                        min=t1.min+t2.min;
                        if (sec>=60)
                        {
                                    min++;
                        }

                        hr=t1.hr+t2.hr;
                        if (min>=60)
                        {
                                    hr++;

                        }
                        sec=sec%60;
                        min=min%60;
                        hr=hr%12;
            }
};
void main()
{
            clrscr();
            time t1,t2,t3;
            t1.gettime();
            t2.gettime();
            t3.timesum(t1,t2);
            cout<
            t1.puttime();
            cout<<"+ T2~";
            t2.puttime();
            cout<
            t3.puttime();
            getch();

}

No comments:

Post a Comment