1

I want to create a general abstract TimePeriod class. Example subclasses of it would be be Day, Hour and Minute.

I also want to create a general TimeData class that associates a TimePeriod object with some data, such as two doubles for the lowest and highest temperature during that time period.

Creating an abstract Data class for this purpose may not be a bad idea. So a TimeData would associate a TimePeriod with a Data.

Here is an example how the hierarchy could look like w.r.t. time:

hierarchy

In addition to the "vertical" parent-child relationship (when I'm working with a certain hour, I want to know which day that hour is in), I also want "horizontal" relationships that allow me to easily loop through the daily data, hourly data, minute data, etc.

Can you give me ideas on how to model this as classes in C++? Do I need to use pointers (in which case I'd prefer smart pointers) or can I use the easier vector, list, etc. STL classes?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
7cows
  • 4,974
  • 6
  • 25
  • 30
  • Why not use c++11's `time_point`s? – jt234 Apr 30 '13 at 13:48
  • Have a look at [Boost.Units](http://www.boost.org/doc/libs/1_38_0/doc/html/boost_units/Units.html) – Peter Wood Apr 30 '13 at 13:53
  • Use `#include < ctime >`? Check details [here](http://www.tutorialspoint.com/cplusplus/cpp_date_time.htm) or you could review `std::chrono`. – Mushy Apr 30 '13 at 13:55
  • I could for sure use some of these to represent the actual times, but it's still important to distinguish the data at the various "parent-child" time granularities (day vs. hour vs. minute etc). sometimes i may need to now the lowest temperature during this day, or highest temperature during that hour/minute, etc. – 7cows Apr 30 '13 at 14:07

1 Answers1

1

You can change struct to class. I really dont get why you want to define TimePeriod as abstract. Also i would suggest making a linked list of type TimePeriod. Each item in the list would point to the next timeperiod item through the next pointer.

struct TimeData
{
    int minTemp;
    int maxTemp;

    public int setData(int minTemp,int maxTemp)
    {
    }
}

struct TimePeriod
{
    Day d;//this is kinda redundant as it only contains one day
    TimeData td;
    TimePeriod *next;//to point to next TimePeriod object in the list
};

struct day
{
    int d;
    Hour h[24];
    TimeData td;
    int setTimeData()
    {
        //cycle through hour timedata and calculate avg or whatever you want
    }
};
struct Hour
{
    int h;
    Min m[60];
    TimeData td;
    int setTimeData()
    {
        //cycle through min timedata and calculate avg or whatever you want
    }

};
struct Min
{
    int m;
    TimeData td;
    int setTimeData(int minTemp,int maxTemp)
    {
        //set  TimeData td
    }
};
thunderbird
  • 2,715
  • 5
  • 27
  • 51