0

I want my Class B to be able to call a member function from Class A. What is the best way to approach this?

For some context, here are snippets of an abbreviated version of my code:

    class Time{
        public:
        Time();
        Time(int hour, int minute);
        int time_GetHour() const;
        -----
        private:
        int hour;
        int minute;
    };

    int Time::time_GetHour() const{
        return hour;
    }


    class DaTime{
        public:
        DaTime();
        DaTime(Day day, Time start, Time end);
        dt_Difference(DaTime &a, DaTime &b) const;

        private:
        int duration;
        Time start;
        Time end;
    };

    int DaTime::dt_Difference(DaTime &a, DaTime &b) const{
        int diff_hour = 0;
        int diff_min = 0;
        int diff = 0;

        if(dt_LessThanOrEqual(a,b)){
            diff_hour = (b.time_GetHour() - a.time_GetHour())*60;
            diff_min = b.time_GetHour() - a.time_GetHour();
            diff = diff_hour + diff_min;
            return diff;
        }
    }

The error I am getting is stating that my class DaTime has no member time_GetHour(), which I can see that. I am asking what is the best way to approach a fix or to bypass this error? Thanks.

Allen S
  • 53
  • 2
  • 11
  • It depends on what `DaTime` is supposed to be extend functionality or use `Time` internally. Look [here](http://stackoverflow.com/q/21692193/1387612) for answer. – janisz Feb 23 '14 at 20:25
  • What do you want time_GetHour to do? And why are you putting `stuff_` before your method names? – Sneftel Feb 23 '14 at 20:25
  • I want `time_GetHour` to just just return an already existing `hour` from an instantiation of the class `Time`. – Allen S Feb 23 '14 at 20:28
  • Also, I should point out that I want `DaTime` to use `Time` internally, and not extend the functionality. At least I think so. That's why I haven't made `DaTime` inherit from `Time`, I thought that wouldn't be the best approach. – Allen S Feb 23 '14 at 20:29
  • One thing you could do is class inheritance, another thing you could do is make a time instance a member of DaTime ( remember class instances can be treated almost like variables). That's not to say that is all you can do though. – The Floating Brain Feb 23 '14 at 20:34

2 Answers2

3

As you clarified you want to use the Time class internally instead of inheriting it, you could simply fix your problems by accessing the member variables you mentioned:

diff_hour = (b.end.time_GetHour() - a.start.time_GetHour())*60;

However I'm not sure to match your necessary time math with the sample above.

BTW, the two operand operation samples you have should be either static class functions, or even global operator definitions IMHO.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

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;
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
R Sahu
  • 204,454
  • 14
  • 159
  • 270