J
J.M.
I have a question concerning inheritance: can an abstract (parent) class
have an abstract object? I would like to make a concrete child inherit from
this class by inheriting from this object. Let me try to make this clear by
example: The following should be "abstract".
"class motor" is abstract because it has a virtual member
"virtual int calculate_horsepower() = 0"
"class car" should have
"motor M;"
"void print_info(){... M.calculate_horsepower(); ..... };"
The following should be concrete:
"class diesel_motor : class motor" and should implement
"virtual int calculate_horsepower(){ calculate hp for diesel };"
"class diesel_car : class car"
For this class, we should have an object "diesel_motor M;" as a child of
"motor M;". In other words, in diesel_car I need an object M as a
diesel_motor, but in the class car, I only need those attributes of M
associated with a motor.
It seems something of this sort is not possible, so maybe there is a
different solution. Let me indicate what I need:
Every car has a motor, either of diesel or gasoline type. Calculating
horsepower is different for each of these, but I want to implement this
method only once, once for each type of motor. For every car, I want to
print the same standardised specifications, so I want one method to do
that. This method only uses methods in available for all motors. In making
a class diesel_car, I need to have a diesel_motor, which is a child of
motor.
Obviously, I could just not have a motor in car, but I need this to define
print_info() - and I do not want to define this in every child....
I hope it is clear, what I need. Thanks for any help in advance.
Jan
have an abstract object? I would like to make a concrete child inherit from
this class by inheriting from this object. Let me try to make this clear by
example: The following should be "abstract".
"class motor" is abstract because it has a virtual member
"virtual int calculate_horsepower() = 0"
"class car" should have
"motor M;"
"void print_info(){... M.calculate_horsepower(); ..... };"
The following should be concrete:
"class diesel_motor : class motor" and should implement
"virtual int calculate_horsepower(){ calculate hp for diesel };"
"class diesel_car : class car"
For this class, we should have an object "diesel_motor M;" as a child of
"motor M;". In other words, in diesel_car I need an object M as a
diesel_motor, but in the class car, I only need those attributes of M
associated with a motor.
It seems something of this sort is not possible, so maybe there is a
different solution. Let me indicate what I need:
Every car has a motor, either of diesel or gasoline type. Calculating
horsepower is different for each of these, but I want to implement this
method only once, once for each type of motor. For every car, I want to
print the same standardised specifications, so I want one method to do
that. This method only uses methods in available for all motors. In making
a class diesel_car, I need to have a diesel_motor, which is a child of
motor.
Obviously, I could just not have a motor in car, but I need this to define
print_info() - and I do not want to define this in every child....
I hope it is clear, what I need. Thanks for any help in advance.
Jan