I was experimenting with inheritance and I came across a particular behaviour. First, look at the code:
class animal
{
public:
int ID;
animal(int id) : ID(id)
{
cout<<"I am an animal and I am not a terrorist. Here is my ID: "
<<ID<<endl;
}
};
class lion : virtual public animal
{
public:
lion(int id) : animal(id)
{
cout<<"I am a lion and I am not a terrorist. Here is my ID: "
<<ID<<endl;
}
};
class tiger : virtual public animal
{
public:
tiger(int id) : animal(id)
{
cout<<"I am a tiger and I am not a terrorist. Here is my ID: "
<<ID<<endl;
}
};
class liger : public lion, public tiger
{
public:
liger(int id) : lion(id), tiger(id), animal(id)
{
cout<<"I am a liger and I am not a terrorist. Here is my ID: "
<<ID<<endl;
}
};
When the constructor of liger was liger(int id) : lion(id), tiger(id), animal(id)...
and I created a object like liger l(444)
then I got the following expected output:
I am an animal and I am not a terrorist. Here is my ID: 444
I am a lion and I am not a terrorist. Here is my ID: 444
I am a tiger and I am not a terrorist. Here is my ID: 444
I am a liger and I am not a terrorist. Here is my ID: 444
Then I changed it to to liger(int id) : lion(55), tiger(55), animal(id)
but it too gave the same output. Now, my question is, if the arguments to the lion
and tiger
constructors are neglected, then what is their purpose?