Overriding Functions in C++
Function overriding in the C++ programming language is an important feature or advantage of polymorphism in the object-oriented programming (OOPS) concept. Function overriding allows a derived subclass created in a user-defined class to provide a specific implementation for a function that is already defined or declared in the base class. When a function is automatically overridden in a C++ program, the user-defined function of the derived class in the existing class replaces or modifies the function of the base or root class when called on an object parameter element of the existing derived class.

Function overriding in the C++ programming language develops when
a base or root class function is declared or defined virtually in a user-defined class.
In which a user-defined derived subclass provides its own implementation of the same function. For example, the class function method name, return data type, and variable parameter argument list declared in the class are the same.
Special features of function overriding in C++ programming.
- Virtual function – In a C++ class program, a function in the base or parent class is declared or marked with the virtual keyword to indicate that it can be overridden in a derived subclass.
- Overriding – The function overriding concept allows a derived subclass to provide its own implementation of a function already declared in the base or root class.
- Polymorphism – The function overriding concept allows for polymorphic behavior within a class. This means that the class method executed depends on the data type of the class object, not solely on the data type of the class pointer or reference to it.
Syntax of function overriding in C++.
class Base {
public:
virtual void preview() { // The reserved virtual keyword here allows function overriding.
std::cout << “Base class preview function element” << std::endl;
}
};
class Derived : public Base {
public:
void preview() override { // This overrides the Base class method.
std::cout << “Derived class preview function” << std::endl;
}
};
Here’s a function overriding example.
- In this example, the Base parent class declares a virtual function preview().
- Here, the Derived subclass overrides the preview() function with its default implementation.
- The override keyword is an optional class method, but it can be used for advanced program source code readability and error checking.
Example of function overriding methods in C++.
#include <iostream>
class Root {
public:
virtual void preview() { // Here we use a virtual function with the preview name
std::cout << “Root/Base class preview function” << std::endl;
}
};
class Subclass : public Root {
public:
void preview() override { // Here it overrides the Root/Base class method
std::cout << “Subclass/Derived class preview override function” << std::endl;
}
};
int main() {
Root* rootPtr; // Here a pointer to the Root/Base class element is defined
Subclass derivedArgs;
rootPtr = &derivedArgs; // here it points to the Subclass class object
rootPtr->preview(); // here it calls the Subclass class version of the preview() functions
return 0;
}
Explanation of function overriding methods in C++.
- In this example, the Root base class declares a virtual function called preview(), allowing it to override the function in the derived class.
- Here, the derived subclass class provides its own implementation of the preview() function, which overrides the base class’s version. In the program’s main() function, we create a base pointer (rootPtr) and assign it the address of a derived object (&derivedArgs).
- When the rootPtr->preview() function is called in the current program, it calls the overridden version of preview() from the derived subclass class, not from the base root class. This happens because of the concept of polymorphism.

