What is overriding in C++?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

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
Design Gurus Team

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
Is working at Meta stressful?
What Node JS interview questions are best to prepare for 10 years experience?
How much does a Microsoft internship pay?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Image
Grokking Data Structures & Algorithms for Coding Interviews
Image
Grokking Advanced Coding Patterns for Interviews
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.