metod, calculating ages with class Date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • him1979
    New Member
    • Jun 2007
    • 4

    metod, calculating ages with class Date

    If i have a class desribing dates from the calendar and second class Student with members:
    name, date of birth(using the first class)

    how could i write a method for the second class with parameter date that returns the ages of the student?
  • Rasputin
    New Member
    • Jun 2007
    • 33

    #2
    My guess:

    create a method in the class Calendar that takes the object parameter Student. The method will be looking like:

    Code:
    int Calendar::today(void){
        //Here somehow retrieve today's date, and try to have it in an integer format (like it does in Excel)
    }
    
    int Calendar::getAge(student *stu){
         return ((today() - stu->birthdate)/365.25)
        //I'm assuming the birthdate is in the same format as today's date
    }

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      This is not a good guess:

      Originally posted by Rasputin
      My guess:

      create a method in the class Calendar that takes the object parameter Student. The method will be looking like:
      The reason is that the Calendar class and the Student are not related. The relationship is the other way: The Student HAS-A Calendar:

      [code=cpp]
      class Student
      {

      private:
      Calendar birthdate;
      public:
      int operator-(Calendar& dt);

      };
      [/code]

      You write an operator- method on the Calendar to take the difference between two dates. You call it from your Student class with the birthdate and a date that is today's date.

      [code=cpp]
      int Student::Age(Ca lendar& dt)
      {
      return this->birthdate - dt;
      }
      [/code]

      Comment

      Working...