Reusing Base Initialization

#include <iostream>
using namespace std;

// TODO 1: Create BaseDriver class
//         - Add function initBase()
//         - Prints base initialization messages
class BaseDriver{
    public:
    void initBase(){
        printf("Base driver init start\nBase driver init complete\n");
    }
};
// TODO 2: Create SpiDriver class
//         - Inherits publicly from BaseDriver
//         - Add function initSpi()
//         - Calls initBase()
//         - Prints "SPI driver initialized"
class SpiDriver : public BaseDriver{
    public:
    void initSpi(){
        initBase();
        printf("SPI driver initialized\n");
    }
};
// TODO 3: Create I2cDriver class
//         - Inherits publicly from BaseDriver
//         - Add function initI2c()
//         - Calls initBase()
//         - Prints "I2C driver initialized"
class I2cDriver : public BaseDriver{
    public:
    void initI2c(){
        initBase();
        printf("I2C driver initialized\n");
    }
};
int main() {
    int mode;
    cin >> mode;

    // TODO 4: If mode == 0, create SpiDriver and call initSpi()
    if(!mode){
        SpiDriver s;
        s.initSpi();
    }
    // TODO 5: If mode == 1, create I2cDriver and call initI2c()
    if(mode){
        I2cDriver i;
        i.initI2c();
    }
    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Expected Output

Base driver init start Base driver init complete SPI driver initialized