What is method hiding in C++?

A method of parent class, re-written in child class with same signature and different body contents is called as method hiding.

#include<iostream>
using namespace std;
class A
{
public:
void aboutme()
{
cout <<"This is A" << endl;
}
};
class B: public A
{
public:
void aboutme()
{
cout <<"This is B" << endl;
}
void welcome()
{
cout <<"Welcome to B" << endl;
}
};
class C: public B
{
public:
void aboutme()
{
cout <<"This is C" << endl;
}
};

main()
{
A *p;
p=new A();
p->aboutme();
p=new B();
p->aboutme();
p=new C();
p->aboutme();
}

Output

This is A
This is A
This is A

Comments

Popular posts from this blog

What is template?

Can we access private members of an instance?

What are wrapper classes in Java?