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 6370361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:55:15+00:00 2026-05-25T00:55:15+00:00

I encountered a question today, found here , which raised this question for me.

  • 0

I encountered a question today, found here, which raised this question for me.

Here’s a pseudo-code example of what I’m getting at:

class Car{
public:
    virtual int goFast() = 0;
};


class FordFocus : public Car {
public:
    int goFast(){
        return 35;
    };
};


class Lamborghini : public Car {
    bool roof;
public:
    int goFast(){
        return -1/0;  // crash 
    };
    void retractTheRoof(){
        roof = 0;
    };
};



class RichGuy {
    vector<Car *> cars;
public:
    void goDrive() {

        for(int i = 0; i < cars.size(); ++i) {
            if(Lamborghini* lambo = dynamic_cast<Lamborghini*>(cars[i])) {
                lambo->retractTheRoof();
            };
            goFast();
        };
    };
};

In the example, there is a RichGuy class. Richguy only keeps track of his Cars in a single vector. Because he has so many Cars it would be too troublesome to keep track of them based on if they’re a FordFocus or a Lamborghini. However, the only type of car he has with a retractable roof is the Lambo. In order to retractTheRoof(), RichGuy must now determine if the Car he has is indeed a Lamboghini, and then downcast to execute this function of it.

Based on this example, was the choice to downcast in good design? Or did it violate the purpose of polymorphism, assuming that purpose is to allow derived classes to define their own behavior, and provide a common interface for classes like RichGuy? And if so, is there a better way to allow for functions like retractTheRoof() (or at least it’s effect) to be available to RichGuy to use?

  • 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-25T00:55:16+00:00Added an answer on May 25, 2026 at 12:55 am

    Now if there are more than one type of cars which is retractable, say such cars are CarA, CarB, and CarC (in addition to Lamborghini), then are you going to write this:

    if(Lamborghini* lambo = dynamic_cast<Lamborghini*>(cars[i])) {
        lambo->retractTheRoof();
    }
    else if(CarA * pCarA = dynamic_cast<CarA*>(cars[i])) {
        pCarA->retractTheRoof();
    }
    else if(CarB * pCarB = dynamic_cast<CarB*>(cars[i])) {
        pCarB->retractTheRoof();
    }
    else if(CarC * pCarC = dynamic_cast<CarC*>(cars[i])) {
        pCarC->retractTheRoof();
    }
    

    So a better design in such cases would be this: add an interface called IRetractable and derive from it as well:

    struct IRetractable 
    {
       virtual void retractTheRoof() = 0;
    };
    
    class Lamborghini : public Car, public IRetractable {
       //...
    };
    
    class CarA : public Car, public IRetractable {
       //...
    };
    class CarB : public Car, public IRetractable { 
       //...
    };
    class CarC : public Car, public IRetractable {
       //...
    }; 
    

    Then you can simply write this:

    if(IRetractable *retractable =  dynamic_cast<IRetractable *>(cars[i])) 
    {
        retractable->retractTheRoof(); //Call polymorphically!
    }
    

    Cool? Isn’t it?

    Online demo : http://www.ideone.com/1vVId

    Of course, this still uses dynamic_cast, but the important point here is that you’re playing with interfaces only, no need to mention concrete class anywhere. In other words, the design still makes use of runtime-polymorphism as much as possible. This is one of the principle of Design Patterns:

    “Program to an ‘interface’, not an ‘implementation’.” (Gang of Four 1995:18)

    Also, see this:

    • What does it mean to "program to an interface"?

    Other important point is that you must make the destructor of Car (base class) virtual:

    class Car{
    public:
        virtual ~Car() {} //important : virtual destructor
        virtual int goFast() = 0;
    };
    

    Its imporant because you’re maintaining a vector of Car*, that means, later on you would like to delete the instances through the base class pointer, for which you need to make ~Car() a virtual destructor, otherwise delete car[i] would invoke undefined behaviour.

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

Sidebar

Related Questions

I was browsing StackOverflow when I encountered this question. Here the author mentions his/her
I encountered this small piece of code in this question , & wanted to
Here's a software design question I've encountered several times and have never found an
I recently encountered this problem. I found many instances of people asking the question—
I recently encountered with this question: How to reduce this expression: s>73?61:60; . The
Encountered a frustrating problem in our application today which came down to an ArrayIndexOutOfBounds
I'm actually embarrassed to ask this question but here goes. In a workflow activity,
I'm studying for upcoming interviews and have encountered this question several times (written verbatim)
This is my first question here on stack overflow. i need help on a
I encountered this question in an interview and had no clue how to answer:

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.