Introduction to Classes

Additional Example


PROBLEM:

Implement a Time class. Each object of this class will represent a specific time of the day, storing the hours and minutes as integers. Include a constructor, access functions, a function advance(int h, int m) to advance the current time of day by the specified amount and a function reset(int h, int m) to reset the current time to the specified time. Also include a print function.

Remember to include a normalise() function which checks that 0 <= minutes < 60 and 0 <= hours < 24. If the minutes are outside the specified range increment the hours and calculate the minutes in the correct range. If the number of hours are greater than 24 then decrement by 24. This function will be called automatically each time the time is changed \fIie.\fR by the constructor, the reset and advance functions.

Write a short main program which declares objects of type Time and manipulates them using the functions given.


SOLUTION:

#include <iostream.h>
#include <math.h>

class Time
{
        public:
                Time(int h, int m)
                {
                        hour = h;
                        min  = m;
                        normalise();
                }
                int  hours() { return hour;}
                int  mins()  { return min;}
                void advance(int h, int m);
                void reset(int h , int m);
                void print();
        private:
                int     hour, min;
                void normalise();
};

void Time::advance(int h, int m)
{
        hour += h;
        min += m;
        normalise();
}
void Time::reset(int h, int m)
{
        hour = h;
        min = m;
        normalise();
}
void Time::print()
{
        cout << hour << ":" << min << "\en";
}
void Time::normalise()
{
        while (min > 60)
        {
                min -= 60;
                hour++;
        } 
        while (hour > 24)
                hour -= 24;
}


main()
{
        Time time1(9,31), time2(10, 25);

        cout << "Time1 is ";
        time1.print();

        time1.advance(11,35);
        cout << "Time 1 is ";
        time1.print();

        time1.advance(11,35);
        cout << "Time 1 is ";
        time1.print();

        cout << "Time 2 is ";
        time2.print();
        time2.reset(12,11);
        cout << "Time 2 is ";
        time2.print();

}

The main program is simply to test the class and its member functions

If run the program will produce the following output


Time 1 is 9:31
Time 1 is 21:6
Time 1 is 8:41
Time 2 is 10:25
Time 2 is 12:11

On entering the main program, the contsructor is called to initialse Time1 to containg the time 9:31 and it is called again to initialsise Time2 to be 10:25. The print() member function is then called to display the value of Time1. This is then advanced by 11 hours and 35 minutes and the new time is printed. This is repeated. Time2 is then prointed and is the reset to the time of 12:11 and thus new time is displayed.


Back to Main Page for Lecture 9