473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Design problem in C++

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.

Apr 3 '07 #1
6 1694
* 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).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 3 '07 #2
dragoncoder wrote:
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-lit...t.html#faq-5.2
Apr 3 '07 #3
On Apr 4, 7:41 am, "dragoncoder" <pktiw...@gmail.comwrote:
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;
}
};

Apr 4 '07 #4
On Apr 4, 3:41 am, "dragoncoder" <pktiw...@gmail.comwrote:
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

Apr 4 '07 #5
dragoncoder <pk******@gmail.comwrote:
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
--
Keith Davies "Sometimes my brain is a very strange
ke**********@kjdavies.org to live in."
ke**********@gmail.com -- Dana Smith
http://www.kjdavies.org/
Apr 4 '07 #6
On 4 Apr., 00:41, "dragoncoder" <pktiw...@gmail.comwrote:
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.

Apr 4 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: adb | last post by:
I came up with a replication configuration that is basically the result of all the restrictions of replication as well as the restrictions of allowable software on work PC's and I was curious if...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
0
by: Edward Diener | last post by:
In Borland's VCL it was possible to divide a component into design time and run time DLLs. The design time DLL would only be necessary when the programmer was setting a component's properties or...
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
2
by: Paul Cheetham | last post by:
Hi, I have moved an application from VS2003 to VS2005, and I am now unable to view most of my forms in the designer. The majority of the forms in my project are derived from class PACForm,...
1
by: Nogusta123 | last post by:
Hi, I have had a lot of problems getting web pages, master pages and content pages to render in VS2005 design view the same as they would in Internet Explorer. I did a lot of looking on the...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
19
by: neelsmail | last post by:
Hi, I have been working on C++ for some time now, and I think I have a flair for design (which just might be only my imagination over- stretched.. :) ). So, I tried to find a design...
10
by: vital | last post by:
Hi, I am designing the middle tier of a project. It has 6 classes and microsoft application data access block. The six classes are DBServices, Logger, ProjectServices ... etc. and all these...
4
by: Ken Fine | last post by:
I've been living with a frustrating issue with VS.NET for some months now and I need to figure out what the problem is. Hopefully someone has run into the same issue and can suggest a fix. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.