D
dragoncoder
Hello experts,
This is actually a design problem which I want to solve using c++.
I have the following class.
class Animal
{
public:
virtual void run() { uselegs(); }
private:
void uselegs() { // Use 4 legs }
};
And I have the following animals
class Dog: public Animal
{
};
class Cat: public Animal
{
};
class Cow: public Animal
{
};
Now One more animal comes.
class Man: public Animal
{
};
But Man has only 2 legs so it can not "Use 4 legs".
I understand that in this case Man *_is_not_* an animal but except
this "Use 4 legs" property everything is same. My question is how can
I reuse my existing Animal class so that Man can use 2 legs to walk,
with the least change in the code and not making the design bad.
Please help.
Thanks in advance.
This is actually a design problem which I want to solve using c++.
I have the following class.
class Animal
{
public:
virtual void run() { uselegs(); }
private:
void uselegs() { // Use 4 legs }
};
And I have the following animals
class Dog: public Animal
{
};
class Cat: public Animal
{
};
class Cow: public Animal
{
};
Now One more animal comes.
class Man: public Animal
{
};
But Man has only 2 legs so it can not "Use 4 legs".
I understand that in this case Man *_is_not_* an animal but except
this "Use 4 legs" property everything is same. My question is how can
I reuse my existing Animal class so that Man can use 2 legs to walk,
with the least change in the code and not making the design bad.
Please help.
Thanks in advance.