#include <iostream>
class DynamicBuffer {
private:
int *data;
int size;
public:
// Constructor with Member Initializer List
DynamicBuffer(int s) : size(s), data(new int[s]) {
std::cout << "🏗️ Allocated buffer of size " << size << " on Heap." << std::endl;
}
// Destructor (RAII Resource Cleanup)
~DynamicBuffer() {
delete[] data;
std::cout << "🧹 Destructor freed Heap memory buffer." << std::endl;
}
};
int main() {
{
DynamicBuffer buf(1024); // Object scope begins
} // Object goes out of scope here -> Destructor fires automatically!
return 0;
}
⊞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]