Manage a Device with a unique_ptr

#include <iostream>
#include <memory>
using namespace std;

class Sensor {
public:
    int read() { return 42; }
};

int main() {
    // your code here: declare unique_ptr<Sensor> and call read() using ->

    std::unique_ptr<Sensor> sen = std::make_unique <Sensor> ();

    int value = sen->read();

    cout<<"Sensor reading: "<< value <<endl;

    return 0;
}
////unique_ptr is used when you want dynamic memory management:
//Avoids manually using new, prevents memory leaks,
//make_unique is more modern C++ (C++14+) and preferred style-- safe, concise, and exception-safe way to create a unique_ptr.
Upvote
Downvote
Loading...

Input

Expected Output

Sensor reading: 42