Object Oriented Programming Lab: Beyond The Syllabus Overloading New and Delete Operator Aim

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

OBJECT ORIENTED PROGRAMMING LAB

BEYOND THE SYLLABUS OVERLOADING NEW AND DELETE OPERATOR AIM: To write a C++ program to implement the overloading of new and delete operators to provide dynamic allocation of memory ALGORITHM: Step 1: Start the program. Step 2: Create a class op; Step 3: Declare the overloaded member functions new & delete. Step 4: Invoke the base class constructor and passing arguments for the new function. Step 5: The inbuilt identifiers __FILE__ and __LINE__ will get the current file which under processing and the line which currently executing the __LINE__ will be passed. Step 6: Using malloc the memory will be allocated for the current processing file. Step 7: Invoke the overloaded operated function delete and pass the pointer variable free the memory which is already allocated. Step 8: Stop the program. PROGRAM: #include<iostream.h> #include<conio.h> #include<stdlib.h> class op { public: void *operator new(size_t size, char const *file,int line); void operator delete(void *p); }; void *op::operator new(size_t size, char const *file,int line) { void *p = malloc(size); cout<<"\n New called the file:"<<file<<"\n line:"<<line<<"\n size:"<<size<<"\n p:"<<p<<endl; return p; } void op::operator delete(void *p) { cout<<"\n Delete called p:"<<p<<endl; free(p); } void main()

{ clrscr(); op *X = new(__FILE__,__LINE__)op; delete X; getch(); } OUTPUT: New called the file:NEWDEL1.CPP line:24 size:1 p:0x8fba0df6 Delete called p:0x8fba0df6 RESULT: Thus the C++ program to implement the overloading of new and delete operators to provide dynamic allocation of memory was executed.
CALCULATION OF BANK INTEREST USING DEFAULT ARGUMENT AIM: To calculate bank interest using the concept of default arguments. ALGORITHM: 1. Start the process. 2. Create a class with sufficient data members and two member functions. 3. The default values are specified in the class 4. The two member functions are defined outside the class. 5. The object is created for the class and the member function is called from the main function. 6. The function assigns a default values to the parameter which does not have a matching argument and function call. 7. If the values for the parameter are specified then the default value is not taken. 8. The bank interest is calculated. 9. Display the result. 10. Stop the process. PROGRAM: #include<iostream.h> #include<conio.h> class arg { float sum,amount,p,n,r; public: float value(float p,int n,float r=0.15); void print(char ch='*', int len=13); }; float arg::value(float p,int n, float r) { int year=1;

float sum=p; while(year<=n) { sum=sum*(1+r); year=year+1; }r eturn(sum); } void arg:: print(char ch, int len) { for(int i=1;i<=len;i++) cout<<ch; cout<<"\n"; } void main() { arg a1; float tot, res; clrscr(); a1.print(); tot=a1.value(5000,5); cout<<tot<<endl; a1.print(); cout<<endl; res=a1.value(5000,1,0.20); cout<<res; getch(); }

OUTPUT: **************************** 10056.786133 **************************** 6000 RESULT: Thus the program for calculation of simple interest using default arguments was executed.

You might also like