![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
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. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
as far as i remember when i tried and failed at this you cant do it :/
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Feb 2005
Posts: 89
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
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();
}#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();
} |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|