I am write a program that both uses inheritance and composition. I am a bit new to inheritance so I am a little confused on some aspects of it.

Let's say I have a class definition like this

class Month
{
  Week *week[4];
}; // class Month

class Week
{
}; //class Week

class Day : public Week
{
} // class Day

I understand what it means if I create a Week object in week[0], but what if I create a Day object? Will week[0] be a Day object with Week characteristics? The classes in my program is composed of an array similar to this. Can I just do something like week[0] = new Day instead of week[0] = new Week to make it a Day object instead of what it was intended to be in my class definition?

Edited 7 Years Ago by red999: n/a

To start off with, based on your example, I'm not sure you've got the general idea of inheritance. In general, you want to inherit from another class if the class you're building has all the attributes of the class you're inheriting from, plus some unique to this child class.
If that confuses you, let me give you an example.

class Car
{
//a car has four wheels.
// a car has an engine
}

class Truck: public Car
{
//a truck ALSO has four wheels
//a truck ALSO has an engine
//a truck has a bed
}

so, you wouldn't want to inherit a Week object from a Day object, because you can't, in essence, say that a Day IS A Week.

On to your later question. If you were to say weeks[0] = new Day(); then yes, it would create a day object, but by accessing it through the weeks array, you could only access the attributes of the "week" portion of it that was inherited.

I kind of noticed my example was bad as I was typing it, but I went with it anyway =P. I'll just use your example since it is more clear. I'll like to add that I have an array *Car[8]
If I do Car[0] = new Truck(), how can I access the "Truck" portion if I do Car[0]?

I have two print() functions in both Car and Truck and both of which are virtual. I thought that if I do something like

Car[0] = new Truck();
  Car[0]->print();

then the compiler will know that I will be calling the print() function for the Truck class.

Edited 7 Years Ago by red999: n/a

That is correct, if the function in the parent class is declared as virtual, it will be overridden by the child class's method of the same prototype, no matter how you access the child class (including your array example above).

To start off with, based on your example, I'm not sure you've got the general idea of inheritance. In general, you want to inherit from another class if the class you're building has all the attributes of the class you're inheriting from, plus some unique to this child class.
If that confuses you, let me give you an example.

class Car
{
//a car has four wheels.
// a car has an engine
}

class Truck: public Car
{
//a truck ALSO has four wheels
//a truck ALSO has an engine
//a truck has a bed
}

so, you wouldn't want to inherit a Week object from a Day object, because you can't, in essence, say that a Day IS A Week.

On to your later question. If you were to say weeks[0] = new Day(); then yes, it would create a day object, but by accessing it through the weeks array, you could only access the attributes of the "week" portion of it that was inherited.

If you are going to critique his work, then give a better example.
Car does not have a "is a" relationship to truck. Instead you should
have a abstract base class and have both of them inherit from it.

If you are going to critique his work, then give a better example.
Car does not have a "is a" relationship to truck. Instead you should
have a abstract base class and have both of them inherit from it.

I disagree, I was using car AS a base class, seeing as the question "what kind of car do you drive?" would apply, regardless of whether or not you drove a truck, or an SUV, or a minivan.

However, that's truly irrelevant to the point. I wasn't trying to 'critique' his work per say, I was trying to ensure he truly understood the idea of inheritance.

I disagree, I was using car AS a base class, seeing as the question "what kind of car do you drive?" would apply, regardless of whether or not you drove a truck, or an SUV, or a minivan.

However, that's truly irrelevant to the point. I wasn't trying to 'critique' his work per say, I was trying to ensure he truly understood the idea of inheritance.

Just because a car is composed of similar things that a truck is
composed of does not mean a truck is a car( or the other way around).
It however suggests that they are composed of similar things, thus
each of their class should inherit from a base class that encapsulate
those similarities.

This article has been dead for over six months. Start a new discussion instead.