Monday, 25 March 2019

CPP-EX66

 Create a class time that contains hours, minute and seconds as data members. Write the member functions:1.  to add two object of type time, passed  as arguments .
    2.  to convert a time into total number of seconds.
    3.  to display the time into format like: 01:02:00


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
class times
{

    int hr,mi,sec;

    public: times() { }
        times(int a,int b,int c)
        { hr=a; mi=b; sec=c;}


        void display()
        {
        cout<<hr<<":";
        cout<<mi<<":";
        cout<<sec<<"";
        }

      /*    void get()
    {
        cout<<"\n Enter time in HH:MM:SS format\t";
        cin>>hr>>mi>>sec;


    }

    void display()
    {
        cout<<"\nAddtion of two time is:\t";
        cout<<hr<<":"<<mi<<":"<<sec<<"\n";
    } */

    void displayinsec()
    {
        cout<<"\nTotal time in second :\t";
        cout<<hr*3600+mi*60+sec<<":sec";
    }


       //    friend times operator +(times &d, times &d2);
     friend times operator +(times , times );

};

//times operator +(times &d, times &d2)
  times operator +(times d, times d2)
{
    times t;
    t.hr=d.hr+d2.hr;
    t.mi=d.mi+d2.mi;
    t.sec=d.sec+d2.sec;

    if(t.mi>=60)
    {

        t.mi=t.mi-60;
        t.hr=t.hr+1;
    }

    if(t.sec>=60)
    {
        t.sec=t.sec-60;
        t.mi=t.mi+1;
    }

    return t;

}

void main()
{

       //    times t1,t2,t3;
    clrscr();
     times t1(3,40,30);
     times t2(5,10,10);
    times  t3;
      // t1.get();
       cout<<"\n First Time:-";
    t1.display();

     // t2.get();
     cout<<"\n Second Time:-";
    t2.display();
     cout<<"\n Addition of Time:-";
    t3=t1+t2;

    t3.display();
    t3.displayinsec();

    getch();
}