Overload new and delete operator to manipulate objects of Student class.Class Student must contain data members like char * name,int roll_no,int branch etc.The overloaded new and delete operator must allocate and deallocate memory for the student class object and its data members.
Here is my code. The problem is that I'm getting some runtime error after reading the roll number of 2nd student record. I'll have to admit that I dont really understand the overloaded functions arguments properly. Please help me
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<new>
#include<string.h>
using namespace std;
class student
{
char *name,*branch;
int rno;
public:
student()
{
}
student(char n[],int r,char br[])
{
strcpy(name,n);
rno=r;
strcpy(branch,br);
}
void putdata();
void *operator new(size_t size);
void operator delete(void *p);
};
void *student::operator new(size_t size)
{
void *p;
p=malloc(size);
return p;
}
void student::operator delete(void *p)
{
free(p);
}
void student::putdata()
{
cout<<"\nName : "<<name<<"\nRoll No : "<<rno<<"\nBranch : "<<branch;
}
int main()
{
int n;
int rno;
char bra[10],name[10];
student *s[10];
cout<<"\nenter the no. of students:";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"\nenter name:";
cin>>name;
cout<<"\nenter roll no:";
cin>>rno;
cout<<"\nenter branch:";
cin>>bra;
s[i]=new student(name,rno,bra);
}
cout<<"\nstudent details\n";
for(int i=0;i<n;i++)
{
s[i]->putdata();
delete s[i];
}
return(0);
}