Loading....
Coupon Accepted Successfully!

 

Overloading NEW and DELETE In Classes

The new and delete operators can be overloaded in a similar manner to other operators. It is interesting to observe their working when they are overloaded for multiple classes. The following program explains the working of overloaded new and delete operators in multiple classes:
 

14.12 Write a program to overload new and delete operators

#include<iostream.h>

#include<stdlib.h>

#include<string.h>

#include<conio.h>

#include<new.h>

#define T_BYTES 2

void warning()

{

cout<<endl<<“Cannot allocate memory”;

exit(1);

}

class boy

{

private :

char name [20];

int age;

float height;

public :

void *operator new (size_t);

void operator delete (void *t);

void assign(char *nm, int a, float h);

void display();

~boy();

};

struct limit

{

boy o;

int position ;

};

int signal=0;

struct limit *l=0;

void *boy ::operator new (size_t s)

{

int j;

if (signal==0)

{

l=(limit*) malloc(s * T_BYTES);

if (l==NULL) warning();

for (j=0;j<T_BYTES;j++)

l[j].position=0;

signal=1;

l[0].position=1;

return &l[0].o;

}

else

{

for (j=0;j<T_BYTES;j++)

{

if (l[j].position==0)

{

l[j].position=1;

return &l[j].o;

}

}

warning();

}

return NULL;

}

void boy ::operator delete ( void *t)

{

if (t==NULL) return ;

for (int j=0;j<T_BYTES;j++)

{

if (t==&l[j].o)

{

l[j].position=0;

strcpy(l[j].o.name,””);

l[j].o.age=0;

l[j].o.height=0.0;

}

}

}

void boy::assign (char *nm, int a, float h)

{

strcpy (name,nm);

age=a;

height=h;

}

void boy::display()

{

cout<<endl<<name<<“\t”<<age<<“\t”<<height;

}

boy::~boy()

{

free(l);

cout<<endl<<“In Destructor (Memory released)”;

}

void main()

{

clrscr();

void warning();

set_new_handler (warning);

boy *b1,*b2,*b3;

b1=new boy;

b1->assign(“Sanjay”,20,4.5);

b2=new boy;

b2->assign(“Vijay”,22,5.1);

b1->display();

b2->display();

delete b1;

delete b2;

b1->display();

b2->display();

b1=new boy;

b2=new boy;

b3=new boy;

b3->display();

}

OUTPUT

Sanjay 20 4.5

Vijay 22 5.1

In Destructor (Memory released)

In Destructor (Memory released)

     0      0

     0      0

Cannot allocate memory

 
Explanation: In the above program, new and delete operators are overloaded class by class. The macro T_BYTES is initialized with the number of records for which the memory is to be occupied. The member function assign() is used to initialize the data members of the objects. The display() function is used to display the contents on the screen. The warning() function displays a warning message when called. It is associated with set_new_handler() and executed when memory allocation fails. The class boy is declared with three member variables.
 
In function main(), *b1, *b2, and *b3 are pointers to structure and by using the new operator, memory is allocated to them. Consider the statement l = (limit*) malloc (s * T_BYTES); here, the memory is allocated only for two records, and, hence, when we try to allocate memory for the third object, the function warning() is executed and it prompts the message “Cannot allocate memory.” The delete operator is used to release the memory allocated. The statements delete b1 and delete b2 release the memory allocated by these objects. After releasing the memory, if each function calls the display() function, the contents displayed of these objects is zero. This is obviously the effect of the delete operator.





Test Your Skills Now!
Take a Quiz now
Reviewer Name