

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


#include
using namespace std; class Location { int longitude, latitude; public: Location() {} // parameterized constructor Location(int lg, int lt) { longitude = lg; latitude = lt; } void show() { cout << "Longitude = " <<longitude << endl; cout << "Latitude = " <<latitude << "\n"; } void *operator new(size_t size); //overloaded new operator receive a parameter size of size_t void operator delete(void *p); Location operator--(int x); Location operator++(int x); }; // new overloaded relative to loc. void *Location::operator new(size_t size) { void *p; cout << "Overloaded new operator called...\n"; p = malloc(size); // allocate memory through alloc return p; // return the position } // delete overloaded relative to loc. void Location::operator delete(void *p) { cout << "Overload delete operator called...\n"; free(p); // free the allocatated position } // -- overloaded Location Location::operator--(int x) { longitude--; // decrement the longitude latitude--; // decrement the latitude return *this; } // ++ overloaded Location Location::operator++(int x) { longitude++; // increment the longitude latitude++; // increment the latitude return *this; }
int main()
Location *p1 = new Location (10, 20); cout<< endl <<"Coordinates for Location 1 :" << endl; p1->show();
cout<< endl <<"After applying overloaded ++ operator on Location 1"<<endl; (*p1)++; p1->show();
Location p2(30, 40); cout<< endl <<"Coordinates for Location 2 :" << endl; p2.show();
cout<< endl <<"After applying overloaded -- operator on Location 2"<<endl; p2--; p2.show();
cout << endl; delete p1; // free the memory
getch(); return 0; }