Docsity

Location, Longitude, Latitude-Applications of Programming-C Plus Plus Code, Exercises of Applications of Computer Sciences

Toggle sidebar
bg1
#include <iostream>
#include <cstdlib>
#include <conio.h>
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()
docsity.com
pf2

Partial preview of the text

Download Location, Longitude, Latitude-Applications of Programming-C Plus Plus Code and more Exercises Applications of Computer Sciences in PDF only on Docsity!

#include #include #include <conio.h>

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()

docsity.com

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; }

docsity.com