Overloading new and delete operator program
2 posters
Page 1 of 1
Overloading new and delete operator program
- Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Number
{
private:
int *arr;
public:
void *operator new(size_t size)
{
void *ptr = ::new int[size];
return ptr;
}
void operator delete(void *ptr)
{
::delete(ptr);
cout<<"Memory deallocated"<<endl;
}
void get_data();
void show_data();
};
void Number::get_data()
{
arr= (int *)malloc(10*sizeof(int));
cout<<"Enter the elements"<<endl;
for(int i=0;i<5;i++)
cin>>arr[i];
}
void Number::show_data()
{
for(int i=0;i<5;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
int main()
{
Number *n = new Number;
n->get_data();
n->show_data();
delete n;
return 0;
}
Re: Overloading new and delete operator program
what is the purpose of using size,where you are passing size..what is the purpose of new operator if you have to allocate memory of arr by malloc function..
akarshsomani- Posts : 21
Join date : 2017-04-04
Similar topics
» Program given by sir i.e given in Reema Thareja on Overloading of new and delete operators is not working . Debug it !!
» templet program
» please check the error and correct the program
» IP6: Write a C++ program that uses the concept of hybrid inheritance.
» Program related to Object Slicing : Why the output is not coming In Base a=2 ?
» templet program
» please check the error and correct the program
» IP6: Write a C++ program that uses the concept of hybrid inheritance.
» Program related to Object Slicing : Why the output is not coming In Base a=2 ?
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum