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 cannot! class Derived : Base { public override void Method() { // This will call Base.
Using a qualified-id to call a base class' function works irrespectively of what happens to that function in the derived class - it can be hidden, it can be overridden, it can be made private (by using a using-declaration), you're directly accessing the base class' function when using a qualified-id.
base (C# Reference)The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method. Specify which base-class constructor should be called when creating instances of the derived class.
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.
d.base::foo();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With