Derived class hiding base class same name method

Simple. You should write the ‘using’ declaration under public section of class as below. If you put the declaration in private section, Base functions are available to Derived class but they will become private members of Derived class. That is why your compiler is throwing ‘inaccessible’ error.

#include <iostream>
#include <complex>

using namespace std;

class Base {
public:
    virtual void f( int ) {
        cout << "Base::f(int)" << endl;
    }

    virtual void f( double ) {
        cout << "Base::f(double)" << endl;
    }

    virtual void g( int i = 10 ) {
        cout << i << endl;
    }
};

class Derived: public Base {
    
public:
    using Base::f;

    void f( complex<double> ) {
        cout << "Derived::f(complex)" << endl;
    }

    void g( int i = 20 ) {
        cout << "Derived::g() " << i << endl;
    }
};


int main() {
    Derived d;

    d.f(1.0);
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top