Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 20th, 2005, 5:38 PM   #1
Shatai
Newbie
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 6
Rep Power: 0 Shatai is on a distinguished road
Send a message via ICQ to Shatai Send a message via AIM to Shatai Send a message via MSN to Shatai
Multiple inheritance

I have a series of classes which represent specific behaviours. For example, "Creature", "BipedalCreature", "SentientCreature", "LightEmittingCreature", etc. From these various behavioural classes, I want to create a class which incorporates, say, the combined behaviours of "BipedalCreature", "SentientCreature", and "LightEmittingCreature", as an example. What would be the most reasonable/efficient way to mesh these classes, their functions, and perhaps per-class flags with C++'s multiple inheritance, or is this even a remotely possible use of it? I would rather not have a serious casting pig-sty for the solution, also.

My real goal is to keep the code as logically structured as possible for extensibility. I've pondered on how I can pull this off properly, and it's stumping me.

Thanks in advance,
James.
Shatai is offline   Reply With Quote
Old Apr 20th, 2005, 7:00 PM   #2
BaroN NighT
Programmer
 
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4 BaroN NighT is on a distinguished road
There are two common ways to relate two classes in a meaningful way: the "is-a" relationship(inheritance) and the "has-a" relationship(composition/containment). Bipedal creature "is-a" creature, sentient creature "is-a" creature and light emitting creature "is-a" creature. So obviously, this is inheritance.
BaroN NighT is offline   Reply With Quote
Old Apr 21st, 2005, 3:57 AM   #3
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
as far as i remember when i tried and failed at this you cant do it :/
Berto is offline   Reply With Quote
Old Apr 21st, 2005, 12:15 PM   #4
brkstf
Programmer
 
brkstf's Avatar
 
Join Date: Feb 2005
Posts: 89
Rep Power: 4 brkstf is on a distinguished road
the logical way is to have a Creature class with flags for bipedalism, lightemittance, etc.

include ALL of the various methods in the class definition, making sure to implement them in such a way that illegal functions return 0 and do nothing, based on the flags.
brkstf is offline   Reply With Quote
Old Apr 21st, 2005, 12:28 PM   #5
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
The big question is do you need polymorphic behavior? Does a SentientCreature need to be used through a reference or pointer to one of the base classes? If so then inheritance is likely to be your best option:
#include <iostream>

class Bipedal {
public:
  virtual void lose_balance() = 0;
};

class Sentient {
public:
  virtual void ponder() = 0;
};

class Creature {
public:
  virtual void reproduce() = 0;
};

class Human: public Creature, public Bipedal, public Sentient {
public:
  void reproduce() { std::cout<<"Getting funky"; }
  void lose_balance() { std::cout<<"Falling over"; }
  void ponder() { std::cout<<"Thinking about "; }
};

int main()
{
  Human eggy;
  Creature& thingy = eggy;
  Bipedal& twoleggy = eggy;
  Sentient& thinky = eggy;

  eggy.ponder(); eggy.lose_balance(); std::cout<<std::endl;
  thingy.reproduce(); std::cout<<std::endl;
  twoleggy.lose_balance(); std::cout<<std::endl;
  thinky.ponder(); std::cout<<std::endl;

  std::cin.get();
}
If you don't need polymorphic behavior then a policy based design may be more elegant and easier to understand, if not easily scalable for large numbers of policies:
#include <iostream>

class NullType {};

class Bipedal {
public:
  void do_legs() { std::cout<<"Falling over"; }
};

class Sentient {
public:
  void do_brain() { std::cout<<"Thinking about "; }
};

template <
  typename Legs = NullType,
  typename Brain = NullType,
  typename Body = NullType
>
class Creature {
  Legs _legs;
  Brain _brain;
  Body _body;
public:
  void ponder() { _brain.do_brain(); }
  void lose_balance() { _legs.do_legs(); }
};

int main()
{
  Creature<Bipedal, Sentient> eggy;

  eggy.ponder(); eggy.lose_balance(); std::cout<<std::endl;

  std::cin.get();
}
Be very careful in using the "is-a" relationship. It can be too easily justified for hierarchies that have no business using inheritance.
Eggbert is offline   Reply With Quote
Old Apr 30th, 2005, 8:16 PM   #6
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
yeah, sounds like you need to implement some virtual functions to define specific behaviors of the derived classes.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:33 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC