0
0
C++programming~20 mins

Why polymorphism is needed in C++ - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Polymorphism Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use polymorphism in C++?

Imagine you have different types of animals, each can make a sound. Why is polymorphism useful in this case?

C++
class Animal {
public:
    virtual void makeSound() {
        std::cout << "Some generic sound" << std::endl;
    }
};

class Dog : public Animal {
public:
    void makeSound() override {
        std::cout << "Bark" << std::endl;
    }
};

class Cat : public Animal {
public:
    void makeSound() override {
        std::cout << "Meow" << std::endl;
    }
};

void playSound(Animal* animal) {
    animal->makeSound();
}

int main() {
    Dog dog;
    Cat cat;
    playSound(&dog);
    playSound(&cat);
    return 0;
}
AIt makes the program run faster by avoiding function calls.
BIt forces all animals to have the same sound, so code is simpler.
CIt prevents creating objects of different animal types.
DIt allows calling the correct makeSound() method for each animal type using a single interface.
Attempts:
2 left
πŸ’‘ Hint

Think about how you can use one function to handle many animal types and get different sounds.

❓ Predict Output
intermediate
1:30remaining
What is the output of this polymorphism example?

Look at the code below. What will it print?

C++
class Base {
public:
    virtual void show() { std::cout << "Base" << std::endl; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived" << std::endl; }
};

int main() {
    Base* b = new Derived();
    b->show();
    delete b;
    return 0;
}
ABase
BRuntime error
CDerived
DCompilation error
Attempts:
2 left
πŸ’‘ Hint

Remember virtual functions decide which method to call at runtime based on the actual object.

πŸ”§ Debug
advanced
2:30remaining
Why does this polymorphism code cause a problem?

Find the error in this code and what problem it causes.

C++
class Animal {
public:
    void makeSound() { std::cout << "Animal sound" << std::endl; }
};

class Dog : public Animal {
public:
    void makeSound() { std::cout << "Bark" << std::endl; }
};

int main() {
    Animal* a = new Dog();
    a->makeSound();
    delete a;
    return 0;
}
AThe output is "Animal sound" because makeSound() is not virtual, so Dog's method is not called.
BThe program crashes because of a missing virtual destructor.
CCompilation error due to missing override keyword.
DDog's makeSound() is called correctly, so output is "Bark".
Attempts:
2 left
πŸ’‘ Hint

Check if the base class method is virtual to enable polymorphism.

πŸ“ Syntax
advanced
1:30remaining
Which option correctly declares a polymorphic base class?

Choose the correct way to declare a base class with a virtual function for polymorphism.

Aclass Shape { public: void draw() { /*...*/ } };
Bclass Shape { public: virtual void draw() { /*...*/ } };
Cclass Shape { public: virtual void draw() = 0; };
Dclass Shape { public: void virtual draw() { /*...*/ } };
Attempts:
2 left
πŸ’‘ Hint

Remember the correct syntax for virtual functions in C++.

πŸš€ Application
expert
3:00remaining
How many objects are created and which method is called?

Analyze the code below. How many objects are created and what is printed?

C++
class Vehicle {
public:
    virtual void start() { std::cout << "Vehicle starts" << std::endl; }
    virtual ~Vehicle() {}
};

class Car : public Vehicle {
public:
    void start() override { std::cout << "Car starts" << std::endl; }
};

int main() {
    Vehicle* v1 = new Vehicle();
    Vehicle* v2 = new Car();
    v1->start();
    v2->start();
    delete v1;
    delete v2;
    return 0;
}
ATwo objects created; output: Vehicle starts\nCar starts
BTwo objects created; output: Car starts\nCar starts
COne object created; output: Vehicle starts\nVehicle starts
DCompilation error due to missing destructor
Attempts:
2 left
πŸ’‘ Hint

Count the new statements and check which start() method is called for each pointer.