Function Overriding

Function overriding occurs when two methods have the same method name and parameters. One of the methods is in the parent class, and the other is in the child class.

Overriding allows a child class to provide the specific implementation of a method that is already present in its parent class.

Know the difference with Function Overloading.

#include <iostream>
using namespace std;
class BaseClass {
public:
   void disp(){
      cout<<"Function of Parent Class";
   }
};
class DerivedClass: public BaseClass{
public:
   void disp() {
      cout<<"Function of Child Class";
   }
};
int main() {
   DerivedClass obj = DerivedClass();
   obj.disp(); // Prints "Function of Child Class"
   return 0;
}

Danger

However if you do Polymorphism, you gotta be careful about behavior. Actually, it seems that this just happens because they didn’t use virtual function keyboard, and then declared the class as the parent.

Note

Function overriding doesn’t care about access specifies. You can override any access specifier. Access specifiers only affect who can call the function.