Dannz
Dannz

Reputation: 495

Polymorphism in C++ why is this isn't working?

class Base {
    public:
    virtual void f();
    void f(int);
    virtual ~Base();
};

class Derived : public Base {
public:
    void f();
};

int main()
{
    Derived *ptr = new Derived;
    ptr->f(1);
    delete ptr;
    return 0;
}

ptr->f(1); is showing the following error: "too many arguments in function call".

Why is this isn't possible? isn't derived inherited all the functions form base and is free to use any of them? I could call it explicitly and it would work but why isn't this allowed?

Upvotes: 6

Views: 1760

Answers (4)

Neo
Neo

Reputation: 11

"Derived *ptr" This definition will only allow "ptr" to access all the member functions which are defined via Derived class or its child class. But, it will not allow u to access the member functions which are coming in Derived class because of inheritance.

If u want to access base class version of function "f" then use "Base *ptr" and it will choose the correct version of function automatically as shown :)

class Base {
    public:
        virtual void f()
        {
            cout<<"Here 2"<<endl;
        }
        void f(int x)
        {
            cout<<"Here 1"<<endl;
        }
        virtual    ~Base() {}
};

class Derived : public Base {
    public:
        void f()
        {
            cout<<"Here 3"<<endl;
        }
        virtual   ~Derived() {}
};

int main()
{
    Base *ptr = new Derived;
    ptr->f(1);
    delete ptr;
    return 0;
}

output is Here 1

Upvotes: 0

sameerkn
sameerkn

Reputation: 2259

Suppose for time being that Derived do have the access to the function void Base::f(int);. Then it will be a case of function overloading. But, this is invalid case of function overloading since one function f(int);is in Base and other function f(); is in Derived. Function Overloading happens inside a single class. Function Overriding happens across classes. The example you posted is a case of Name Hiding in Inheritance

Upvotes: 0

Daksh Gupta
Daksh Gupta

Reputation: 7804

As mentioned by @Some Programming Dude : it is because of Hiding.

To understand hiding in relatively simpler language

Inheritance is meant to bring Baseclass variables / functions in Derived class.

But, on 1 condition : "If its not already available in Derived Class"

Since f() is already available in Derived, it doesn't make sense to look at Base class from compiler perspective.

That's the precise reason why you need to scope clarify while calling this function

void main()
    {
        Derived *ptr = new Derived;
        ptr->Base::f(1);
        delete ptr;
    }

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409166

What you are seeing is called hiding.

When you override the function void f() in the Derived class, you hide all other variants of the f function in the Base class.

You can solve this with the using keyword:

class Derived : public Base {
public:
    using Base::f;  // Pull all `f` symbols from the base class into the scope of this class

    void f() override;  // Override the non-argument version
};

Upvotes: 15

Related Questions