You will need a function in Time
to return the minutes. Here's a revised Time
.
class Time
{
public:
Time();
Time(int hour, int minute);
int time_GetHour() const;
int time_GetMinute() const;
private:
int hour;
int minute;
};
int Time::time_GetHour() const
{
return hour;
}
int Time::time_GetMinute() const
{
return minute;
}
Your definition of DaTime
is a little bit unclear. Do you need to store start
and end
as well as duration
? Can you get away with storing either start
and end
or duration
? What purpose does the input Day day
serve in construction of DaTime
?
If you have already computed duration
, DaTime::dt_Difference
can be altered a little bit to use duration
instead of relying on the member functions of Time
.
int DaTime::dt_Difference(DaTime &a, DaTime &b) const
{
int diff = 0;
if(dt_LessThanOrEqual(a,b))
{
diff = b.duration - a.duration;
}
return diff;
}
However, the way you have decalredDaTime::dt_Difference
, it will be awkward to use. You have to have lines like:
DaTime dt1;
DaTime dt2;
DaTime dt3;
int difference = dt1.dt_Difference(dt2, dt3);
Notice that the state of dt1
has no impact on the return value of dt_Difference
. Is this how intended to use it? A better alternative would be to change the declaration of dt_Difference
to be:
int dt_Difference(const DaTime &a) const;
Then, the implementation would be altered to be:
int DaTime::dt_Difference(const DaTime &a) const
{
int diff = 0;
if(dt_LessThanOrEqual(*this,a))
{
diff = b.duration - this->duration;
}
return diff;
}
Then, your usage could be changed to:
DaTime dt1;
DaTime dt2;
int difference = dt1.dt_Difference(dt2);
In addition, there is no need to have names time_GetHour
and time_GetMinute
. time
is redundant since you are dealing with the class Time
. You could change them to getHour
and getSecond
. Similarly, you could also change dt_Difference
to operator-
. You could also replace the use of if(dt_LessThanOrEqual(*this, a))
by if ( *this <= a )
. The second form is more readable than the first form.
In summary, your code could be changed to look like the following:
class Time
{
public:
Time();
Time(int hour, int minute);
int getHour() const;
int getMinute() const;
private:
int hour;
int minute;
};
int Time::getHour() const
{
return hour;
}
int Time::getMinute() const
{
return minute;
}
class DaTime
{
public:
DaTime();
DaTime(Day day, Time start, Time end);
int operator-(const DaTime &a) const;
bool operator<=(const DaTime& rhs) const;
private:
int duration;
};
int DaTime::operator-(const DaTime &a) const
{
int diff = 0;
if( a <= *this )
{
diff = this->duration - a.duration;
}
return diff;
}
DaTime dt1;
DaTime dt2;
int difference = dt2 - dt1;