Define a class Queue_time that represents a queue of Time objects (from our textbook) by keeping its elements in a dynamically allocated array (with maximum capacity) and make the queue as a "circular array" (the first element follows the last one).
Supply the "big three" memory management functions. Use this class to demonstrate
(a) the difference between initialization
Queue_time s;
Queue_time t = s;
and assignment operation
Queue_time s;
Queue_time t;
s = t;
(b) the fact that all constructed objects are automatically destroyed
(c) the fact that the copy constructor is invoked if an object is passed by value to a function
(d) the fact that the copy constructor is not invoked when a parameter is passed by reference
(e) the fact that the copy constructor is used to copy a return value to the caller.
Supply a member function size(). Overload the += binary operator for push an element to a queue and the -- unary operator for pop an element from the queue. For example
Queue_time q;
q += Time(10,10,0);
q--;
produces an empty queue. Overload the stream operators << and >> for output and input queue objects. Demonstrate all these functions and operators.
I have to use this header.
#ifndef CCC_TIME_H
#define CCC_TIME_H
/**
A class that describes a time of day
(between 00:00:00 and 23:59:59)
*/
class Time
{
public:
/**
Constructs a time of day.
@param hour the hours
@param min the minutes
@param sec the seconds
*/
Time(int hour, int min, int sec);
/**
Constructs a Time object that is set to
the time at which the constructor executes.
*/
Time();
/**
Gets the hours of this time.
@return the hours
*/
int get_hours() const;
/**
Gets the minutes of this time.
@return the minutes
*/
int get_minutes() const;
/**
Gets the seconds of this time.
@return the seconds
*/
int get_seconds() const;
/**
Computes the seconds between this time and another.
@param t the other time
@return the number of seconds between this time and t
*/
int seconds_from(Time t) const;
/**
Adds a number of seconds to this time.
@param s the number of seconds to add
*/
void add_seconds(int s);
private:
int time_in_secs;
};
#endif
and the source file
#include <ctime>
#include <cassert>
using namespace std;
#include "ccc_time.h"
/**
Computes the correct remainder for negative dividend
@param a - an integer
@param n - an integer > 0
@return the mathematically correct remainder r such that
a - r is divisible by n and 0 <= r and r < n
*/
int remainder(int a, int n)
{
if (a >= 0)
return a % n;
else
return n - 1 - (-a - 1) % n;
}
Time::Time(int hour, int min, int sec)
{
assert(0 <= hour);
assert(hour < 24);
assert(0 <= min);
assert(min < 60);
assert(0 <= sec);
assert(sec < 60);
time_in_secs = 60L * 60 * hour + 60 * min + sec;
}
Time::Time()
{
time_t now = time(0);
struct tm& t = *localtime(&now);
time_in_secs = 60L * 60 * t.tm_hour + 60 * t.tm_min + t.tm_sec;
}
int Time::get_hours() const
{
return time_in_secs / (60 * 60);
}
int Time::get_minutes() const
{
return (time_in_secs / 60) % 60;
}
int Time::get_seconds() const
{
return time_in_secs % 60;
}
int Time::seconds_from(Time t) const
{
return time_in_secs - t.time_in_secs;
}
void Time::add_seconds(int s)
{
const int SECONDS_PER_DAY = 60 * 60 * 24;
time_in_secs = remainder(time_in_secs + s, SECONDS_PER_DAY);
}
If anybody can help me?
Thank you in advance.