Function Overriding in C++ Programming Language is one of the important concept in C++.
Function Overloading is used to achieve run time polymorphism
What is Function Overriding in C++?
When two classes are in inheritance hierarchy.
Then parent class function is re-defined in child class with same method signature.
The re defined function in child class is known as overridden function and this process is called function overriding.
Simple Example of Function Overriding in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> using namespace std; class A { // base class public: void show() { cout << "Show of base class "; } }; class B: public A { //derived class public: void show() { //overridden function cout << "Show of derived class "; } }; int main() { B b; b.show(); // derived class show() } |
Output
1 | Show of derived class |
In the above program, class A & B both have a function (show()
) with same name and same parameter list.
When we call a show()
function of with the child class variable then child class show() is get called.
Why do we need function overriding?
Base or parent class is general in nature and child classes are specialize class.
Parent class function represents general behavior, to provide specific behavior according to child class we need to override function.
Function Overriding Example in C++
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 | #include <iostream> using namespace std; class Shape { public: void draw() { cout << "Draw Shape "; } }; class Triangle: public Shape { public: void draw() { cout << "Draw Triangle "<<endl; } }; class Circle: public Shape { public: void draw() { cout << "Draw Circle "<<endl; } }; int main() { Triangle t; t.draw(); Circle c; c.draw(); } |
We have created a base class Shape. This class has one function draw().
This method prints Draw Shape.
But can you tell which shape it will draw?
Here Shape is not defined but we need draw() in all sub classes because subclasses are specific and it can draw specific Shape.
Class Triangle can draw Triange Shape
Class circle can Draw Circle Shape.
To implement child’s specific behavior by all subclasses we need to override a function.
Overriding provides new definition to an existing method.
How to Access Base Class overriden method using child object in C++
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 | #include <iostream> using namespace std; class Shape { public: void draw() { cout << "Draw Shape "<<endl; } }; class Triangle: public Shape { public: void draw() { cout << "Draw Triangle "<<endl; } }; class Circle: public Shape { public: void draw() { cout << "Draw Circle "<<endl; } }; int main() { Triangle t; t.draw(); t.Shape::draw(); Circle c; c.draw(); } |
Here to call base class overridden method from Child class object Scope Resolution operator is used (::)
t.Shape::draw()
How to Access Base Class overriden method from child class in C++
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 | #include <iostream> using namespace std; class Shape { public: void draw() { cout << "Draw Shape "<<endl; } }; class Triangle: public Shape { public: void draw() { cout << "Draw Triangle "<<endl; Shape::draw(); } }; class Circle: public Shape { public: void draw() { cout << "Draw Circle "<<endl; } }; int main() { Triangle t; t.draw(); Circle c; c.draw(); } |
Inside circles’s draw() we called Shape’s draw() function using scope resolution operator
Shape::draw()
Read More