Multilevel Driver Initialization

#include <iostream>
using namespace std;

// TODO 1: Create CoreDriver class
class CoreDriver {
public:
    CoreDriver() {
        cout << "Core driver initialized" << endl;
    }
};

// TODO 2: Create CommDriver class
// Publicly inherits from CoreDriver to ensure Core initializes first
class CommDriver : public CoreDriver {
public:
    CommDriver() {
        cout << "Comm driver initialized" << endl;
    }
};

// TODO 3: Create SpiDriver class
// Publicly inherits from CommDriver
class SpiDriver : public CommDriver {
private:
    int spiSpeed;

public:
    // Constructor takes speed and stores it
    SpiDriver(int speed) : spiSpeed(speed) {
        cout << "SPI driver initialized" << endl;
    }

    // Function to display the stored speed
    void printSpeed() {
        cout << "SPI speed " << spiSpeed << endl;
    }
};

int main() {
    int speed;
    
    // Read integer speed from standard input
    if (!(cin >> speed)) return 0;

    // TODO 4: Create SpiDriver object
    // This triggers the chain: CoreDriver -> CommDriver -> SpiDriver
    SpiDriver mySpi(speed);

    // TODO 5: Call function to print SPI speed
    mySpi.printSpeed();

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

8

Expected Output

Core driver initialized Comm driver initialized SPI driver initialized SPI speed 8