Question

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?

Was it helpful?

Solution

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
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top