I was writing a tutorial for inheritance and it made me question something that may be interesting. Why is it that an object cannot determine which base object it inherits from based on the constructor?
For example:
Imagine that some unknown Alien species invaded earth. One half of the species was a reptillian humanoid breed, while the other looked more like monkeys.
Now consider these definitions for animal, reptile, and mammal:
Animal.h:
Code:
#ifndef _ANIMAL_
#define _ANIMAL_
//typedef char* string;
typedef const char Color;
typedef const char Gender;
typedef const char Texture;
class Animal
{
public:
Animal(void) {}
~Animal(void);
};
struct Colors
{
static Color
Red = 0,
Blue = 1,
Green = 2,
Cyan = 3,
Magenta = 4,
Yellow = 5,
Black = 6,
White = 7,
Brown = 8,
Orange = 9,
Purple = 10;
};
struct Textures
{
static Texture
Smooth = 0,
Rough = 1,
Ridged = 2;
};
struct Skin
{
public:
Skin(void) : _color(Colors::Black), _texture(Textures::Smooth) {}
Skin(Color color, Texture texture) : _color(color), _texture(texture) {}
~Skin(void) {}
private:
Color _color;
Texture _texture;
};
struct Appendage
{
public:
Appendage(void);
~Appendage(void);
};
struct Offspring
{
public:
Offspring(void);
~Offspring(void);
};
struct Genders
{
static Gender
Male = 0,
Female = 1,
Asexual = 2,
Unisex = 3;
};
#endif
Mammal.h:
Code:
#ifndef _MAMMAL_
#define _MAMMAL_
#include "animal.h"
struct Hair : public Skin
{
public:
Hair(void) {}
Hair(Color color, Texture texture) : Skin(color, texture) {}
~Hair(void) {}
};
class Mammal : public Animal
{
public:
Mammal(void) {}
Mammal(Hair hair) : Animal(), _hair(hair) {}
~Mammal(void);
rivate:
Hair _hair;
};
struct Baby : public Offspring
{
public:
Baby(void);
~Baby(void);
};
#endif
Reptile.h:
Code:
#ifndef _REPTILE_
#define _REPTILE_
#include "animal.h"
struct Scales : public Skin
{
public:
Scales(void) {}
Scales(Color color, Texture texture) : Skin(color, texture) {}
~Scales(void) {}
};
class Reptile : public Animal
{
public:
Reptile(void) {}
Reptile(/*string Name,*/ Scales scales) : Animal(/*Name*/), _scales(scales) {}
~Reptile(void);
private:
Scales _scales;
};
#endif
So...with this in mind, we would now define the aliens:
Alien.h:
Code:
#ifndef _ALIEN_
#define _ALIEN_
#include "reptile.h"
#include "mammal.h"
class ALIEN: public Mammal, public Reptile
{
public:
ALIEN(Scales scales) : Reptile(scales) {}
ALIEN(Hair hair) : Mammal(hair) {}
~ALIEN(void) {}
};
#endif
Humans would identify the monkey aliens as mammals and the reptile ones as reptiles even though they are from the same race. (Lets also pretend they don't produce offspring in the conventional methods so you can't argue that they really are.)
Here is a human:
Code:
class Human : public Mammal
{
public:
Human(Hair hair, Texture texture) : Mammal(hair, texture) {}
~Human(void);
string Identify(Animal animal); //This would give the class name as a string to identify the animal (Since even aliens would inherit from animal, they could be identified)
};
Now pretend that the alien mothership has a forcefield that doesn't allow any animals without the aliens' DNA to enter.
In our ship, we "pickup" aliens with a function similar to:
Code:
void PickUpAliens(Alien *alien)
{
CrewList.Add(alien);
}
This makes it so that ONLY aliens can be on the ship.
Now the situation here is as follows:
-Humans and Aliens are both Animals
-Humans are Mammals
-Aliens can be Mammals or Reptiles (according to humans)
-The Mothership will not allow for Humans or any other animals to come on board, other than aliens.
-The mothership puts Reptile Aliens to hard labor, and the mammal aliens to the command center.
So, in this situation (though it can be done many different ways), it would seem logical to have this "conditional inheritance" to quicken things up, even though it can be accomplished in different ways. (Just like how the strings quicken a lot in C#)
Now...assuming that there is an actual occasion where you need to inherit from either one class or another, as this example hopefully illustrates, what would be the downside to a language that allows for inheritance like that? Would it even be possible?
Keep in mind that I'm talking about conditional inheritance - meaning that aliens would determine the base properties from its constructor, and NOT from its declaration/definition.
Feel free to point out how stupid the idea is, if it is. Just please be sure to give a detailed description of why it would be.