Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:08:48+00:00 2026-05-30T00:08:48+00:00

I recently saw an OO design question on some forum and started thinking of

  • 0

I recently saw an OO design question on some forum and started thinking of using RTTI. However this must be bad design but I am unable to think of an alternative. Here is the simple question :

Create a C++ program for the following scenario using OO concepts –

My dog, named Buddy, lives in the backyard. He barks at night when he sees a cat or a squirrel that has come to visit. If he sees a frog, and he is hungry, he eats it. If he sees a frog and he isn’t hungry, he plays with it. If he has eaten 2 frogs already, and is still hungry, he will let it go. If he sees a coyote, he crys for help. Sometime his friend Spot stops by, and they chase each other. If he sees any other animal, he simply watches it. I would expect that you would have an animal class, and a cat, dog, squirrel, coyote class that inherits from the animal class.

I started thinking of having a see() method in the dog class which takes an Animal argument and then checks the actual type of the object (frog, cat etc) and takes the required action – play, chase etc depending on the actual type. However this would require RTTI which must be bad design. Can anybody please suggest a better design which would avoid RTTI and also point out the mistake in my thinking?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:08:50+00:00Added an answer on May 30, 2026 at 12:08 am

    There are a ridiculously large number of ways to satisfy this problem using “OO concepts,” depending on what you want to emphasize.

    Here’s the simplest solution that I can come up with:

    class Animal {
    public:
        virtual void seenBy(Buddy&) = 0;
    };
    
    class Buddy {
    public:
        void see(Cat&)      { /* ... */ }
        void see(Squirrel&) { /* ... */ }
        // ...
    };
    
    class Cat : public Animal {
    public:
        virtual seenBy(Buddy& b) { b.see(*this); }
    };
    
    class Squirrel : public Animal {
    public:
        virtual seenBy(Buddy& b) { b.see(*this); }
    };
    
    // classes for Frog, Coyote, Spot...
    

    If you need multiple kinds of “perceiving” animals, it’s straightforward to make a virtual wrapper for see (producing a form of double dispatch):

    // On a parent class
    virtual void see(Animal&) = 0;
    
    // On Buddy
    virtual void see(Animal& a) { a.seenBy(*this); }
    

    The above requires that the Animal class know something about the Buddy class. If you don’t like your methods being passive verbs and want to decouple Animal from Buddy, you can use the visitor pattern:

    class Animal {
    public:
        virtual void visit(Visitor&) = 0;
    };
    
    class Cat : public Animal {
    public:
        virtual void visit(Visitor& v) { v.visit(*this); }
    };
    
    class Squirrel : public Animal {
    public:
        virtual void visit(Visitor& v) { v.visit(*this); }
    };
    
    // classes for Frog, Coyote, Spot...
    
    class Visitor {
    public:
        virtual void visit(Cat&) = 0;
        virtual void visit(Squirrel&) = 0;
        // ...
    };
    
    class BuddyVision : public Visitor {
    public:
        virtual void visit(Cat&)      { /* ... */ }
        virtual void visit(Squirrel&) { /* ... */ }
        // ...
    };
    
    class Buddy {
    public:
        void see(Animal& a) {
            BuddyVision visitor;
            a.visit(visitor);
        }
    };
    

    The second mechanism could be used for purposes other than Buddy seeing an animal (possibly for that animal seeing Buddy). It is, however, more complicated.


    Note that OO is definitely not the only way to solve this problem. Other solutions exist that may be more practical for this problem, such as storing the properties of the various animals that cause Buddy to bark, eat, play, etc. This additionally decouples the Buddy class from the Animal class (even the visitor pattern needs an exhaustive list of everything that Buddy can perceive).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently saw some code that reminded me to ask this question. Lately, I've
I was reading a forum recently, and saw this comment: So, you see you've
I recently saw this demo . My question is: how is it possible in
Recently I saw some code like this: <tr> <th> Some label: </th> <td> <input
In some code I saw recently there was a structure defined like this: typedef
(This post is regarding High Frequency type programming) I recently saw on a forum
I recently saw a bit of code that looked like this (with sock being
i recently saw some videos on F#. it seems it used mainly for Service
I recently saw some Clojure or Scala (sorry I'm not familiar with them) and
I recently saw someone with a T-shirt with some Perl code on the back.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.