109. Reusing Base Initialization

#include <iostream>
using namespace std;

// Base driver class
class BaseDriver {
public:
    void initBase() {
        cout << "Base driver init start" << endl;
        cout << "Base driver init complete" << endl;
    }
};

// SPI driver
class SpiDriver : public BaseDriver {
public:
    void initSpi() {
        initBase();
        cout << "SPI driver initialized" << endl;
    }
};

// I2C driver
class I2cDriver : public BaseDriver {
public:
    void initI2c() {
        initBase();
        cout << "I2C driver initialized" << endl;
    }
};

int main() {
    int mode;
    cin >> mode;

    if (mode == 0) {
        SpiDriver spi;
        spi.initSpi();
    } else {
        I2cDriver i2c;
        i2c.initI2c();
    }

    return 0;
}

Explanation & Logic Summary:

The base driver contains mandatory shared initialization logic.
Multiple derived drivers reuse this logic by calling the same base function.
This prevents duplication and guarantees consistent hardware setup.

Firmware Relevance & Real-World Context:

In real embedded systems, SPI, I2C, and UART drivers often share:

  • Clock enable sequences
  • Reset logic
  • Safety checks

Writing this logic once in a base driver:

  • Prevents bugs
  • Simplifies maintenance
  • Makes audits easier

This is a valid and realistic use of inheritance in embedded firmware design.

 

 

 

 

 

Loading...

Input

0

Expected Output

Base driver init start Base driver init complete SPI driver initialized