Coding Discussions
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Overloading new and delete operator program

2 posters

Go down

Overloading new and delete operator program Empty Overloading new and delete operator program

Post by Prateek Gupta 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

https://clanofcoders.board-directory.net

Back to top Go down

Overloading new and delete operator program Empty Re: Overloading new and delete operator program

Post by akarshsomani 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

Back to top Go down

Back to top

- Similar topics

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