111. Fix Lost Driver Data

#include <iostream>
using namespace std;

// Base driver class
class BaseDriver {
public:
    BaseDriver(int v) : baseConfig(v) {}

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

protected:
    int baseConfig;
};

// Derived driver class
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;
};

// Base handler
void processDriver(const BaseDriver& driver) {
    driver.printBase();
}

// Overload for derived driver
void processDriver(const DerivedDriver& driver) {
    driver.printBase();
    driver.printDerived();
}

int main() {
    int baseVal, derivedVal;
    cin >> baseVal >> derivedVal;

    DerivedDriver driver(baseVal, derivedVal);
    processDriver(driver);

    return 0;
}

Explanation & Logic Summary:

  • Passing objects incorrectly can cause part of the object to be ignored.
  • Passing by reference avoids unnecessary copying.
  • Function overloading ensures the correct function is selected at compile time.
  • No virtual functions or dynamic memory are required.

Firmware Relevance & Real-World Context:

This problem reinforces:

  • Safe object passing in firmware
  • Preventing silent configuration loss
  • Compile-time polymorphism
  • Writing predictable, allocation-free C++ code

 

 

 

 

Loading...

Input

10 5

Expected Output

Base config 10 Derived config 5