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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
//Programming Assignment 5 Classes, Ine Hordvik
#include <iostream>
using namespace std;
class Time
{
int hours,
minutes,
seconds;
public:
//constructors
Time() : hours(0), minutes(0), seconds(0)
{}
Time(int h, int m = 0, int s = 0) : hours(h), minutes(m), seconds(s)
{}
//access functions
int get_hours() const
{
return hours;
}
int get_minutes() const
{
return minutes;
}
int get_seconds() const
{
return seconds;
}
bool lessthan(Time t2) //For two Time objects t1 and t2, t1.lessthan(t2) returns true if t1 is less than, or comes before t2.
{
if (hours < t2.hours)
{
return true;
}
if (minutes < t2.minutes)
{
return true;
}
if (seconds < t2.seconds)
{
return true;
}
return false;
}
bool greaterthan(Time t2) //For two Time objects t1 and t2, t1.greaterthan(t2) returns true if t1 is greater than, or comes after t2.
{
if (hours > t2.hours)
{
return true;
}
if (minutes > t2.minutes)
{
return true;
}
if (seconds > t2.seconds)
{
return true;
}
return false;
}
bool equalto(Time t2) //For two Time objects t1 and t2, t1.equalto(t2) returns true if t1 is equal to, or is the same time as t2.
{
if (hours == t2.hours)
{
return true;
}
if (minutes == t2.minutes)
{
return true;
}
if (seconds == t2.seconds)
{
return true;
}
return false;
}
//Modifier functions
void set_hours(int h) //Set the hours of Time object to value specified by h.
{
hours = h;
}
void set_minutes(int m) //Set the minutes of Time object to value specified by m.
{
minutes = m;
}
void set_seconds(int s) //Set the seconds of Time object to value specified by s.
{
seconds = s;
}
void set_time(int h, int m, int s)
{
hours = (h >= 0 && h < 24) ? h : 00;
minutes = (m >= 0 && m < 60) ? m : 00;
seconds = (s >= 0 && s < 60) ? s : 00;
}
//Input / Output Functions :
void read()
{
char skip;
cin >> hours
>> skip
>> minutes
>> skip
>> seconds;
}
void write()
{
cout << hours<<":"<< minutes<<":"<<seconds;
}
};
void main()
{
Time t1,
t2(23, 45, 35);
t1.write(); //output default time t1 00:00:00
cout<< endl;
t2.write(); //output initialized time t2 23:45:35
cout << "\nEnter first time value (hh:mm:ss): ";
t1.read();
cout << "\nEnter second time value (hh:mm:ss): ";
t2.read();
cout << "\nTime values entered in ascending order: "<<endl;
if (t1.lessthan(t2))
t1.write();
cout << endl;
t2.write();
cout << endl;
}
|