Practical-10
Write a program to demonstrate the overloading of memory
management operators.
Program:
#include <iostream>
#include <cstdlib>
using namespace std;
class Box {
private:
int size;
public:
Box() {
cout << "CONSTRUCTOR CALLED" << endl;
}
~Box() {
cout << "DESTRUCTOR CALLED" << endl;
}
void* operator new(size_t size) {
cout << "MEMORY ALLOCATED" << endl;
void* p = malloc(size);
return p;
}
void operator delete(void* p) {
cout << "MEMORY DEALLOCATED" << endl;
free(p);
}
};
int main() {
Box* b = new Box();
delete b;
return 0;
}
Output: