Wednesday, September 29, 2010

(OOPS)Overloding new & delete operator(longitude,latitude)

EX NO.4                                    PROGRAM TO OVERLOAD NEW AND DELETE OPERATOR


AIM: To write a c++ program to perform type conversion from basic to class type and class to basic type.

ALGORITHM: 
Step 1: Start the program.
Step 2: A member function,parametrized constructor is created to initialize the value of latitude and longitude.
Step 3: Another member function is created to display latitude and longitude values.
Step 4: New operator overloading function is created to allocate the memory.
Step 5: Delete operator overloading function is created to deallocate the memory.
Step 6: A main function is created to implement new and delete operator overloading.
Step 7:End the program.


PROGRAM:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class position
{
float longitude,latitude;
public:
position()
{
};
position (float a, float b)
{
latitude=a;
longitude=b;
}
void *operator new(size_t size)
{
cout<<"\nin new";
return malloc(sizeof(size));
}
void display()
{
cout<<"\n\nlatitude:"<<latitude<<"\n\nlongitude:"<<longitude<<"\n\n";
}
void operator delete(void *p)
{
cout<<"\nin delete \n";
}
};
main()
{
clrscr();
position a(23.4,78.6);
position *b;
cout<<"A value:\n";
a.display();
cout<<"B value:\n";
b=new position;
b->display();
*b=a;
delete b;
getch();
}


RESULT:
      Thus a c++ program for performing type conversion is written,executed and verified successfully.