1
votes

I know what is the use of Run Time Polymorphism in an Object Oriented programming language. For example Base class Superhero would be useful to define basic abstract properties of a Superhero that can be inherited and extended in different Super Heroes for example Batman.

I also know that we use inheritance when there is an is-a relationship, for example, Superman is-a Superhero, and we can use a Base class pointer to point to a Derived class object for example...

Superhero *h = new Spiderman();

is valid.

But I want to know what are the circumstances where we would need to do something like this, why can't we just use...

Spiderman *s_man = new Spiderman()

What situation or circumstances would force us to use Base class pointer to hold Child class object.

I would be thankful if someone can make this clear to me.

3
Write a single line of code to delete any SuperHero *. That should be enough to explain why Superhero *h would be used. - PaulMcKenzie
Insert opening example from any OO book here. - Barry
I know what is the use of Run Time Polymorphism in an Object Oriented programming language. I suspect that you don't. Run time (object oriented) polymorphism can only be achieved by pointing to a derived object with a base pointer (or a reference). When you know what run time polymorphism is used for, the answer will become obvious. - eerorika
For what it is worth I think people use base class pointers more often then they need to. I think the first choice should be to not use a pointer at all: Spiderman s_man. - Chris Drew

3 Answers

5
votes

Having a base class pointing to a derived class allows you to perform operations on groups of base class objects as if they were all the same. In this contrived but marvel cinematic universe conforming example, you could collect all of the avengers in a single list and perform some sort of operation.

std::vector<Superhero*> GetAvengers()
{
    std::vector<Superhero*> avengers;
    avengers.push_back(new Spiderman());
    avengers.push_back(new CaptainAmerica());
    avengers.push_back(new IronMan());
    avengers.push_back(new Thor());
    // ... ad nauseam for three generations of unnecessary standalone films
    return avengers;
}

void FightThanos()
{
    SuperVillan* thanos = new Thanos();
    auto Avengers = GetAvengers();

    for(auto&& avenger : Avengers)
    {
        avenger->SaySomethingSnarky();
        avenger->ParticipateInMontageActionSequence(thanos);
    }

    // Clean up after the avengers, they're not needed for another 4 years
    for(auto&& avenger : Avengers)
    {
       delete avenger;
    }
    avengers.clear();
 } 
0
votes

If you want to have standard calls for all types of super heroes. But the implementation is different. No syntax checking here :)

class Superhero
{
public:
virtual void changeClothes() = 0;
}

class Spiderman : public Superhero
{

public:
void changeClothes()
{
  body.wear(spideySuit);
}

};


class Hulk public Superhero
{

public:
void changeClothes()
{
  body.shorts.TearApart();
}
};

SuperHero * spidy = (Superhero *)  new Spiderman;
SuperHero * hulkster = (Superhero *)  new Hulk;

spidey->changeClothes();
hulkster->changeClothes();
0
votes

Consider a list of superhero pointers. Such a list could store references (pointers) to new Spiderman(), new Superman(), and other objects that inherit superhero.

@lcs has provided an excellent illustration of this concept.