I hope it's right place for it, if no - sorry, please tell me where I should place it.
I wrote simple time class, could you review it, tell me what I did good, what wrong, and how I can fix it?
I excluded things like include guards, namespaces, include directives...
header file:
class time {
private:
unsigned miliseconds, seconds, minutes, hours;
public:
// default constructor
time() : miliseconds(0), seconds(0), minutes(0), hours(0) {}
// constructor which takes 4 arguments and sets class' variables
explicit time(const unsigned, const unsigned = 0, const unsigned = 0, const unsigned = 0);
// sets variables to given values
void Set(const unsigned hrs = 0, const unsigned mins = 0, const unsigned secs = 0, const unsigned milisecs = 0) { *this = time(hrs, mins, secs, milisecs); }
// adds hours
void AddHour(const unsigned hrs) { hours += hrs; }
// adds minutes
void AddMin(const unsigned mins) { minutes += mins; hours += minutes / 60; minutes %= 60; }
// adds seconds
void AddSec(const unsigned secs) { *this += time(0, 0, secs); }
// adds milisecs
void AddMsec(const unsigned milisecs) { *this += time(0, 0, 0, milisecs); }
// subtracts hours
void SubHour(const unsigned hrs) { *this -= time(hrs); }
// subtracts minutes
void SubMin(const unsigned mins) { *this -= time(0, mins); }
// subtracts seconds
void SubSec(const unsigned secs) { *this -= time(0, 0, secs); }
// subtracts milisecs
void SubMsec(const unsigned milisecs) { *this -= time(0, 0, 0, milisecs); }
// returns miliseconds
unsigned GetMseconds() const { return miliseconds; }
// returns seconds
unsigned GetSeconds() const { return seconds; }
// returns minutes
unsigned GetMinutes() const { return minutes; }
// returns hours
unsigned GetHours() const { return hours; }
// returns time in miliseconds
unsigned ToMsecs() const { return hours * 3600000 + minutes * 60000 + seconds * 1000 + miliseconds; }
// compares if left time is bigger than right time
bool operator>(const time& comp) const { return this->ToMsecs() > comp.ToMsecs(); }
// compares if left time is smaller than right time
bool operator<(const time& comp) const { return this->ToMsecs() < comp.ToMsecs(); }
// compares if left time is equal to right time
bool operator==(const time& comp) const { return this->ToMsecs() == comp.ToMsecs(); }
// compares if left time is not equal to right time
bool operator!=(const time& comp) const { return this->ToMsecs() != comp.ToMsecs(); }
// compares if left time is bigger or equal to right time
bool operator>=(const time& comp) const { return this->ToMsecs() >= comp.ToMsecs(); }
// compares if left time is smaller or equal to right time
bool operator<=(const time& comp) const { return this->ToMsecs() <= comp.ToMsecs(); }
// multiplies time by a value
time operator*(const double multi) const { return time(0, 0, 0, this->ToMsecs() * multi + 0.5); }
// divides time by a value
time operator/(const double div) const { return time(0, 0, 0, this->ToMsecs() / div + 0.5); }
// adds times
time operator+(const time& add) const { return time(hours + add.hours, minutes + add.minutes, seconds + add.seconds, miliseconds + add.miliseconds); }
// subtracts times
time operator-(const time&) const;
// multiplies time by a value
time& operator*=(const double multi) { return *this = *this * multi; }
// divides time by a value
time& operator/=(const double div) { return *this = *this / div; }
// adds times
time& operator+=(const time& add) { return *this = *this + add; }
// subtracts times
time& operator-=(const time& sub) { return *this = *this - sub; }
// multiplies value by a time
friend time operator*(const double multi, const time& timeMulti) { return timeMulti * multi; }
};
cpp file:
time::time(const unsigned hrs, const unsigned mins, const unsigned secs, const unsigned milisecs) {
miliseconds = milisecs % 1000;
seconds = secs + milisecs / 1000;
minutes = mins + seconds / 60;
hours = hrs + minutes / 60;
minutes %= 60;
seconds %= 60;
}
time time::operator-(const time& sub) const{
if (*this <= sub)
return time(0);
return time(hours - sub.hours, minutes - sub.minutes, seconds - sub.seconds, miliseconds - sub.miliseconds);
}