when accessing foo() of "base" using derived class's object.
#include <iostream>
class base
{
public:
void foo()
{
std::cout<<"\nHello from foo\n";
}
};
class derived : public base
{
public:
void foo(int k)
{
std::cout<<"\nHello from foo with value = "<<k<<"\n";
}
};
int main()
{
derived d;
d.foo();//error:no matching for derived::foo()
d.foo(10);
}
how to access base class method having a method of same name in derived class. the error generated has been shown. i apologize if i am not clear but i feel i have made myself clear as water. thanks in advance.
You could add
using base::foo
to your derived class:Edit: The answer for this question explains why your
base::foo()
isn't directly usable fromderived
without theusing
declaration.Add following statement to your derived class :
This should serve the purpose.
When you have a function in derived class which has the same name as one of the functions in base class, all the functions of base class are hidden and you have to explicitly bring them in scope of your derived class as mentioned.
we can call base class method using derived class object. as per below code.
Output will be base class.