Multi VS Single Inheritance

Started by
7 comments, last by wah_on_2 22 years, 5 months ago
I know C++ can support Multi-Inheritance. What are the advantages of Multi-Inheritance? Which one is better of multi or simgle Inheritance? Thx
Advertisement
Single inheritance is simpler to understand and work with. Multiple inheritance makes everything alot more complicated, but that just my opinion.
Well advantages are 1)If you want to derive a class from 2 different classes like in SAMS C++ 21 days let say you have a horse class

class Horse : public Animal
{
/// members
// member functions
};
and a class Bird
class Bird : public Animal
{
public:
void fly()
};
and you want to make a horse that fly''s
you would use multiple inheritance Ive been working w/ Java a lot lately but I think the syntax is
class Pegesus : public Horse, public Bird
{
//we now have a flying horse
};

If you only had single inheritance you would have to make a bird class in the liniage of the horse animal-->horse--->bird(NOT GOOD) better ---> Horse--__
Animal- __ --->Pegesus
---> Bird--

again sorry if the syntax is incorrect
==--When the time comes you will realize but no sooner.--==
Multiple inheritance means that you''ve got two or more parent classes or interfaces for a class. Sometimes, you''ll want a class that''s derived from another class, and uses an interface (a struct, but used in a special way not discussed here, or an ADT). Or you''ll have one huge class, with multiple interfaces. Interfaces, pretty much, are treated as if they were classes when dealing with inheritance, at least by the compiler.

All multiple inheritance complicates is knowledge of where everything is coming from. So really, just extra hunting through your code. It''s not been a problem for me.


coldacid

Meldstar Studios - Creation, cubed.

Chris 'coldacid' Charabaruk – Programmer, game designer, writer | twitter

MI lets you create re-usable implementation code.
Even so-called SI systems allow you to multiple inherit interfaces (because they''re worthless without that).

In a language without native MI capiblities, you resort to aggregation or containment - which are viable solutions, but more work on part of the coder - to accomplish the same goals.

The ATL is a good example of how templates and MI can be used to drastically reduce the amount work that''s required to implement a class.

Understanding how MI works isn''t that complicated. Think of it as a tree, you have branches of inheritence. You can freely and safely move up & down any single branch. If you want to switch branches you need special code to make sure it''s safe (like RTTI or QueryInterface).

Multi is better, because you have the choice of a SI or MI based design and implementation.

Magmai Kai Holmlor
- Not For Rent
- 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
oic. thx a lot of
Just learn the pitfalls and advantages of MI, don't buy the JAVA-gay crap about it being evil or something, it's just a powerful programming-tool.

As with any tool u have to learn how to use it.

A good start can be to use it just like Interfaces are used in Java.
// abstract baseclass, you can't create a object of this classclass Game{public:   // make sure the destructor is virtual in a abstract baseclass   virtual~Game(){}   virtual void Action()=0; // Pure virtual func};// abstract baseclass, you can't create a object of this classclass ResManagement{public:// make sure the destructor is virtual in a abstract baseclass   virtual ~ResManagement() {} virtual void Create() =0; // makes the class abstractvirtual void Release() =0;};class DXWrapper{// DX objects...};class GameManager : public DXWrapper, protected Resources, public Game{public:// must implement Action      void Action();// must implement Create    void Create();// must implement Release void Release();}; 


I just typed this on the fly as an example, to make a real world working example it would make sense to think it through a little more, test, design, re-design etc



Edited by - Lowas on November 8, 2001 10:56:33 AM
C++, C++, C++
BTW:

How do I format the text into code format???

Edited by - Lowas on November 8, 2001 8:10:00 AM
C++, C++, C++
quote:Original post by Lowas
BTW:

How do I format the text into code format???

Edited by - Lowas on November 8, 2001 8:10:00 AM


Put stuff between [''code] and [/''code] (without the '')


Reach out and torch someone.
-------homepage - email

This topic is closed to new replies.

Advertisement