#include <iostream>
using namespace std;
class Buffer {
private:
int data;
public:
Buffer() : data(0) {
cout << "Buffer created" << endl;
}
~Buffer() {
cout << "Buffer destroyed" << endl;
}
void write(int value) {
data = value;
}
int read() const {
return data;
}
};
class Driver {
private:
// Composition: Driver owns the Buffer
Buffer buf;
public:
// Constructor: Prints initialization and writes the value to the buffer
Driver(int value) {
buf.write(value);
cout << "Driver initialized" << endl;
}
// Destructor: Observes the shutdown sequence
~Driver() {
cout << "Driver destroyed" << endl;
}
void print() const {
cout << "Stored value: " << buf.read() << endl;
}
};
int main() {
int value;
if (!(cin >> value)) return 0;
// Local scope to trigger destruction
{
Driver drv(value);
drv.print();
} // drv goes out of scope here
return 0;
}
Input
10
Expected Output
Buffer created Driver initialized Stored value: 10 Driver destroyed Buffer destroyed