Polymorphic Driver Shutdown

#include <iostream>
using namespace std;

class Driver {
public:
   virtual ~Driver() {
        cout << "Base driver shutdown" << endl;
    }
};

class SpiDriver : public Driver {
public:
    ~SpiDriver() {
        cout << "SPI driver shutdown" << endl;
    }
};

class I2cDriver : public Driver {
public:
    ~I2cDriver() {
        cout << "I2C driver shutdown" << endl;
    }
};

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

   // SpiDriver spi;
    //I2cDriver i2c;

    Driver* driver = nullptr;

    if (value == 0) {
        driver = new SpiDriver();
       //driver = &spi;
    } else {
        //driver = &i2c;
        driver = new I2cDriver();
    }

    delete driver;

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

0

Expected Output

SPI driver shutdown Base driver shutdown