Operator overloading in 4-2 electronic clock

Keywords: Attribute less

Problem Description

Operator overloading can be achieved through the exercises in this topic.
Design a Time class, private data members are hour, minute and second.
The public member functions are: setHour(int) sets the value of data member hour, illegal input defaults to 12; setMinue(int) sets the value of data member minute, illegal input defaults to 0; setSecond(int) sets the value of data member seconds, illegal input defaults to 0; setTime(int, int) sets the value of three data members in time, minutes and seconds; three member functions int getHour (); int G EtMinute (); int getSecond (); used to get the attribute values of the time object, respectively.
Define two constructors Time(); and Time(int,int,int);
Define a member function void displayTime(); for display time, note that the format is hh:mm:ss, the number of digits is not enough to fill in 0;
Define a member function void tick() with a clock increment of 1; add the value of second to 1, and note whether it reaches 60;
Define a member or friend function bool operator=(... To determine whether the values of two time objects are equal;
Define a member or friend function bool operator >(... To determine whether the value of the first time object is greater than that of the second time object.

In the main function main(), the start time and end time are specified, and the corresponding member function is called to display the values of all time objects from the start time to the end time. The format is shown in the example output.
Input

Enter 6 integers with a space interval between them; the values of the start time, minute, second and end time, minutes and seconds, respectively
Output

Values of all time objects from start time to end time; each value takes one line in the form of hh:mm:ss
Example Input

01 01 01 01 01 10
Example Output

01:01:01
01:01:02
01:01:03
01:01:04
01:01:05
01:01:06
01:01:07
01:01:08
01:01:09
01:01:10
Hint

input
11 10 12 10 12 56
output
The begin time is not earlier than the end time!

There are many functions that are not required, but the title requires writing...

#include <iostream>
#include <stdio.h>
using namespace std;
class Time
{
private:
    int hour, minute, second;
public:
    Time()
    {
        hour = 0;
        minute = 0;
        second = 0;
    }
    Time(int a, int b, int c)
    {
        hour = a;
        minute = b;
        second = c;
    }
    void sethour(int a)
    {
        if(a <= 0 || a > 12)//<=0 are all 12
            hour = 12;
        else
            hour = a;
    }
    void setminute(int a)
    {
        if(a < 0 || a >= 60)//>=60 are all 0
            minute = 0;
        else
            minute = a;
    }
    void setsecond(int a)
    {
        if(a < 0 || a >= 60)
            second = 0;
        else
            second = a;
    }
    void settime(int a, int b, int c)
    {
        hour = a;
        minute = b;
        second = c;
    }
    int gethour()
    {
        return hour;
    }
    int getminute()
    {
        return minute;
    }
    int getsecond()
    {
        return second;
    }
    void display()
    {
        printf("%02d:%02d:%02d\n", gethour(), getminute(), getsecond());//Less than two complements 0
    }
    void tick()
    {
        second ++;
        if(second >= 60)
        {
            second = 0;
            minute++;
            if(minute >= 60)
            {
                minute = 0;
                hour++;
            }
        }
    }
    bool operator==(Time &t)
    {
        if(hour == t.hour && minute == t.minute && second == t.second)
            return true;
        else
            return false;
    }
    bool operator >(Time &t)
    {
        if(hour > t.hour)
            return true;
        else
        {
            if(minute > t.minute)
                return true;
            else
            {
                if(second > t.second)
                    return true;
                else
                    return false;
            }
        }
    }
};
int main()
{
    Time t1, t2;
    int a, b, c, d, e, f;
    cin>>a>>b>>c>>d>>e>>f;
    t1.settime(a, b, c);
    t2.settime(d, e, f);
    if(t1 > t2)
        cout<<"The begin time is not earlier than the end time!"<<endl;
    else
    {
        t1.display();
        while(t2 > t1)
        {
            t1.tick();
            t1.display();
        }
    }
    return 0;
}

Posted by stockdalep on Sun, 19 May 2019 17:37:24 -0700