473,662 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multiple inheritence question

Car is a class, with four wheel variables. SUV is a class with four
wheel variables. Outback is a class that inherits from Car and SUV.
How many wheel variables does Outback have? (In general, how does
multiple inheritance work?)

Jul 19 '05 #1
4 2465
"Michael C. Starkie" <mi**@bogus.com > wrote...
Car is a class, with four wheel variables. SUV is a class with four
wheel variables. Outback is a class that inherits from Car and SUV.
How many wheel variables does Outback have? (In general, how does
multiple inheritance work?)


Well, in presented hierarchy the Outback object will have 8 wheels.
4 in its Car subobject, 4 in its SUV subobject. However, it would
probably be more correct to define the hierarchy this way:

class FourWheelVehicl e {
Wheel wheels[4];
...
};

class Car : public virtual FourWheelVehicl e { .. };
class SUV : public virtual FourWheelVehicl e { .. };
class Outback : public Car, public SUV { .. };

which will provide you with only one 'FourWheelVehic le' subobject in
Outback, and therefore only four wheels.

The reason I suggest extracting FourWheelVehicl e into a separate
base class is because there is too much common between Car and SUV
to ignore that. The common functionality and implementation should
be placed in the common base class.

Victor
Jul 19 '05 #2
On Wed, 13 Aug 2003 23:34:53 GMT, "Michael C. Starkie" <mi**@bogus.com > wrote:
Car is a class, with four wheel variables. SUV is a class with four
wheel variables. Outback is a class that inherits from Car and SUV.
How many wheel variables does Outback have? (In general, how does
multiple inheritance work?)


Why does SUV not inherit from Car? That would solve your four-wheel problem.

Multiple inheritance is exactly what it says--your derived class will have a
combination of members from all of its parents.

Jul 19 '05 #3
> <snip>
class FourWheelVehicl e {
Wheel wheels[4];
...
};

class Car : public virtual FourWheelVehicl e { .. };
class SUV : public virtual FourWheelVehicl e { .. };
class Outback : public Car, public SUV { .. };

which will provide you with only one 'FourWheelVehic le' subobject in
Outback, and therefore only four wheels.


<snip>

A little confused victor, Is that not 12 wheels as Outback will have
one Car subobject,one SUV subobject and one FourWheelVehicl e subobject??


Jul 19 '05 #4
Senthilvel Samatharman wrote:
<snip>
class FourWheelVehicl e {
Wheel wheels[4];
...
};

class Car : public virtual FourWheelVehicl e { .. };
class SUV : public virtual FourWheelVehicl e { .. };
class Outback : public Car, public SUV { .. };

which will provide you with only one 'FourWheelVehic le' subobject in
Outback, and therefore only four wheels.


<snip>

A little confused victor, Is that not 12 wheels as Outback will have
one Car subobject,one SUV subobject and one FourWheelVehicl e
subobject??


I could understand if you think 8, but where would the extra
FourWheelVehicl e come from?
Anyway, since Car and SUV inherit virtually from FourWheelVehicl e, there
will only be one copy of that base class in Outback. The Car and SUV
subobjects of Outback share their FourWheelsVehic le.

Jul 19 '05 #5

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

Similar topics

2
4328
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
7
1978
by: preetam | last post by:
Hi, This question is more towards design than towards c++ details. By looking at books on design patterns and various google threads on the same topic, I see that composition is favoured to inheritence. One more article I read was Allen holub's "Why extends is evil". http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html One general advice I found was that - extending behaviour is better done with interfaces and...
5
2107
by: Tony Johansson | last post by:
Hello Experts! I just play around just to try to understand this about multiple inheritance. You have all the class definition below and at the bottom you have the main program. So here I use multiple inheritance. One top class called MBase and below this we have D1 and D2 on the same level.
22
23347
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
5
1429
by: Bob Rundle | last post by:
I heard that managed C++ in VS 2005 supports multiple inheritence. Is this true? Bob Rundle
7
3383
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always either AND or OR but never mixed together. We can use Northwind database for my question, it is very similar to the structure of the problem on the database I am working on. IF(SELECT OBJECT_ID('REPORT')) IS NOT NULL DROP TABLE REPORT_SELECTION
5
2119
by: Neelesh Bodas | last post by:
This might be slightly off-topic. Many books on C++ consider multiple inheritence as an "advanced" concept. Bruce Eckel says in TICPP, volume 2 that "there was (and still is) a lot of disagreement about whether is essential in C++". Are there any disadvantages of using multiple inheritence?
47
4006
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever support multiple inheritance. In C++ for instance I noticed that the compiler flags an error if you use the "ref" keyword on a class with multiple base classes. This supports the above quote. However, under the "CodeClass2.Bases" property (part...
4
3342
by: Mukesh_Singh_Nick | last post by:
Does PHP support multiple inheritence? If not, how would one workaround it when one needs a class to inherit from multiple classes?
0
8432
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
8343
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
8856
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8762
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...
0
8633
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...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4179
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...
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1747
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.