C++ : Base class member function not accessible from derived class

Hello All,
I am a learner in C++. I was testing my inheritance knowledge with following piece of code.

#include <iostream>

using namespace std;

class base
{
	public :
		void display()
		{
			cout << "In base display()" << endl;
		}
		void display(int k)
		{
			cout << "In base display(int k)" << endl;
		}
};

class derived : public base
{
	public :
		void display()
		{
			cout << "In derive display()" << endl;
		}
};

int main()
{
	derived der;	
	der.display(50);
}	
    As far as I know it should compile without giving any error. But that is not the case as it is giving compilation error saying :- 
StaticMember_1.cpp: In function �int main()':
StaticMember_1.cpp:30: error: no matching function for call to �derived::display(int)'
StaticMember_1.cpp:21: note: candidates are: void derived::display()
       Can you guys help me out in finding why its giving error. there must be some concept here which I am missing. 
      Thanks in advance all of you.

Regards,
Anand Shah

When you declare display() in the derived class, it hides the base class display() instead of overloading it. So when you call the display() using a derived class object, you will get an error since the compiler is unable to find the correct function.

Refer C++ FAQ for more info.

1 Like

Thank you Chacko193 for your efforts. I understood it completely. I always felt there is some catch in it which I was missing. Thanks again.