How to Override a Base Class Method in a Derived Class in C++? Last Updated : 21 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 class in C++. Override Inherited Methods in C++In C++, you can override a base class function in a derived class by declaring a function with the same signature in the derived class. Also, ensure that the base class function is declared as virtual function to enable dynamic binding. C++ Program to Override a Base Class Function in a Derived Class C++ // C++ Program to Override a Base Class Function in a // Derived Class #include <iostream> using namespace std; // Base class Person class Person { public: // Virtual function to greet virtual void greet() { cout << "Person: Hello, there! \n"; } }; // Derived class AngryPerson inheriting from Person class AngryPerson : public Person { public: // Override the greet function to provide a different // greeting void greet() override { cout << "Angry Person: Hello, looser! \n"; } }; // Driver Code int main() { // Create objects of both classes Person p; AngryPerson ap; // Call the greet function for both objects p.greet(); ap.greet(); return 0; } OutputPerson: Hello, there! Angry Person: Hello, looser! Note: It is recommended to use the override keyword to make sure that the user have not forgotten to declare the base class version of the function as virtual. Comment More infoAdvertise with us Next Article How to Override a Base Class Method in a Derived Class in C++? mycodenotein Follow Improve Article Tags : C++ Programs C++ CPP-OOPs CPP Examples Practice Tags : CPP Similar Reads How to Override a Base Class Method in C++? 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 Met 2 min read How to Create a Derived Class from a Base Class in C++? In C++, one of the fundamental concepts of Object Oriented Programming (OOPS) is inheritance, allowing users to create new classes based on existing classes. The class that inherits another class is called derived class and the class that is inherited is called base class. In this article, we will l 2 min read Base Class Pointer Pointing to Derived Class Object in C++ Prerequisite: Pointers in C++ A pointer is a data type that stores the address of other data types. Pointers can be used for base objects as well as objects of derived classes. A pointer to the object of the derived class and a pointer to the object of the base class are type-compatible (may be used 3 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 How to Call a Virtual Function From a Derived Class in C++? In C++, virtual functions play an important role because they allow the users to perform run-time polymorphism. While dealing with inheritance and virtual functions, it is very crucial to understand how to call a virtual function from a derived class. In this article, we will learn how to call a vir 2 min read Like