class Animal
{
public:
    void speak() { cout << "Some sound\n"; }
};

class Dog : public Animal
{
public:
    void speak() { cout << "Bark!\n"; }   // overrides Animal::speak()
};

Dog d;
d.speak();   // "Bark!" -- Dog's version is used

Overriding vs Overloading — the distinction examiners test most

Overloading Overriding
Where Same class (or same scope) Base class vs derived class
Signature MUST differ (params/types) MUST be identical
Resolved Compile-time Runtime (if virtual) or compile-time (if not)
Purpose Same operation, different input types Same operation, specialized for a subclass
class Printer
{
public:
    void print(int x) { cout << "int: " << x; }      // OVERLOADING --
    void print(string s) { cout << "string: " << s; } // -- same class, different signatures
};

class Animal
{
public:
    void speak() { cout << "Some sound\n"; }
};
class Dog : public Animal
{
public:
    void speak() { cout << "Bark!\n"; }   // OVERRIDING -- same signature, different class
};

Overriding WITHOUT virtual — the trap

class Animal { public: void speak() { cout << "Some sound\n"; } };
class Dog : public Animal { public: void speak() { cout << "Bark!\n"; } };

Animal* a = new Dog();
a->speak();   // prints "Some sound" -- NOT "Bark!" -- because speak() isn't virtual, static binding applies

Without virtual, the compiler decides which speak() to call based on the pointer's declared type (Animal*), not the actual object type (Dog). This is called static binding, and it's the #1 reason overriding "doesn't work" for students — see the Virtual Function page for the fix.

Important MCQs

  1. Overriding requires the derived class method to have:
  2. Without the virtual keyword on the base method, calling it through a base-class pointer to a derived object uses:
  3. Overloading differs from overriding mainly because overloading:

Self-Test Questions

  1. What's the key structural difference between overloading and overriding?
  2. In the trap example above, why does a->speak() print "Some sound" instead of "Bark!"?
  3. What single keyword fixes the trap example, and where does it need to be added?

📌 Sample & Repeated FPSC Questions (2016–2026)

2026, Paper I, Q4(c) (8 marks) — Demonstrate method overriding using a base class Account and a derived class Current Account. Make balance a protected data member.