Introduction to C++ | Function Overriding
C++ Function Overriding
Function Overriding in C++ is a feature that allows a derived class to provide a specific implementation of a function that is already defined in its base class. It is a key concept in object-oriented programming and supports polymorphism.
What is Function Overriding?
Function overriding occurs when:
- A derived class has a function with the same name, return type, and parameters as a function in its base class.
- The base class function is marked as
virtual
, allowing the derived class to override it.
Syntax of Function Overriding
The syntax for function overriding in C++ is:
Syntax
class Base {
public:
virtual returnType functionName(parameters) {
// Base class implementation
}
};
class Derived : public Base {
public:
returnType functionName(parameters) override {
// Derived class implementation
}
};
Key Features of Function Overriding
- Function overriding is used in the context of inheritance.
- The function in the base class must be declared as
virtual
. - The overridden function in the derived class must have the same signature as the base class function.
- Overriding enables runtime polymorphism (method is resolved at runtime).
Example of Function Overriding
Here’s an example demonstrating function overriding:
Code Example
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
virtual void sound() {
cout << "Animal makes a sound" << endl;
}
};
// Derived class
class Dog : public Animal {
public:
void sound() override {
cout << "Dog barks" << endl;
}
};
// Derived class
class Cat : public Animal {
public:
void sound() override {
cout << "Cat meows" << endl;
}
};
int main() {
Animal* animal;
Dog dog;
Cat cat;
// Base class pointer pointing to derived class objects
animal = &dog;
animal->sound(); // Calls Dog's sound()
animal = &cat;
animal->sound(); // Calls Cat's sound()
return 0;
}
Output
Cat meows
Why Use Function Overriding?
- To provide specific implementations of a base class function in derived classes.
- To enable runtime polymorphism in C++.
- To extend the functionality of a base class in a derived class.
Rules for Function Overriding
- The function in the base class must be declared as
virtual
. - The signature of the function in the derived class must exactly match the base class function.
- Function overriding occurs in the context of inheritance only.
- To prevent overriding, you can use the
final
specifier in the base class function.
Function Overriding with Access Specifiers
The access specifier (e.g., public
, protected
, or private
) of the overriding function can differ from the base class function. However, the visibility of the function is determined by the access specifier in the derived class.
Common Mistakes with Function Overriding
- Forgetting to use the
virtual
keyword in the base class function. - Using a different function signature in the derived class, which causes the function to be treated as a new function instead of overriding.
- Assuming that function overriding works without inheritance (it does not).
Pro Tip:
💡 Pro Tip
Always use the override
keyword in the derived class to ensure that you are overriding a base class function and not accidentally defining a new function. This helps catch errors during compilation.