#include <iostream>
#include <memory>
class Widget {
public:
Widget() { std::cout << "💎 Widget Allocated on Heap" << std::endl; }
~Widget() { std::cout << "🧹 Widget Automatically Destructed (Zero Memory Leaks)" << std::endl; }
void execute() { std::cout << "⚡ Widget executing task..." << std::endl; }
};
int main() {
// Modern C++ Memory Allocation (No explicit delete required!)
std::unique_ptr<Widget> w1 = std::make_unique<Widget>();
w1->execute();
// Transfer ownership using std::move
std::unique_ptr<Widget> w2 = std::move(w1);
if (!w1) std::cout << "w1 is now null after std::move ownership transfer." << std::endl;
return 0;
} // w2 goes out of scope here -> Widget destructor fires automatically!
⊞Windows 11 Command Prompt Execution OutputWindows 11 Verified
⊞Administrator: Windows 11 Command Prompt (g++ GCC 13.2.0)
—□✕
Microsoft Windows [Version 10.0.22631.3007] (Windows 11 Pro x86_64)
(c) Microsoft Corporation. All rights reserved.
C:\Users\Student>#include <iostream>
C:\Users\Student\CPP_Project> g++ -o main.exe main.cpp
C:\Users\Student\CPP_Project> main.exe
Hello World from C++ on Windows 11 Pro!
[Process exited with code 0 in 0.001 seconds]