Run-Time Polymorphism (Introduction)

Medium Polymorphism & Function Overloading
Solve in Playground →

Problem Statement

Create a base `Animal` class with a `virtual void speak()` that prints ‘Animal’. Create derived classes `Dog` and `Cat` that override `speak()` to print ‘Woof’ and ‘Meow’. In `main()`, declare an `Animal*` pointer. Point it to a `Dog` object and call `speak()`. Then point it to a `Cat` object and call `speak()`.

Input Format

None

Output Format

Print the sounds on separate lines.

Constraints

None

Sample Output

Woof
Meow

Explanation

The virtual keyword tells the compiler to defer method resolution until runtime (Late Binding), ensuring the pointer triggers the derived object's actual method.

Starter Code

#include <iostream>

class Animal {
public:
    // Add virtual speak method
};

class Dog : public Animal {
public:
    // Override speak
};

class Cat : public Animal {
public:
    // Override speak
};

int main() {
    // Create pointer, execute dog behavior, then cat behavior
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used