How to Override a Base Class Method in C++? Last Updated : 13 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, method overriding allows us to redefine a function from a parent class (base class) in its child class (derived class) to modify its behavior for objects of the child class. In this article, we will learn how to override a base class method in the derived class in C++. Override Inherited Method in C++In C++, to override a base class method in a derived class, we first have to declare that function as a virtual function in the base class. After that, we can redefine this virtual function in the derived class. Declaring the function as virtual tells the compiler to use the most derived version of the function available for that object. We can also use the override keyword in the inherited function redefinition to prevent errors. ApproachDefine a base class with the method we want to override using a virtual keyword. Create a derived class that inherits from the base class. In the derived class, redefine the method to be overridden with the same signature (name and parameters) and use the override keyword to explicitly indicate that we intend to override the method.Now, use objects of the derived class to call the overridden method and when the method is called, the implementation in the derived class will be executed instead of the base class implementation. C++ Program to Override a Base Class MethodThe below example demonstrates how we can override a base class method in a derived class in C++. C++ // C++ Program to illustrate how to override the function of // base class in derived class #include <iostream> using namespace std; // Base class class Animal { public: virtual void makeSound() { cout << "The animal makes a sound" << endl; } }; // Derived class class Dog : public Animal { public: // Overriding makeSound() function of the base class void makeSound() override { cout << "The dog barks" << endl; } }; int main() { Dog dog; dog.makeSound(); // Outputs: "The dog barks" return 0; } OutputThe dog barks Time Complexity: O(1)Auxilliary Space: O(1) Note: To enable overriding, the base class method should be declared as virtual in the base class. This enables dynamic dispatch, ensuring that the appropriate derived class method is called based on the actual type of the object at runtime. Comment More infoAdvertise with us Next Article How to Override a Base Class Method in C++? susobhanakhuli19 Follow Improve Article Tags : C++ Programs C++ C++-Virtual Functions CPP-OOPs CPP Examples +1 More Practice Tags : CPP Similar Reads How to Override a Base Class Method in a Derived Class in C++? Function overriding is a concept in object-oriented programming languages, in which a function/method of a parent class is redefined in its child class to change its behavior for the objects of the child class. In this article, we are going to learn how to override a base class function in a derived 2 min read How to access private/protected method outside a class in C++ Prerequisites: Access Modifiers in C++, Runtime Polymorphism Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functi 5 min read How to Create a Template Class in C++? In C++, template classes are used to create generic classes that can work for different data types. In this article, we will learn how to create a template class in C++. Create a Template Class in C++ To create a template class in C++, we can follow the below syntax: Syntax of Template Classtemplate 2 min read How to Implement Interfaces Using Abstract Class in C++ C++ doesn't have built-in functionality of interfaces like other programming languages such as Java. However, interfaces can be implemented in C++ using the abstract classes and pure virtual functions. In this article, we will learn how to implement interfaces using abstract classes in C++.Implement 4 min read How to convert a class to another class type in C++? Pre-requisite: Type Conversion in C++, Advanced C++ | Conversion OperatorsThrough class conversion, one can assign data that belongs to a particular class type to an object that belongs to another class type. Example: Let there be two classes 'A' and 'B'. If we want to allocate the details that belo 5 min read Like