#include <iostream>
using namespace std;
class Buffer {
private:
int data;
public:
Buffer() {
cout << "Buffer created" << endl;
}
~Buffer() {
cout << "Buffer destroyed" << endl;
}
void write(int value) {
data = value;
}
int read() const {
return data;
}
};
class RegisterBlock {
private:
int reg;
public:
RegisterBlock() {
cout << "RegisterBlock created" << endl;
}
~RegisterBlock() {
cout << "RegisterBlock destroyed" << endl;
}
void write(int value) {
reg = value;
}
int read() const {
return reg;
}
};
class Driver {
private:
// Declare Buffer first
Buffer buf;
// Declare RegisterBlock second
RegisterBlock regBlock;
public:
// Constructor must store input values
// Print "Driver initialized"
Driver(int _bufVal, int regVal)
{
buf.write(_bufVal);
regBlock.write(regVal);
cout << "Driver initialized" << endl;
}
void print() const {
// Print buffer value
// Print register value
cout << "Buffer value: " << buf.read() << endl
<< "Register value: " << regBlock.read() << endl;
}
~Driver()
{
cout << "Driver destroyed" << endl;
}
// Destructor must print "Driver destroyed"
};
int main() {
int a, b;
cin >> a >> b;
{
Driver drv(a, b);
drv.print();
}
return 0;
}
Input
10 20
Expected Output
Buffer created RegisterBlock created Driver initialized Buffer value: 10 Register value: 20 Driver destroyed RegisterBlock destroyed Buffer destroyed