In C++, function overriding is a concept of OOPs (Object-Oriented Programming) that allows a derived class to redefine a function that is already provided by its base class. It is used to achieve runtime polymorphism.
Function Overriding is particularly useful when we need to modify or extend the behavior of an inherited function without changing the base class itself. It enables us to provide a specific implementation of the function, which is already provided by its base class.
Syntax
It has the following syntax:
class Base_class {
public: //Access Modifier
virtual void show() // Overriding Function
{
cout << "It is the Base Class Function." << endl;
}
};
class Derived_class : public Base_class {
public: //Access Modifier
void show() override // Overriding Function
{
cout << "It is the Derived Class Function." << endl;
}
};
In this syntax, the function name, argument, and return type functions are the same.
C++ Function Overriding Example
Let's see a simple example of Function overriding in C++. In this example, we are overriding the eat function.
Example
#include <iostream>
using namespace std; //using standard namespace
class Animal {
public: //Access Modifier
void eat(){
cout<<"Eating...";
}
};
class Dog: public Animal
{
public: //Access Modifier
void eat()
{
cout<<"Eating bread...";
}
};
int main(void) //Main Function
{
Dog d = Dog();
d.eat();
return 0;
}
Output:
Eating bread...
Rules for Function Overriding in C++:
Several rules for the function overriding in C++ are as follows:
- Same Function Name: The function in the derived class should be the same name as in the base class.
- Same Parameter: The function signature (parameters and return type) should be the same in both (base and derived class) classes.
- Inheritance Requirement: The derived class must inherit from the base class.
- Virtual Keyword: If the function in the base class is declared with virtual, it ensures proper function overriding and allows dynamic dispatch.
- Access Modifier Restrictions: Access specifiers can be changed, but it is not recommended.
How does Function Overriding Work in C++?
In C++, function overriding depends on the inheritance and the use of virtual keywords. When a base class is specified as virtual, it instructs the compiler to allow dynamic dispatch, which means the function call is resolved at runtime based on the object type, not the type of pointer.
If a derived class object overrides the base class virtual function and the function is called via a base class pointer or reference at the runtime, the derived class's version of the function is executed. This technique is implemented using a virtual table (vtable) that is maintained internally by the compiler.
C++ Function Overriding with Access Specifiers Example:
Let's take another example to illustrate the function overriding with access specifiers in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
class Vehicle {
public: //Access Modifier
virtual void startEngine() // Virtual function to allow overriding
{
cout << "Starting a generic vehicle..." << endl;
}
};
class Car : public Vehicle {
public: //Access Modifier
void startEngine() override // Overriding the function
{
cout << "Starting a car with a push button..." << endl;
}
};
int main() //Main Function
{
Vehicle* v1; // Base class pointer
Car c1;
v1 = &c1; // Assign address of derived class object to base class pointer
v1->startEngine(); // Calls the overridden function
return 0;
}
Output:
Starting a car with a push button...
Explanation:
In this example., we have taken a vehicle class (base class) that contains a startEngine function, which shows "the starting a generic vehicle". After that, the car class (derived class) inherits from the vehicle and overrides the startEngine function.
After that, we create a pointer of type Vehicle* and assign it to a car object. As the startEngine function is virtual, the overridden function in the Car class is called dynamically at runtime instead of the base class function.
Accessing Overridden Function in C++
There are several methods to access an overridden function in the derived class, such as directly from the object and from the base class via pointers and references . Some of the methods are as follows:
1. Calling Overridden Function from Derived Class Directly
We can directly call the overridden function from the derived class by using the derived class object. It calls the function that was redefined in the derived class.
Overridden Function from Derived Class Directly Example:
Example
#include <iostream>
using namespace std; //using standard namespace
class Base_class {
public: //Access Modifier
virtual void display() {
cout << "It is the base class display function" << endl;
}
};
class Derived_class : public Base_class {
public:
void display() override {
cout << "It is the derived class display function" << endl;
}
void callBaseFunc() {
Base_class::display(); //It calls base class function directly
}
};
int main() //Main Function
{
Derived_class object;
object.display(); //It calls overridden function in Derived
object.callBaseFunc(); //It calls the base class function
return 0;
}
Output:
It is the derived class display function
It is the base class display function
Explanation:
In this example, the object.display function calls the overridden function in the derived class, while the obj.callBaseFunction function calls the base class's version of the display function directly within the derived class.
2. Calling Overridden Function via Base Class Pointer or Reference
When we call a function using a base class pointer or reference, and the function is overridden in the derived class, the derived class version of the function is called in the program.
Overridden Function via Base Class Pointer and Reference Example:
Example
#include <iostream>
using namespace std; //using standard namespace
class Car {
public: //Access Modifier
virtual void showBrand() {
cout << "Generic Car Brand" << endl;
}
};
class Mahindra : public Car {
public:
void showBrand() override {
cout << "Brand Name: Mahindra" << endl;
}
};
class Kia : public Car {
public:
void showBrand() override {
cout << "Brand Name: Kia" << endl;
}
};
int main() //Main Function
{
Mahindra mahindraCar;
Kia kiaCar;
// Base class pointer
Car* carPtr;
carPtr = &mahindraCar;
carPtr->showBrand();
carPtr = &kiaCar;
carPtr->showBrand();
// Base class reference
Car& carRef = kiaCar;
carRef.showBrand();
return 0;
}
Output:
Brand Name: Mahindra
Brand Name: Kia
Brand Name: Kia
Explanation:
In this example, the base class Car defines a virtual function as showBrand. Mahindra and Kia brands override the function to provide brand-specific output. The use of base class pointer and reference allows us to call the overridden function at runtime based on the actual object, not the pointer and reference type.
3. Calling Overridden Function using Scope Resolution Operators
We can use the scope resolution operators (::) to access the overridden function in C++.
Overridden Function using Scope Resolution Operators Example:
Example
#include <iostream>
using namespace std; //using standard namespace
class base_Class {
public: //Access Modifiers
void print() {
cout << "This is the Base Function." << endl;
}
};
class derived_Class : public base_Class {
public:
void print() {
cout << "This is the Derived Function." << endl;
}
};
int main() //Main Function
{
derived_Class a1, a2;
a1.print();
// Using the scope resolution operator ::
a2.base_Class::print();
return 0;
}
Output:
This is the Derived Function.
This is the Base Function.
Advantages and Disadvantages of Function Overriding:
Several advantages and disadvantages of Function Overloading in C++ are as follows:
Advantages:
- Function overriding provides the option for runtime polymorphism that lets flexible and dynamic code.
- It helps to save the memory space.
- It makes code re-usability easier.
- In function overriding, the child class can access the function of the parent class.
- We can specialize the inherited behaviors of the base class functions to meet a specific class.
- Static functions can never be overridden.
- It cannot be performed inside the same class. Therefore, we are required to implement inheritance.
Disadvantages:
C++ Function Overriding MCQs
- Which of the following statements is true about function overriding in C++?
- Inheritance is needed only for multiple functions.
- Overriding can be applied without using inheritance.
- Inheritance should not be required when overriding is used in the program.
- Inheritance must be used to use function overriding.
- How can we access the overridden method of the base class from the derived class in C++?
- Using dot operator
- Using scope resolution operator
- Using arrow operator
- Cannot be accessed once overridden
- Which keyword is used to achieve runtime polymorphism in C++?
- virtual
- override
- static
- friend
- What does the following code output?
class Base_class {
public:
virtual void show() { cout << "This is the Base Function"; }
};
class Derived_class : public Base_class {
public:
void show() override { cout << "This is the Derived Function"; }
};
int main() {
Base_class* bptr = new Derived_class();
bptr->show();
return 0;
}
- This is the Base Function
- This is the Derived Function
- Compilation Error
- Runtime Error
- Can a private member function in the base class be overridden in C++?
- A) Yes
- B) No
- C) Only if declared virtual
- D) Only if declared public