CodeBlanks Logo Icon CodeBlanks

Summary - Polymorphism

Combine virtual, abstract and dynamic_cast to write an employee hierarchy.

Summary - Polymorphism

In this group you learned polymorphism. virtual methods provide dynamic dispatch; a base pointer calls the derived implementation. The virtual table (vtable) makes this possible.

Abstract classes define interfaces with pure virtual functions. dynamic_cast performs safe downcasting. A virtual destructor ensures the derived class is cleaned up on polymorphic delete. Together these tools build flexible and safe object-oriented design.

Abstract class and virtual destructor together

Let us combine two tools: an abstract class and a virtual destructor. You can define an interface and safely delete the derived class.

For example, an Animal abstract class contains virtual void speak() = 0; and virtual ~Animal() {}. A Cat class implements speak(). Create with Animal* a = new Cat();, call a->speak(), and safely clean up with delete a. This is a full example of polymorphic design.

Practice Animal abstract class and Cat implementation

Define an Animal abstract class with virtual void speak() = 0; and virtual ~Animal() {}. A Cat class inherits with void speak() override body std::cout << "Meow";. In main write Animal* a = new Cat();, a->speak(); and delete a;.

Starter code

main.cpp
#include <iostream>

// Define Animal and Cat classes here

int main() {
    // Create a Cat via Animal*, call speak and delete
    return 0;
}

Sample solution

main.cpp
#include <iostream>

class Animal {
public:
    virtual void speak() = 0;
    virtual ~Animal() {}
};

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

int main() {
    Animal* a = new Cat();
    a->speak();
    delete a;
    return 0;
}

Expected output

Meow

Practice dynamic_cast and virtual destructor together

Define a Base class with virtual void info() body std::cout << "Base"; and virtual ~Base() {}. A Derived class inherits with void info() override body std::cout << "Derived";. In main write Base* b = new Derived();, Derived* d = dynamic_cast<Derived*>(b);, if (d) d->info(); and delete b;.

Starter code

main.cpp
#include <iostream>

// Define Base and Derived classes here

int main() {
    // Create a Derived via Base*, downcast and call info
    return 0;
}

Sample solution

main.cpp
#include <iostream>

class Base {
public:
    virtual void info() { std::cout << "Base"; }
    virtual ~Base() {}
};

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

int main() {
    Base* b = new Derived();
    Derived* d = dynamic_cast<Derived*>(b);
    if (d) d->info();
    delete b;
    return 0;
}

Expected output

Derived

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does a polymorphic class contain?

main.cpp
class A { ??? };
  • Only static methods
  • Only const members
  • At least one virtual method Correct answer
  • Only inline variables

Explanation: A polymorphic class has at least one virtual method.

2 Match each concept to its purpose

Term Meaning
virtual Dynamic dispatch
= 0 Pure virtual
dynamic_cast Safe downcast
virtual ~Base Virtual destructor

All lessons in Intermediate