473,799 Members | 3,817 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_horse power() = 0"

"class car" should have
"motor M;"
"void print_info(){.. . M.calculate_hor sepower(); ..... };"

The following should be concrete:

"class diesel_motor : class motor" and should implement
"virtual int calculate_horse power(){ calculate hp for diesel };"

"class diesel_car : class car"

For this class, we should have an object "diesel_mot or 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 1771
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_horse power() = 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_horse power() = 0;
virtual ~motor() {}
};

class diesel_motor : public motor
{
public:
virtual int calculate_horse power()
{
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_hors epower() << "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_hor sepower(); ..... };"

The following should be concrete:

"class diesel_motor : class motor" and should implement
"virtual int calculate_horse power(){ calculate hp for diesel };"

"class diesel_car : class car"

For this class, we should have an object "diesel_mot or 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_horse power() = 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_hor sepower(); ..... };"

The following should be concrete:

"class diesel_motor : class motor" and should implement
"virtual int calculate_horse power(){ calculate hp for diesel
};"

"class diesel_car : class car"

For this class, we should have an object "diesel_mot or 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_9 8(double volume_purchase d);"
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_purchase d 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_horse power() = 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_hor sepower(); ..... };"

The following should be concrete:

"class diesel_motor : class motor" and should implement
"virtual int calculate_horse power(){ calculate hp for diesel
};"

"class diesel_car : class car"

For this class, we should have an object "diesel_mot or 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_9 8(double volume_purchase d);"
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_purchase d 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_moto r>
{
};

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_9 8(double volume_purchase d);"
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_purchase d 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_moto r>
{
};

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
4186
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, relating to relationship graphing or something) and some stored procs begining with "dt_" (some kind of source control stuff, possible visual studio related). These show up when I use "exec sp_help 'databaseName'"
6
2024
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 understanding that only "named" storage areas(from the standard) were data objects. Since constants and string literals don't have lvalues, they can't be data objects. Yet, I was shown the first page of chapter 2 in K&R2, which states that variables...
2
1389
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 the Web Form designer. Please correct the following error, then load it again: Type Abstract" This has only just started happening, as I've had the inheritance working for a good week or so now. I've no
5
2053
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 term? Stupid question, I know ... and quite irrelevant in many ways ... but I really would like to know!
8
4046
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 'virtual constructor' - ie if the 'clone()' method is called on a polymorphic type, an object of the same (unknown) type is returned. */
0
1256
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; String *EventName; USHORT EventNum; String *EventType;
0
2681
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 looked for one on the net (I did browse the boost library though). It doesn't matter right now, anyway. To put it simply, Abstract Iterator is mainly a wrapper class. It helps
3
3482
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 xmlns="http://www.w3.org/2001/XMLSchema" xmlns:this="this" targetNamespace="this" elementFormDefault="qualified"> <complexType name="abstractAnswerType" abstract="true"/> <complexType name="yesWithDescType">
14
6028
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 static inside functions (i.e. local static objects) 5. objects declared at file scope.
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9543
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.