- Asked 2/10
- 2074 Asoj · 5 marks
- 2072 Kartik · 4 marks
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.
| Point | Overloading | Overriding |
|---|---|---|
| Scope | Same class (same scope) | Base and derived class |
| Signature | Must differ in parameters | Must be identical |
| Inheritance | Not required | Required |
| Binding | Compile time (early) | Run time if virtual, else compile time |
virtual keyword | Not used | Used for run-time polymorphism |
| Purpose | Same operation on different data types | Change 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)
Related questions
- Function overriding and how to resolve it · Object Oriented Programming, chapter 6
- Default arguments and function overloading · Object Oriented Programming, chapter 3 · asked 5×
- Function overloading; new/delete average program · Object Oriented Programming, chapter 3
- Need of template; function template overloading · Object Oriented Programming, chapter 9
- Member function overriding with virtual function · Object Oriented Programming, chapter 7
- Overloading via member and non-member function · Object Oriented Programming, chapter 5
- Switch versus user defined function · Computer Programming, chapter 5