Reusing Base Initialization

#include <iostream>
using namespace std;

// TODO 1: Create BaseDriver class
//         - Add function initBase()
//         - Prints base initialization messages

// TODO 2: Create SpiDriver class
//         - Inherits publicly from BaseDriver
//         - Add function initSpi()
//         - Calls initBase()
//         - Prints "SPI driver initialized"

// TODO 3: Create I2cDriver class
//         - Inherits publicly from BaseDriver
//         - Add function initI2c()
//         - Calls initBase()
//         - Prints "I2C driver initialized"

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

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

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

int main() {
    int mode;
    cin >> mode;
    if(mode == 0){
        SpiDriver spi;
        spi.initSpi();
    }
    else if(mode == 1){
        I2cDriver i2c;
        i2c.initI2c();
    }
    // TODO 4: If mode == 0, create SpiDriver and call initSpi()
    // TODO 5: If mode == 1, create I2cDriver and call initI2c()

    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Expected Output

Base driver init start Base driver init complete SPI driver initialized