Overrideing operaors... is the the correct way?

closed account (38RiNwbp)
Override operators

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75


#include <iostream>
#include <iomanip>


    friend istream& operator >>(istream& input, Time& inputTime);
    friend ostream& operator <<(ostream& output, const Time& outputTime);
    friend Time operator +(const Time& t1, const Time& t2);
    friend Time operator -(const Time& t1, const Time& t2);
    friend bool operator <(const Time& t1, const Time& t2);
    friend bool operator >(const Time& t1, const Time& t2);



istream& operator >>(istream& input, Time& inputTime) 
{
    input >> inputTime.hours;
    input >> inputTime.minutes;
    input >> inputTime.seconds;

    return input;
}

ostream& operator <<(ostream& output, const Time& outputTime) 
{
    output << setfill('0')<<setw (2);
    output << outputTime.hours<<":";
    output << setfill('0')<<setw (2);
    output << outputTime.minutes<<":";
    output << setfill('0')<<setw (2);
    output << outputTime.seconds<<endl;

    return output;
}

Time operator +(const Time& t1, const Time& t2)
{
    Time Together;

    Together.hours = t1.hours + t2.hours;
    Together.minutes = t1.minutes + t2.minutes;
    Together.seconds = t1.seconds + t2.seconds;

    return Together;
}

Time operator -(const Time& t1, const Time& t2)
{
    Time Together;

    Together.hours = t1.hours - t2.hours;
    Together.minutes = t1.minutes - t2.minutes;
    Together.seconds = t1.seconds - t2.seconds;

    return Together;
}

bool operator <(const Time& t1, const Time& t2)
{

    return (t1.ConvertToseconds() < t2.ConvertToseconds());

}

bool operator >(const Time& t1, const Time& t2)
{

    return (t1.ConvertToseconds() > t2.ConvertToseconds());

}



Topic archived. No new replies allowed.