97. Driver Resource Composition

In embedded systems, a driver often owns multiple internal resources whose lifetimes must be fully controlled and deterministic.
These resources are typically created when the driver is initialized and destroyed automatically when the driver shuts down.

In this task, you will model a driver that owns multiple internal resources using composition, ensuring correct construction and destruction order without dynamic allocation.

The driver owns:

  • An internal buffer
  • A hardware register block

Both resources must:

  • Be created automatically when the driver object is created
  • Be destroyed automatically when the driver object is destroyed
  • Never be accessed directly from main()

Do This:

  • Create a Buffer class that stores one integer
  • Create a RegisterBlock class that stores one integer
  • Create a Driver class that owns both objects as direct members
  • Read two integers from input
  • Store the first value in the buffer
  • Store the second value in the register block
  • Print both stored values only through the Driver
  • Observe the exact construction and destruction order when the driver goes out of scope

⚠️ Important C++ Rule (Required for Correct Output):
Class member objects are constructed in the order they are declared in the class, and destroyed in the reverse order, regardless of constructor body code.
Your solution must rely on this rule to match the required output exactly.

Program Flow:

  1. Read two integers from input
  2. Enter a local scope
  3. Create a Driver object
  4. Driver stores values in its internal resources
  5. Driver prints both stored values
  6. Scope ends
  7. Driver shuts down
  8. Owned resources are destroyed automatically

Input:

Two integers separated by whitespace

Output (Exact Order Required):

Buffer created
RegisterBlock created
Driver initialized
Buffer value: <value1>
Register value: <value2>
Driver destroyed
RegisterBlock destroyed
Buffer destroyed

Constraints:

  • Buffer and RegisterBlock must not be created in main()
  • Access to both resources must occur only through the Driver
  • No dynamic memory allocation
  • No inheritance
  • Member declaration order must enforce construction order
  • Output format and order must match exactly

 

 

 

Loading...

Input

10 20

Expected Output

Buffer created RegisterBlock created Driver initialized Buffer value: 10 Register value: 20 Driver destroyed RegisterBlock destroyed Buffer destroyed