#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 reg;
public:
// Constructor must store input values
// Print "Driver initialized"
Driver(int a, int b){
buf.write(a);
reg.write(b);
cout << "Driver initialized" << endl;
}
void print() const {
// Print buffer value
cout<< "Buffer value: "<< buf.read() << endl;
// Print register value
cout << "Register value: " << reg.read()<< endl;
}
// Destructor must print "Driver destroyed"
~Driver(){
cout << "Driver destroyed" << endl;
}
};
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