PREV NEXT
What is function overriding?
- The function overriding means when the function has the same name, same return type and same parameters.
- The function overriding also means when the derived class defines the same function as defined in its base class.
- Through function overriding you can perform runtime polymorphism.
- The function overriding allows you to have the same function in child class which is already defined in the parent class.
- The child class inherits the data members and member functions of parent class but if you want to override a function in the child class then you can use function overriding.
Let us have a look at the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> using namespace std; class Car { public: void speed(){ cout<<"Speed..."; } }; class BMW: public Car { public: void speed() { cout<<"Speed is awesome..."; } }; int main(void) { BMW b = BMW(); b.speed(); return 0; } |
Difference between Function Overloading and Function Overriding
Function Overloading |
Function Overriding |
The function overloading can be done without inheritance. | The function overriding occurs when once class is inherited from another class. |
In function overloading it should have a different signature either in the form of number of arguments or type of arguments. | In function overriding the function signature should be same. |
The function overloading is done in the same scope. | The function overriding is done in different scopes. |
The function overloading have same name of functions which behave differently depending upon the parameters passed. | The function overriding is required when derived class function has to do some different tasks than the base class function. |