472,958 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Inheritance: "abstract objects"?

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
Mar 16 '06 #1
4 1726
J.M. wrote:
I have a question concerning inheritance: can an abstract (parent) class
have an abstract object?
No. The whole idea of an abstract class is that it can't be instantiated.
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"
It is abstract because it has a _pure_ virtual member. Make it just virtual,
and the class won't be abstract anymore. But I guess what you really want
is not store a motor in your car, but rather a pointer to motor, like the
following simplified example:

#include <iostream>

class motor
{
public:
virtual int calculate_horsepower() = 0;
virtual ~motor() {}
};

class diesel_motor : public motor
{
public:
virtual int calculate_horsepower()
{
std::cout << "lots of horses in that diesel\n";
return 300;
}
};

class car
{
public:
car(motor* M)
:M_(M)
{}

void print_info()
{
std::cout << M_->calculate_horsepower() << "HP\n";
}
~car() { delete M_; }
private:
motor* M_;
};

int main()
{
car mercedes(new diesel_motor);
mercedes.print_info();
}

"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.
Well, if you want to instantiate motor, simply don't make it abstract.
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.


Why exactly do you need a diesel_car? Just let your car have a pointer to
motor, and it can have any motor that you have a class for.

Mar 16 '06 #2
Rolf Magnus schrieb:

Thanks for your response.

J.M. wrote:
I have a question concerning inheritance: can an abstract (parent) class
have an abstract object?
No. The whole idea of an abstract class is that it can't be instantiated.


Yup. I figured that much -- as I indicated, I did not expect that to
work ;-)
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"
It is abstract because it has a _pure_ virtual member. Make it just
virtual, and the class won't be abstract anymore.


That would work, but that would require some sort of implementation, would
it not? But without knowing the type of motor, I can't calculate the
horsepower....
But I guess what you
really want is not store a motor in your car, but rather a pointer to
motor,
Actually, I would like to store the motor.. Granted, a pointer would work
too, sort of.. see below.

[zap example]
"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.
Well, if you want to instantiate motor, simply don't make it abstract.


Well, I don't "really" want to instantiate it - I want it to be part of an
abstract class :)
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.


Why exactly do you need a diesel_car? Just let your car have a pointer to
motor, and it can have any motor that you have a class for.


Well, the class diesel_car and gasoline_car would have specific methods that
car (or motor) would not have. For example
"void refuel_octane_98(double volume_purchased);"
might be specific to a gasoline_car. It would not work as a method for car,
because the type of fuel a car needs depends on the motor. On the other
hand, I cannot let this method be associated with motor, because I want to
check that volume_purchased fits into the car - and that depends on the
tank size and the current tank contents. So ideally, when defining the
class "diesel_car" as derived from "car" the motor M in car should become a
diesel_motor.... But I suppose there is no way of doing that.

Jan

Mar 16 '06 #3
J.M. wrote:
"class motor" is abstract because it has a virtual member
"virtual int calculate_horsepower() = 0"


It is abstract because it has a _pure_ virtual member. Make it just
virtual, and the class won't be abstract anymore.


That would work, but that would require some sort of implementation, would
it not? But without knowing the type of motor, I can't calculate the
horsepower....


Right.
"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.


Well, if you want to instantiate motor, simply don't make it abstract.


Well, I don't "really" want to instantiate it - I want it to be part of an
abstract class :)


If it is part of a class (be it abstract or not), then this class will
contain an instance of the motor (and exactly that).
Why exactly do you need a diesel_car? Just let your car have a pointer to
motor, and it can have any motor that you have a class for.


Well, the class diesel_car and gasoline_car would have specific methods
that car (or motor) would not have. For example
"void refuel_octane_98(double volume_purchased);"
might be specific to a gasoline_car. It would not work as a method for
car, because the type of fuel a car needs depends on the motor. On the
other hand, I cannot let this method be associated with motor, because I
want to check that volume_purchased fits into the car - and that depends
on the tank size and the current tank contents. So ideally, when defining
the class "diesel_car" as derived from "car" the motor M in car should
become a diesel_motor.... But I suppose there is no way of doing that.


Maybe you could work with templates, like:

template <class T> class car
{
/*...*/
protected:
T M_;
};

class diesel_car: public car<diesel_motor>
{
};

Then you can derive from car, and every derived class will contain the motor
that belongs to it.

Mar 16 '06 #4
Rolf Magnus schrieb:

That would work, but that would require some sort of implementation,
would it not? But without knowing the type of motor, I can't calculate
the horsepower....
Right.


I suppose a dummy implementation could work, but I do not really like that.
On the other hand, there is nothing wrong having a dummy implementation for
a dummy motor ;-)))


Well, if you want to instantiate motor, simply don't make it abstract.


Well, I don't "really" want to instantiate it - I want it to be part of
an abstract class :)


If it is part of a class (be it abstract or not), then this class will
contain an instance of the motor (and exactly that).


Yes, I know, which is why the really was in quotes ;-)
Why exactly do you need a diesel_car? Just let your car have a pointer
to motor, and it can have any motor that you have a class for.


Well, the class diesel_car and gasoline_car would have specific methods
that car (or motor) would not have. For example
"void refuel_octane_98(double volume_purchased);"
might be specific to a gasoline_car. It would not work as a method for
car, because the type of fuel a car needs depends on the motor. On the
other hand, I cannot let this method be associated with motor, because I
want to check that volume_purchased fits into the car - and that depends
on the tank size and the current tank contents. So ideally, when defining
the class "diesel_car" as derived from "car" the motor M in car should
become a diesel_motor.... But I suppose there is no way of doing that.


Maybe you could work with templates, like:

template <class T> class car
{
/*...*/
protected:
T M_;
};

class diesel_car: public car<diesel_motor>
{
};

Then you can derive from car, and every derived class will contain the
motor that belongs to it.


Yes, I think that should work. In fact, that was the solution I was looking
for. Now that you have pointed it out to me, I recall having seen a
solution using templates before, but obviously, I had forgotten it...
Thanks for you help!

Jan

Mar 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave C. | last post by:
Hi, I have a few things on my databases which seem to be neither true system objects or user objects - notably a table called 'dtproperties' (created by Enterprise manager as I understand,...
6
by: kobu.selva | last post by:
I was recently part of a little debate on the issue of whether constants and string literals are considered "data objects" in C. I'm more confused now than before. I was always under the...
2
by: Ben Fidge | last post by:
I've got a couple of UserControls that are derived from a common base-class. When I try to open them in the IDE, the following error is displayed in a dialog window: "The file failed to load in...
5
by: G. Stewart | last post by:
The word "Business" in the term implies some sort of commercial aspects or connotations. But from what I can see, that is not necesserially the case at all? So what is the reasoning behind the...
8
by: Asfand Yar Qazi | last post by:
Hi, I have the following header file in my 'everything useful I think of in one place' library: ============= BEGIN CODE SNIPPET =========== /** All classes that derive from this obtain a...
0
by: Ken Varn | last post by:
I have the following code that I cannot get to compile. I get the error "A Sealed class cannot be abstract." public __value struct DVRAVIFileHeader : public ISerializable { double EventID;...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
3
by: Lord0 | last post by:
I am trying to implement variable content containers using an abstract type and type substitution. My schema is as follows: <?xml version="1.0" encoding="UTF-8"?> <schema...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.