Skip to main content

Function overriding versus overloading

Short answer

Function overloading is having multiple functions with the same name but different parameter lists in the same class, resolved at compile time. Function overriding is redefining a base class function in a derived class with identical signature; with virtual, it enables runtime polymorphism.

How does function overriding differ from function overloading? Explain with example.

Function overloading means having several functions with the same name in the same scope but different parameter lists (number, type or order of parameters). Function overriding means a derived class redefines a base class function that has the same name, return type and parameters.

PointOverloadingOverriding
ScopeSame class (same scope)Base and derived class
SignatureMust differ in parametersMust be identical
InheritanceNot requiredRequired
BindingCompile time (early)Run time if virtual, else compile time
virtual keywordNot usedUsed for run-time polymorphism
PurposeSame operation on different data typesChange base behaviour in derived class

Example of overloading:

class Calc {
public:
    int add(int a, int b)       { return a + b; }
    double add(double a, double b) { return a + b; }   // different types
};

Example of overriding:

#include <iostream>
using namespace std;

class Animal {
public:
    virtual void sound() { cout << "Some sound" << endl; }
};
class Dog : public Animal {
public:
    void sound() { cout << "Bark" << endl; }   // overrides Animal::sound
};

int main() {
    Animal *p = new Dog;
    p->sound();          // Bark (decided at run time)
    delete p;
    return 0;
}

✓ Answer checked by Claude Opus 5.5 (high effort)

All Object Oriented Programming chapter 6 questions →