There is nothing much difficult in Overloading new and delete operator................
Here is a short tutorial on it.......
#include <iostream.h>
#include <cstdlib.h>
#include <new>
Class loc {
int longitude,latitude;
public:
loc{}
loc(int lg,int lt) {
longitude=lg;
latitude=lt; }
void show() {
cout<<longitude<<“ “;
cout<< latitude<<“\n”;
}
void *operator new(size_t size);
void operator delete(void *p);
};
Void *loc::operator new(size_t size)
{
void *p;
cout << “ in overloaded new\n.;
p=malloc(size);
if(!p) {
bad_alloc ba;
throw ba;
}
return p;
}
Void loc:;operator delete(void *p)
{
cout << “in overloaded delete “;
free(p);
}
Int main()
{
loc *p1,*p2;
try { p1=new loc(10,20); }
catch(bad_alloc xa) {
cout<<“Allocation error for p1\n”;
return 1; }
try { p2=new loc(-10,-20); }
catch (bad_alloc xa) {
cout<<“Allocation error for p1\n”;
return 1; }
p1->show();
p2->show();
delete p1;
delete p2;
return 0;
}