c_-_c_operator_overloading:new_and_delete_operator_overloading
C - C++ Operator Overloading - New and Delete operator overloading
#include<iostream.h> #include<conio.h> const int ARRAY=10; class vector { public: int *array1; void *opreator new(size_t size) { vector *d; d=::new vector; d->array1= new int[ARRAY]; return d; } void operator delete(void *vec) { vector *c; c=(vector *)vec; delete(int *)c->array1; ::delete vec; } void read() { for(int i=0;i<ARRAY;i++) { cout<<"vector ["<<i<<"]=?"; cin>>array[i]; } } int sum() { int sums=0; for (int i=0;i<ARRAY;i++) sums+=array1[i]; return sums; } }; void main() { clrscr(); vector *d=::new vector; cout<<"enter vector data.."<<endl; d->read(); cout<<"sum of vector="<<d->sum(); delete d; getch(); }
c_-_c_operator_overloading/new_and_delete_operator_overloading.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1