Function Overloading

What Is Function Overriding? Explained With an Example

Function overriding lets a derived class replace a virtual function from its base class. Learn how it differs from overloading, with a short C++ example.

On this page
  1. Overriding in one sentence
  2. A working example
  3. Why virtual matters
  4. Overriding vs. overloading
  5. Common mistakes
  6. Where overriding fits
  7. Key takeaways

Overriding in one sentence

Function overriding happens when a derived class provides its own version of a member function that the base class marks virtual. A call through a base class pointer or reference then runs the derived version.

A working example

#include <iostream>

class Animal {
public:
    virtual void speak() const {
        std::cout << "...\n";
    }
    virtual ~Animal() = default;
};

class Dog : public Animal {
public:
    void speak() const override {
        std::cout << "Woof\n";
    }
};

int main() {
    Dog dog;
    Animal* pet = &dog;
    pet->speak();  // prints Woof
}

Dog::speak has the same name, the same (empty) parameter list, and the same const qualifier as Animal::speak, so it overrides the base version. The override keyword asks the compiler to confirm that. The call goes through an Animal*, but the object is a Dog, so the Dog version runs.

The virtual destructor in Animal follows the same logic. It makes sure that deleting a Dog through an Animal* cleans up correctly.

Why virtual matters

Without virtual in Animal, pet->speak() calls Animal::speak, because the compiler uses the pointer’s type to pick the function. The speak in Dog would then be a separate function that hides the base version. It would not be an override.

Overriding vs. overloading

Overloading Overriding
Where it happens Functions in the same scope A base class and a derived class
Parameter list Must differ Must match
When the version is chosen At compile time At runtime, for virtual functions
Keywords None needed virtual in the base class, and override in the derived class

Overloading is the subject of what is function overloading, and the two are often mixed up because both reuse a name.

Common mistakes

  • Forgetting virtual in the base class. The derived function then hides the base one, and calls through base pointers use the base version.
  • Mismatched signatures. A derived void speak() without const doesn’t override a base void speak() const. With override, the compiler reports an error, which makes the mistake easy to catch.
  • Leaving out override. Without it, the same mismatch compiles quietly, and the bug can go unnoticed.

Where overriding fits

Overriding is a main way C++ achieves runtime polymorphism, which is one part of polymorphism in C++. It depends on inheritance in C++, where a derived class is built on a base class. Keep it separate from overloading, which picks a version at compile time based on the arguments.

Key takeaways

  • Overriding replaces a virtual function from a base class in a derived class.
  • The signature has to match, and override lets the compiler check it.
  • The version that runs is chosen at runtime, based on the object’s actual type.
  • Overloading is a different idea: it chooses among functions with the same name at compile time.

The complete guide to function overloading

Does C++ Have Function Overloading?
Read the guide

Keep exploring

Guides from other topics

All topics →

Shift Operators in C++

Learn how the << and >> shift operators work in C++, with binary examples, compound forms, precedence traps, undefined cases, and safe ways to use them.

C++ While Loop Syntax

Learn the C++ while loop syntax, how the condition is checked, and how to avoid infinite loops, with step-by-step examples you can compile and run.

While Loops 4 min read

How to Use goto in C++

Learn how to use goto in C++: write a label and jump to it, see forward and backward examples, avoid compile errors, and pick safer alternatives.

goto 5 min read