Name Hiding in Inheritance

#include <iostream>
using namespace std;

// Base driver class
class BaseDriver {
public:
    void configure(int value) {
        cout << "Base configuration value " << value << endl;
    }
};

// Derived driver class
class DerivedDriver : public BaseDriver {
public:
    // This brings BaseDriver::configure into the scope of DerivedDriver
    // to prevent name hiding.
    using BaseDriver::configure;

    void configure() {
        cout << "Derived default configuration" << endl;
    }
};

int main() {
    int value;
    // Read integer N
    if (!(cin >> value)) return 0;

    // Create a DerivedDriver object
    DerivedDriver driver;

    // Call derived configure() (no parameters)
    driver.configure();

    // Call base configure(int) through the derived object
    // This works because of the 'using' keyword above.
    driver.configure(value);

    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Input

8

Expected Output

Derived default configuration Base configuration value 8