Multilevel Driver Initialization

#include <iostream>
using namespace std;

// TODO 1: Create CoreDriver class
//         - Constructor prints "Core driver initialized"
class CoreDriver{
    public:
    CoreDriver()
    {
        cout<<"Core driver initialized\n";
    }

};

// TODO 2: Create CommDriver class
//         - Publicly inherits from CoreDriver
//         - Constructor prints "Comm driver initialized"
class CommDriver:public CoreDriver{
    public:
    CommDriver()
    {
        cout<<"Comm driver initialized\n";
    }
};

// TODO 3: Create SpiDriver class
//         - Publicly inherits from CommDriver
//         - Constructor takes int speed
//         - Prints "SPI driver initialized"
//         - Stores speed
//         - Function prints "SPI speed <speed>"
class SpiDriver:public CommDriver{
    public:
    int s;
    SpiDriver(int speed)
    {
        cout<<"SPI driver initialized\n";
        s= speed;
    }

    void print()
    {
        cout<<"SPI speed "<<s;
    }
};

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

    // TODO 4: Create SpiDriver object
    SpiDriver s(speed);
    // TODO 5: Call function to print SPI speed
    s.print();

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

8

Expected Output

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