Addition of time with the help of class

 

//adding two time using time class

#include<iostream.h>

#include<conio.h>

 

class time

{

            int hour, minutes, second;

            public:

            time(int h, int m, int s);

            time* operator +(time t);

            void showTime();

            friend class time;

}

 

time::time(int h=0, int m=0, int s=0)

{

            hour=h;

            minutes=m;

            second=s;

}

 

time* time::operator +(time t)

{

            second += t.second;

            if(second>=60)

            {

                        minutes++;

                        second-=60;

            }

 

            minutes += t.minutes;

            if(minutes>=60)

            {

                        hour++;

                        minutes-=60;

            }

 

            hour+=t.hour;

 

            return this;

}

 

void time::showTime()

{

            cout<<endl<<"Hour "<<hour<<"\tminutes "<<minutes<<"\tseconds "<<second;

}

 

void main()

{

            clrscr();

            time *t1,*t2;

            int h,m,s;

            cout<<endl<<"Enter time 1 in hh::mm::ss format"<<endl;

            cin>>h>>m>>s;

            t1 = new time(h,m,s);

            cout<<end<<"Enter time 2 in hh::mm::ss format"<<endl;

            cin>>h>>m>>s;

            t2 = new time(h,m,s);

            t1=*t1+(*t2);

            t1->showTime();

            getch();

}