title: C++ Calling Member Function from a different class member function
body: <p>I am writing a simple program to simulate a watch. Class Date, Class Time, Class Watch. Watch is derived from Date and Time. Trying to call a date member function from Time member function. It says I cant do it without an object. I am learning and I am sure most of this is crap haha. Any help would be appriciated. Basically at the end of Time::Tick, I am trying to call Class Date member function inDay. Calling one member function from another class member function. I dont know how to do a friend function from another class. I tried to make my functions static but it leads me down a long road of errors. Any advice?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;windows.h&gt;
  using namespace std;
  
  class Date
  {
  public:
      Date();      
      void inDay();;// increments day, tests if month increments, if true, increment
      void inMonth();   // increments month, tests if year increments, if true, increment
      void inYear();    // increment year
      void displayWatch();   // calls daysInMonthSwith, calls inDay, Display date
      int daysInMonthSwitch(); // determines the number of days in the month based on which month using a switch
      //void testPrint(); //Testing purposes only
      void setDay();
      void setMonth();
      void setYear();
  
  private:
  
      int day;
      int month;
      int year;
      int daysInMonth;
  };
  
  
  class Time{       
  public:
      Time();
  
      void setTime(int, int, int ); // set hours, minutes, seconds
      void printStandard(); // print time in standard time format 12h
      void setHour();
      void setMin();
      void setSec();
      void tick();
  
  private:
      int hour; 
      int minute;
      int second;
  };
  
  class Watch: public Time, public Date {       
  public:
      Watch();
  
      int getDate();
      void setTime(int, int, int);
      int getTime();
  
  private:
  
  };
  
  //class Time::
  
  Date::Date() : // Class Constructor
  day(27),
      month(2),
      year(2013),
      daysInMonth(31)
  {            
  } // End Constructor
  
  void Date::setDay()
  {
      int tempDay;
      cout &lt;&lt; "There are " &lt;&lt; daysInMonth &lt;&lt; " in the current month. Please enter a day between 1 and " &lt;&lt; daysInMonth &lt;&lt; ": ";
      cin &gt;&gt; tempDay;
      while (tempDay &lt; 1 || tempDay &gt; daysInMonth)
      {
          cout &lt;&lt; "You entered a day outside the range." &lt;&lt; endl;
          cout &lt;&lt; "There are " &lt;&lt; daysInMonth &lt;&lt; " in the current month. Please enter a day between 1 and " &lt;&lt; daysInMonth &lt;&lt; ": ";
          cin &gt;&gt; tempDay;
      }
      day = tempDay;
  }
  
  
  
  void Date::setMonth()
  {
      int tempMonth;
      cout &lt;&lt; "There are 12 in the year. Please enter a day between 1 and 12: ";
      cin &gt;&gt; tempMonth;
      while (tempMonth &lt; 1 || tempMonth &gt; 12)
      {
          cout &lt;&lt; "You entered a month outside the range." &lt;&lt; endl;
          cout &lt;&lt; "There are 12 in the year. Please enter a day between 1 and 12: ";
          cin &gt;&gt; tempMonth;
      }
      month = tempMonth;    
  }     
  
  void Date::setYear()
  {
      int tempYear;
      cout &lt;&lt; "Please enter a year in the full number(Correct: 2005, Incorrect: 05): ";
      cin &gt;&gt; tempYear;
      year = tempYear;     
  }
  
  
  
  void Date::displayWatch()
  {
      daysInMonthSwitch();
      cout &lt;&lt; "Date: " &lt;&lt; month &lt;&lt; "/" &lt;&lt; day &lt;&lt; "/" &lt;&lt; year &lt;&lt; endl; 
      //testPrint(); // Prints number of days in current month for testing only
      //cout &lt;&lt; "Month: " &lt;&lt; month &lt;&lt; endl; // Prints month for testing purposes
      //inDay();
  }
  ////////////// inDay function increments day and if conditions are met, increments month and year
  void Date::inDay() //increments month and if day is more greater than number of days in month, call inMonth function
  {  
      day++;
  
      if (day &gt; daysInMonth)
      {     
          inMonth();
          day = 1;
      }
  }// end function
  void Date::inMonth() // increment month and if month is more greater than or equal to 12, call inYear function
  {
      month++;
  
      if (month &gt;= 12)
      {
          Date::inYear();
      }
  } // end function
  void Date::inYear() // increment year
  {
      year++;
      month = 1;
  } // end function
  
  //void Date::testPrint()
  //{
  // cout &lt;&lt; "Days in month: " &lt;&lt; daysInMonth &lt;&lt; endl;
  //} // for testing purposes only
  
  int Date::daysInMonthSwitch() // Function contains switch that determines the number of days in the current month
  {
      //int month = m;
      int result;
  
      switch(month)
      {
      case 1:
          result = 31;
          break;
      case 2:
          result = 28; 
          break;
      case 3:
          result = 31;
          break;
      case 4:
          result = 30;
          break;
      case 5:
          result = 31;
          break;
      case 6:
          result = 30;
          break;
      case 7:
          result = 31;
          break;
      case 8:
          result = 31;
          break;
      case 9:
          result = 30;
          break;
      case 10:
          result = 31;
          break;
      case 11:
          result = 30;
          break;
      case 12:
          result = 31;
          break;
      default :
          cout &lt;&lt; "Invalid Month" &lt;&lt; endl;
      }
      daysInMonth = result;
      return daysInMonth;
  } // end daysInMonthSwitch function
  
  //////////////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////////
  
  
  Time::Time()
  {
      const time_t currentTime = time(0);
      const tm *localTime = localtime( &amp;currentTime );
      setTime(localTime-&gt;tm_hour, localTime-&gt;tm_min, localTime-&gt;tm_sec);
  }
  void Time::setHour()
  {
      int h;
      cout &lt;&lt; "Please enter the hour: ";
      cin &gt;&gt; h;
      hour = ( h &gt;= 0 &amp;&amp; h &lt;= 24 ) ? h : 0;
  }
  
  void Time::setMin()
  {
      int m;
      cout &lt;&lt; "Please enter the minute: ";
      cin &gt;&gt; m;
      minute = ( m &gt;= 0 &amp;&amp; m &lt;= 60 ) ? m : 0;
  }
  
  void Time::setSec()
  {
      int s;
      cout &lt;&lt; "Please enter the second: ";
      cin &gt;&gt; s;
      second = ( s &gt;= 0 &amp;&amp; s &lt;= 60 ) ? s : 0;
  }
  
  void Time::setTime( int h, int m, int s )
  { // validating time time, if incorrent, set to 0
      hour = ( h &gt;= 0 &amp;&amp; h &lt;= 24 ) ? h : 0;
      minute = ( m &gt;= 0 &amp;&amp; m &lt;= 60 ) ? m : 0;
      second = ( s &gt;= 0 &amp;&amp; s &lt;= 60 ) ? s : 0;
  }
  ///void Time::tick();
  
  void Time::printStandard()
  {
      cout &lt;&lt; ((hour == 0 || hour == 12) ? 12 : hour % 12) &lt;&lt; ":" &lt;&lt;
          setfill('0') &lt;&lt; setw(2) &lt;&lt; minute &lt;&lt; ":" &lt;&lt; setw(2) &lt;&lt; second 
          &lt;&lt; ( hour &lt; 12 ? " AM" : " PM");
  }
  
  void Time::tick()
  {
      while (second &gt;=0 &amp;&amp; second &lt;=60)
      {
  
          if (second &lt; 59)
          {
              Sleep(1000);
              second++;
          }
          else  
          {   
              Sleep(1000);     
              minute++;
              second = 0;
              if (minute &gt;= 60)
              {
                  hour++;
                  minute = 0;
                  if (hour &gt;= 24)
                      hour = 0;
                  inDay();
              }
          }
      }
  };
  
  ///////////////////////////////////////////////////////////////////
  Watch::Watch()
  {}
  
  
  ////////////////////////////////////////////////////////////////// End Member Functions
  int main()
  {
  
      Watch watch1;
      watch1.displayWatch();
      watch1.printStandard();
      system("pause");  
  
      watch1.setDay();  
      watch1.displayWatch();
      watch1.printStandard();
      system("pause");  
  
      watch1.inDay();
      watch1.displayWatch();
      watch1.printStandard();
      system("pause");  
  
      return 0;
  } // end main
  </code></pre>