45. Manage a Device with a unique_ptr

We have already defined a class Sensor with a method:

int read() { return 42; }

Your task is to:

  1. Declare a unique_ptr<Sensor> object in main().
  2. Call the read() method using the arrow operator (->).
  3. Print the result in the format:
Sensor reading: 42

 

Example

Output:

Sensor reading: 42

 

Why this output?

Because the unique_ptr manages the Sensor object and the read() function always returns 42.

 

Question Significance

This teaches how to declare and use a unique_ptr to safely manage a dynamically allocated object.

Loading...

Input

Expected Output

Sensor reading: 42