Overloading and Overriding
Expert Answer & Key Takeaways
Understanding Polymorphism in C++: The crucial differences between Function/Operator Overloading (Compile-time) and Function Overriding (Run-time).
Polymorphism: Overloading vs. Overriding
Polymorphism (meaning 'many forms') is one of the most powerful features of C++. It allows functions or operators to behave differently based on the context. Polymorphism is broadly classified into two categories: Compile-time (Early Binding) and Run-time (Late Binding).
1. Compile-Time Polymorphism (Static Binding)
In compile-time polymorphism, the compiler determines exactly which function to call during the compilation phase itself. It is extremely fast. This is achieved through Overloading.
1.1 Function Overloading
Function overloading occurs when two or more functions in the same scope have the exact same name, but their parameter lists (signatures) are different.
- The compiler decides which function to call based on the number or data types of the arguments passed.
- Note: Functions CANNOT be overloaded based purely on their return type. The parameters must differ.
class Math {
public:
// 1. Adds two integers
int add(int a, int b) {
return a + b;
}
// 2. Adds three integers (Different number of parameters)
int add(int a, int b, int c) {
return a + b + c;
}
// 3. Adds two doubles (Different type of parameters)
double add(double a, double b) {
return a + b;
}
};1.2 Operator Overloading
C++ allows you to redefine the way standard operators (like
+, -, *) work when applied to user-defined data types (like objects).- For example, you can overload the
+operator to mathematically add twoMatrixobjects or concatenate twoStringobjects. - Syntax: Uses the
operatorkeyword.
2. Run-Time Polymorphism (Dynamic Binding)
In run-time polymorphism, the compiler does not know which function to call at compile-time. The decision is made dynamically while the program is running. This is achieved through Inheritance and Function Overriding.
2.1 Function Overriding
Function overriding occurs when a Derived Class (Child) provides a specific, modified implementation for a function that is already defined in its Base Class (Parent).
- The function in the child class must have the exact same name, return type, and parameters as the one in the parent class.
- It requires an Inheritance relationship.
class Animal {
public:
void speak() {
cout << "Animal makes a sound";
}
};
class Dog : public Animal {
public:
// Overriding the parent's function
void speak() {
cout << "Dog barks: Woof!";
}
};
// Usage: Dog d; d.speak(); // Output: Dog barks: Woof!2.2 Virtual Functions
To achieve true run-time polymorphism using pointers, C++ uses Virtual Functions. When a base class pointer points to a derived class object, the compiler normally calls the base class's function. By marking the base class function with the virtual keyword, we force the compiler to perform late binding and call the derived class's overridden version instead.
3. Key Differences Summary
| Feature | Overloading | Overriding |
|---|---|---|
| Definition | Same name, DIFFERENT parameters. | Same name, SAME parameters. |
| Scope | Occurs within the SAME class/scope. | Occurs across Parent and Child classes. |
| Polymorphism Type | Compile-Time (Static) | Run-Time (Dynamic) |
| Inheritance | Not required. | Absolutely required. |
| Keyword | None. | Uses virtual for dynamic binding. |
Course4All Editorial Board
Verified ExpertSubject Matter Experts
Comprising experienced educators and curriculum specialists dedicated to providing accurate, exam-aligned preparation material.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.