#include <iostream> using namespace std; class ClockController { public: ClockController() { cout << "Clock enabled" << endl; } ~ClockController() { cout << "Clock disabled" << endl; } void write(int offset, int value) { cout << "Clocked write: " << offset << " " << value << endl; } }; class Peripheral { private: ClockController& clk; //stores the address of a pre existing clk obj public: Peripheral(ClockController& c): clk(c) { //recieve the clk obj as address cout << "Peripheral ready" << endl; } ~Peripheral() { cout << "Peripheral stopped" << endl; } void writeReg(int offset, int value) { clk.write(offset, value); // write through clk } }; class Driver { private: // Must own the clock ClockController clk; Peripheral peri; //initialise peri obj for now, pass clk obj in the constructor public: Driver(): peri(clk){ cout << "Driver started" << endl; } void operate(int offset, int value) { peri.writeReg(offset, value); //operate through peri } ~Driver() { cout << "Driver stopped" << endl; } }; int main() { int offset, value; cin >> offset >> value; { Driver drv; drv.operate(offset, value); } return 0; }
Test Cases
Test Results
Input
8 55
Expected Output
Clock enabled Peripheral ready Driver started Clocked write: 8 55 Driver stopped Peripheral stopped Clock disabled