Hello,

I have a very general question about object-orientation programming. I have been programming for a few years, but I'm just wondering what the best practice is for this particular situation.

Suppose that I have a class, which contains an object of a second class. When I want to update the member variables of the second class, should I update these variables directly from the main program loop, or should I update them indirectly from a function in the first class?

For example, consider a class "Person", which has an object of type "Age" as a member variable. In my main program loop, each loop represents one day in the life of the person, and so in each loop I want to increase the age of the person by one day. One method for doing this is to directly change the number of days and years from the main program loop. A second method is to call a function in Person, which then directly changes the number of days and years. The third method is to call this function in Person, which then calls a function in Age, which itself changes the number of days and years.

Below are the three ways I have described, but which is the best one? The first approach is the fastest, but is less elegant. The third approach makes the code the most readable, but if there are lots of embedded classes within each other, this seems quite an inefficient method. So I'm just wondering, in the case of lots of embedded classes, how far down do you go before you access the data directly, rather than calling lots of functions?

Thanks!

Method 1 (direct)
Code:
class Age
{
public:
     int num_days;
     int num_years;

     Age() : num_days(0), num_years(0)
};

class Person
{
public:

     Age age;
};

int main()
{
     Person david;
     int total_num_days = 1000;

     for (int i = 0; i < total_num_days; i++)
     {
          david.age.num_days++;

          if (david.age.num_days % 365 == 0)
          {
               david.age.num_years++;
          }

          // Do some other stuff for that day
     }

     return 0;
}
Method 2 (semi-direct)
Code:
class Age
{
public:
     int num_days;
     int num_years;

     Age() : num_days(0), num_years(0)
};

class Person
{
public:

     Age age;

     void AddDay()
     {
          age.num_days++;

          if (age.num_days % 365 == 0)
          {
               age.num_years++;
          }
     };
};

int main()
{
     Person david;

     int total_num_days = 1000;

     for (int i = 0; i < total_num_days; i++)
     {
          david.AddDay();

          // Do some other stuff for that day
     }

     return 0;
}
Method 3 (indirect)
Code:
class Age
{
public:
     int num_days;
     int num_years;

     Age() : num_days(0), num_years(0)

     AddDay()
     {
          age.num_days++;

          if (age.num_days % 365 == 0)
          {
               age.num_years++;
          }
     }
};

class Person
{
public:

     Age age;

     void AddDay()
     {
          age.AddDay();
     };
};

int main()
{
     Person david;
     int total_num_days = 1000;

     for (int i = 0; i < total_num_days; i++)
     {
          david.AddDay();

          // Do some other stuff for that day
     }

     return 0;
}