What is overriding in C++?

Overriding in C++ is a feature of Object-Oriented Programming (OOP) that allows a subclass (derived class) to provide a specific implementation of a method that is already defined in its base class. The method in the derived class overrides the method in the base class. This is typically done when the derived class needs to alter or extend the behavior of the base class method.

Key Points of Overriding:

  1. Same Method Signature: The overriding method in the derived class must have the same name, return type, and parameters as the method in the base class.
  2. Virtual Functions: To enable method overriding, the method in the base class must be declared with the virtual keyword. This tells the compiler that the method can be overridden in derived classes.
  3. Run-Time Polymorphism: Overriding enables polymorphism, where the method called is determined at runtime based on the object type, not the reference type.

Example of Method Overriding in C++:

#include <iostream> using namespace std; class Base { public: virtual void display() { // Virtual function cout << "Base class display" << endl; } }; class Derived : public Base { public: void display() override { // Overriding the base class method cout << "Derived class display" << endl; } }; int main() { Base* basePtr; Derived derivedObj; basePtr = &derivedObj; basePtr->display(); // Calls Derived class's display method return 0; }

Output:

Derived class display

Key Features of Overriding:

  • Virtual Functions: The base class method must be marked as virtual, signaling to the compiler that the method can be overridden in derived classes.
  • override Keyword: In C++11 and later, the override keyword is used in the derived class to explicitly indicate that the method is overriding a base class method. This helps catch mistakes, like mismatched signatures, during compilation.

Benefits of Overriding:

  • Polymorphism: Overriding allows the same method to behave differently based on the object that is calling it, enabling polymorphism.
  • Customization: Derived classes can provide their own specific behavior while still maintaining the same interface.

Sources:

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is an SDK vs API?
What is dbms in SQL?
Why do companies ghost candidates after interview?
What is an Amazon bar raiser?
What is the fastest way to become a software engineer?
Why work at PayPal?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.