By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

metod, calculating ages with class Date

P: 4
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?
Jun 5 '07 #1
Share this Question
Share on Google+
2 Replies


P: 33
My guess:

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

Expand|Select|Wrap|Line Numbers
  1. int Calendar::today(void){
  2.     //Here somehow retrieve today's date, and try to have it in an integer format (like it does in Excel)
  3. }
  4.  
  5. int Calendar::getAge(student *stu){
  6.      return ((today() - stu->birthdate)/365.25)
  7.     //I'm assuming the birthdate is in the same format as today's date
  8. }
  9.  
Jun 5 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
This is not a good guess:

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:

Expand|Select|Wrap|Line Numbers
  1. class Student
  2. {
  3.  
  4.     private:
  5.        Calendar birthdate;
  6.     public:
  7.        int operator-(Calendar& dt);
  8.  
  9. };
  10.  
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.

Expand|Select|Wrap|Line Numbers
  1. int Student::Age(Calendar& dt)
  2. {
  3.      return this->birthdate - dt;
  4. }
  5.  
Jun 5 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.