function overriding classes in oop cplusplus
What is member functions overriding?
Function overriding means to have the two or more functions with same name and with same signatures. Same signature means that to have the same name, same number of parameters and same data types.
A deriver (child) class inherits the data members and member functions of base (parent) class. Function overriding is just like creating a new version of an old function, in the child class.
Give example of member functions overriding?
Suppose Class A and class B have two functions with same name and same parameters . Then we can say that the function in class B overrides the function in class A.
Example program:
Serial# | Code |
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 |
#include<iostream>
using namespace std; class A { public: void print() { cout<<“print function for class A”<<endl; } }; class B : public A { public: void print() { cout<<“print function for class B”<<endl; } }; int main() { B object; object.print(); object.A :: print(); }
|
Here we have two functions of print() with same name and with same parameters…This is actually the function overriding.