Fix Lost Driver Data

#include <iostream>
using namespace std;

// Base driver class - DO NOT MODIFY
class BaseDriver {
public:
    BaseDriver(int v) : baseConfig(v) {}

    void printBase() const {
        cout << "Base config " << baseConfig << endl;
    }

protected:
    int baseConfig;
};

// Derived driver class - DO NOT MODIFY
class DerivedDriver : public BaseDriver {
public:
    DerivedDriver(int b, int d) : BaseDriver(b), derivedConfig(d) {}

    void printDerived() const {
        cout << "Derived config " << derivedConfig << endl;
    }

private:
    int derivedConfig;
};

/**
 * Overloaded processing functions.
 * In embedded systems, using overloading instead of virtual functions
 * saves memory (no vtable) and improves performance (static dispatch).
 */

// Function for BaseDriver objects
void processDriver(const BaseDriver& driver) {
    driver.printBase();
}

// Overloaded function for DerivedDriver objects
// This ensures the derived configuration is not ignored.
void processDriver(const DerivedDriver& driver) {
    driver.printBase();    // Call method from base
    driver.printDerived(); // Call method from derived
}

int main() {
    int baseVal, derivedVal;
    
    // 1. Read two integers from input
    if (!(cin >> baseVal >> derivedVal)) return 0;

    // 2. Create a DerivedDriver object
    DerivedDriver driver(baseVal, derivedVal);

    // 3. Pass the object to a processing function
    // 4. Print configuration values inside the function
    processDriver(driver);

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

10 5

Expected Output

Base config 10 Derived config 5