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.

I think should be something like that

#include <iostream>
#include "ccc_time.h"
using namespace std;

class Queue_time : public Time
{
    public:
            Queue_time();
            Queue_time(int a, int b, int c);
            Queue_time operator<<(Queue_time a);
            Queue_time operator>>(Queue_time b);
};

But I'm not quite sure...

I don't know how to overload << and >> operators. If you know how it can be solved I will appreciate.

queue shouldn't inherit from time, it should be separate.

class Queue_time
{
public:
    Queue_time();
    Queue_time(Time);
    // ...
}

The overloaded << operator will use a loop to go through each element in the queue and print it out. the >> operator will take in the values to create a Time object and then add that time object to the end of the queue.

Edited 6 Years Ago by NathanOliver: n/a

I see. You are right. But how can queue be "circular array"? Actually, this task is difficult for me. My knowledge in C++ is fundamental.

Is it something like that?

#include <iostream>
#include "ccc_time.h"
using namespace std;

class Queue_time
{
    public:
            Queue_time();
            Queue_time(Time);
            Queue_time operator<<(queue<Time> a);
            Queue_time operator>>(queue<Time> a);
};
Queue_time::Queue_time operator<<(Time)
{
    while (a.size()>0)
    {
        string numbers=a.front();
        a.pop();
        cout<<numbers<<"\n";
        s.push(numbers);
    }
}
This article has been dead for over six months. Start a new discussion instead.