Design problem in C++

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.
 
A

Alf P. Steinbach

* 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.

The short answer is to make uselegs() virtual.

The little bit longer answer is to recognize that a C++ class hierarchy
models static knowledge of an idealized simplified abstraction of something.

To model something more messy, such as animals sans legs, discovery of
new animal types, etc., you need a more messy, in practice dynamic, model.

So it's all about choosing the right tool for the job.

Decide what you want to achieve, choose the right tools, revise the
choice as the original decision about what to achieve is revised (and
yep, that incurs some costs).
 
R

red floyd

dragoncoder said:
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.

Your answer is a FAQ.
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2
 
S

Sarath

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.

Hello,

There may be different alternatives as solution for your problem.
But still the following seems to be very simple in sense for me.
Surely legs should be the attributes of an animal. For your particular
case constructors will help you which can re-initialize/re-assign the
attributes of base class. All you need is to declare a protected
variable inside the Animal class which can store the number of legs.
HTH

If you dont want to store number of legs in your class, only virutal
functions could help you out in this matter. Please see the sample
snippet.

class Animal
{
protected:
int m_nLegs;

public:
Animal()
{
m_nLegs = 4;
}
virtual void run() { uselegs(); }
private:
void uselegs() { std::cout<<"Running with "<<m_nLegs<<" Legs"; }
};

class Dog: public Animal{};
class Cat: public Animal{};
class Cow: public Animal{};
class Man: public Animal
{
public:
Man()
{
m_nLegs = 2;
}
};
 
S

Sheth Raxit

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 } You are fixing number of 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".
because when 'designing' your classes, you did not realize that any
animal can come with 0,1,2,...16.. legs.

i.e. Man class should after all have 2 legs and any Operation/function
performed on Man, take 2 legs.

can you make number_of_legs variable in your base class, ?
when object of new animal is created you must specify how many legs
the animal has (or default).- hint: constructor.

if your animal do surgeries, you need to consider, AddLegs/RemoveLegs
or ChangeLegs.-

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,

Better to change Animal Class, without breaking down existing Derived
class, so less change.
with the least change in the code and not making the design bad.
Please help.

Thanks in advance.

--raxit
 
K

Keith Davies

dragoncoder said:
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.

The correct answer is probably to redesign the classes somewhat:

// is 'interface'
class Runner {
public:
virtual void run() = 0;
};

class FourLeggedAnimal : public Runner {
public:
void run() { run_with_four_legs(); } // compiler uninlines
private:
void run_with_four_legs();
};

/* classes Cat, Dog, Emu, etc. */

class Man : public Runner {
public:
void run() { run_like_forrest(); } // compilier uninlines
private:
void run_like_forrest();
};

This could, of course, shatter things because the interfaces and class
signatures change. You might not have the luxury of redesigning the
parent class.

A *pragmatic* answer, not as correct as actually fixing things to
correctly incorporate your desired change, is to simply override run()
in Man (that's why it's virtual). I'd probably also put a note on it
saying 'FIX THIS' or 'REVIEW THIS'.

This doesn't break current interfaces or class signatures. It may also
be code that ranges in naughtiness from 'mild' to 'Catholic schoolgirl
nympho', depending on the real context. (I'm assuming this is a
simplified version of a RL problem, rather than homework.)


Keith
 
S

slot

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.

I'm not an animal scientist but isnt the animal familiy divided up
into "2 legged animals" and "4 legged animals"? then you got the
spiders and so on. Shouldnt you solve it by making the leg problem to
a class problem? Fore example you could make a private int in each
animal class and set it to the amonut of legs that the specific animal
has.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,779
Messages
2,569,606
Members
45,239
Latest member
Alex Young

Latest Threads

Top