Advertisement

Inheritance

Started by May 23, 2001 11:28 AM
2 comments, last by night_owl 23 years, 8 months ago
When should a person use multiple inheritance over single inheritance? When should a person use containment over single inheritance? When should a person use single inheritance over multiple inheritance and containment?
"Pssst! Did you get the answer to number 6?"

"Not yet..since it''s take-home, I''ll ask around some boards"

"Cool!"
Advertisement
Think of it like this:

1. Use inheritance when the relationship is ''is-a''
2. Use containment when the relationship is ''has-a''

Example:

Dog ''is-a''(n) Animal
German Sheppard ''is-a'' type of Dog

A flower ''is-a'' type of plant (CFlower derives from CPlant)
A flower ''has-a'' petals (CPetals class).
A flower ''has-a'' stem (CStem class)

You should generally try to avoid multiple inheritance (even though it is very prevalent in COM interface design). Multiple inheritance is generally useful when a subclass has a ''is-a'' relationship between more than one super class.

The classic example:

Amphibious Vehicle ''is-a'' Land Vehicle AND Water Vehicle. Though I''d never model the relationship this way, it is a valid, albeit, contrived example.

Hope this helps,



Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
quote:

When should a person use multiple inheritance over single inheritance? When should a person use containment over single inheritance? When should a person use single inheritance over multiple inheritance and containment?



When you need to.


If you really want to impress your teacher (or perhaps confuse) discuss the particulars of interface, implementation, and behavioral inheritance.

If you knew what you were asking, you wouldn't need to ask.


Magmai Kai Holmlor
- The disgruntled & disillusioned


Edited by - Magmai Kai Holmlor on May 24, 2001 2:03:12 AM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement