Overloading new and delete operator program

View previous topic View next topic Go down

Overloading new and delete operator program

Post by Prateek Gupta on Mon May 08, 2017 8:27 pm

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;
}


Prateek Gupta
Admin

Posts : 6
Join date : 2017-04-04

View user profile http://clanofcoders.forumotion.com

Back to top Go down

Re: Overloading new and delete operator program

Post by akarshsomani on Sat May 13, 2017 7:29 am

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

View user profile

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum