9

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.

jww
  • 97,681
  • 90
  • 411
  • 885
Ashish Yadav
  • 1,667
  • 6
  • 20
  • 26
  • Possible duplicate of [How to call a parent class function from derived class function?](https://stackoverflow.com/questions/357307/how-to-call-a-parent-class-function-from-derived-class-function) – jww Jun 02 '18 at 20:00

4 Answers4

12

You could add using base::foo to your derived class:

class derived : public base
{
public:
     using base::foo;
     void foo(int k)
     {
        std::cout<<"\nHello from foo with value = "<<k<<"\n";
     }
};

Edit: The answer for this question explains why your base::foo() isn't directly usable from derived without the using declaration.

Community
  • 1
  • 1
Josh Townzen
  • 1,338
  • 1
  • 10
  • 17
  • but why base class's foo() is not accessible in derived class while I have inherited that base class... – Ashish Yadav Mar 13 '10 at 08:06
  • 2
    @ashish: It is accessible (which means access specifiers of public/protected/private), but it is *hidden*. Read the question Josh linked: http://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the –  Mar 13 '10 at 09:23
  • oh whew, I was about to create wrappers to delegate to the base overloads that became hidden just to expose them from the derived object, kind of like `void foo() { base::foo() }`, and then figured there must be a better way to do this, and thankfully, yes there is. There's also the option of doing `d.base::foo()`, but from a client side perspective that seems kind of annoying. – solstice333 Sep 24 '17 at 19:53
6
d.base::foo();

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
2

Add following statement to your derived class :

using Base::foo ;

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.

mukeshkumar
  • 2,698
  • 3
  • 19
  • 20
0

we can call base class method using derived class object. as per below code.

#include<iostream.h>
class base
{
public:
void fun()
{
    cout<<"base class\n";
}
};
class der : public base
{
public:
void fun()
{
cout<<"derived class\n";
}
};
int main()
{

der d1;


d1.base::fun();


return 0;
} 

Output will be base class.

ashutosh
  • 405
  • 6
  • 10