// Method overriding , method overloading and method hiding in C++
//1. When both child and parents have same function plus same arguments passing in
//their function then it is called the function overriding.
//2. And if both child and parents have same function but they have different
//argument passed in them then it is called the function hiding.
#include <iostream>
class A
{
public:
void f1(){
}
void f2()
{
}
};
class B : public A
{
public:
void f1(){ // Function overriding
}
void f2(int x) // Function hiding
{
}
};
int main()
{
B obj;
obj.f1();// When a object is created and called the function then FIRST compiler moves
// to the class whom object is created and called the function in our case it is Class B
// and if compiler do not get that function in the class then it move to its parent class.
//obj.f2();// It generated an error because the workong of compiler is that it first goes
//to the class whom object called the function and in this case compiler moves to class B
//where the function is present f2 but their is argument required in this case so it's an
// error and if f2 is not present in derived class then compiler moves to parent class
// function f2 so no error occur
// Function overloading is occur when same class contain all the possible version of Function
//like if class B contain f2() without argument and f2(int ) single argument etc.then
// it is called Function overloading.
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: