Compile Time Function Overriding
In compile time function overriding, the function call and the definition is binded at the compilation of the program. Due to this, it is also called early binding or static binding.
class Parent {
access_modifier :
// overridden function
return_type name_of_the_function() {}
};
class child : public Parent {
access_modifier :
// overriding function
return_typename_of_the_function() {}
};
Example of Compile Time Function Overriding
// C++ program to demonstrate compile time function overriding
#include <iostream>
using namespace std;
class Parent {
public:
void w3wiki_Print()
{
cout << "Base Function" << endl;
}
};
class Child : public Parent {
public:
void w3wiki_Print()
{
cout << "Derived Function" << endl;
}
};
int main()
{
Child Child_Derived;
Child_Derived.w3wiki_Print();
return 0;
}
Output
Derived Function
Function Overriding in C++
A function is a block of statements that together performs a specific task by taking some input and producing a particular output. Function overriding in C++ is termed as the redefinition of base class function in its derived class with the same signature i.e. return type and parameters. It can be of both type: Compile Time and Runtime Polymorphism.