Wednesday 16 August 2017

Augment the time class referred to in Exercise 3 to include overloaded increment (++) and decrement (--) operators that operate in both prefix and postfix notation and return values. Add statements to main() to test these operators.


 
SOLUTION:

#include "stdafx.h"
#include "iostream"
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////
class time
{           private:
                        int hrs, mints, secs;
            public:
                        time( ) : hrs(0), mints(0), secs(0)
                                    { /*Empty Body*/ }
                        time(int h, int m, int s) : hrs(h), mints(m), secs(s)
{ /*Empty Body*/ }
                        void display( ) const
                                     {          cout << hrs << ":" << mints << ":" << secs << endl;              }
                        time operator ++ ( );
                        time operator ++ (int);
                        time operator -- ( );
                        time operator -- (int);
};
//---------------------------------------------------------------------------------------
time time::operator ++ ( )
{
            ++secs;
            if(secs > 59)
                        {           secs -=60;       mints++;          }
            if(mints > 59)
                        {           mints -=60;      hrs++;              }
            return time(hrs, mints, secs); 
}
time time::operator ++ (int)
{
            secs++;
            if(secs > 59)
                        {           secs -=60;       mints++;          }
            if(mints > 59)
                        {           mints -=60;      hrs++;              }
            return time(hrs, mints, secs); 
}
time time::operator -- ( )
{
            --secs;
            if(secs < 0)
                        {           secs +=60;      mints--;            }
            if(mints < 0)
                        {           mints +=60;     hrs--;                }
            return time(hrs, mints, secs); 
}
time time::operator -- (int)
{
            secs--;
            if(secs < 0)
                        {           secs +=60;      mints--;            }
            if(mints < 0)
                        {           mints +=60;     hrs--;                }
            return time(hrs, mints, secs); 
}
/////////////////////////////////////////////////////////////////////////////////////////
void main( )
{
            time time1(10,25,2), time2(5,59,58);
            cout << "Time1 is: ";               time1.display( );
            cout << "Time2 is: ";               time2.display( );
            ++time1;          cout << "\nTime1 after prefix increment is: ";            time1.display( );
            ++time2;          cout << "Time2 after prefix increment is: ";               time2.display( );
            time1++;          cout << "\nTime1 after postfix increment is: ";           time1.display( );
            time2++;          cout << "Time2 after postfix increment is: ";              time2.display( );
            --time1;            cout << "\nTime1 after prefix decrement is: ";           time1.display( );
            --time2;            cout << "Time2 after prefix decrement is: ";              time2.display( );
            time1--;            cout << "\nTime1 after postfix decrement is: ";          time1.display( );
            time2--;            cout << "Time2 after postfix decrement is: ";             time2.display( );
            system("pause");
}

OUTPUT:



0 comments: