CS201 # 4 Solution June 2012
Solution:-
#include <iostream.h>
class Location
{
int l1;
int l2;
public:
Location();
Location(int lon,int lat);
void view();
Location operator ++();
Location operator --();
void* operator new (size_t size);
void operator delete( void * ptr );
};
Location::Location()
{
l1=0;
l2=0;
}
Location::Location(int lon,int lat)
{
l1=lon;
l2=lat;
}
void Location::view()
{
coutendl"Longitude : "l1endl;
cout"Latitude : "l2endlendl;
}
void* Location::operator new(size_t size)
{
cout"Overloaded new operator called....." endl;
void * rtn = malloc (size ) ;
return rtn;
}
Location Location::operator ++()
{
++l1;
++l2;
}
Location Location::operator --()
{
--l1;
--l2;
}
void Location :: operator delete( void *memory )
{
cout"Overload delete operator called....."endlendl;
free( memory );
}
main()
{
system("cls");
Location l1(10,20), *l2= new Location(30,40);
coutendl"Coordinates for Location 1:";
l1.view();
++l1;
cout"After applying overloaded ++ operator on Location 1 : ";
l1.view();
cout"Coordinates for Location 2:";
l2[0].view();
--l2[0];
cout"After applying overloaded -- operator on Location 2 : ";
l2[0].view();
delete l2;
system("pause");
}
ANOTHER SOLUTION
#include<iostream>
using namespace std;
class Location
{
private:
int longitude;
int latitude;
public:
void display()
{
cout<<"Longitude = "<<longitude<<endl<<"Latitude = "<<latitude<<endl;
}
int setLongitude(int i)
{
longitude=i;
}
int setLatitude(int i)
{
latitude=i;
}
Location()
{
longitude=10;
latitude=20;
}
Location(int i, int j)
{
longitude=i;
latitude=j;
}
Location operator ++()
{
++longitude;
++latitude;
}
Location operator --()
{
--longitude;
--latitude;
}
void* Location::operator new(size_t size)
{
cout<<"Overloaded new operator called..."<<endl;
void * p=malloc(size);
return p;
}
void Location::operator delete (void *p)
{
cout<<"Overload delete operator called...";
Location* pc=static_cast<Location*>(p);
free(p);
}
};
main()
{
Location *p, a, b(30,40);
p=new Location(1,2);
cout<<endl<<endl<<"Coordinates for Location 1:"<<endl;
a.display();
cout<<endl<<endl<<"After applying overloaded ++ operator on Location 1:"<<endl;
++a;
a.display();
cout<<endl<<endl<<"Coordinates for Location 2:"<<endl;
b.display();
cout<<endl<<endl<<"After applying overloaded -- operator on Location 2:"<<endl;
--b;
b.display();
cout<<endl<<endl;
delete p;
system("pause");
}
using namespace std;
class Location
{
private:
int longitude;
int latitude;
public:
void display()
{
cout<<"Longitude = "<<longitude<<endl<<"Latitude = "<<latitude<<endl;
}
int setLongitude(int i)
{
longitude=i;
}
int setLatitude(int i)
{
latitude=i;
}
Location()
{
longitude=10;
latitude=20;
}
Location(int i, int j)
{
longitude=i;
latitude=j;
}
Location operator ++()
{
++longitude;
++latitude;
}
Location operator --()
{
--longitude;
--latitude;
}
void* Location::operator new(size_t size)
{
cout<<"Overloaded new operator called..."<<endl;
void * p=malloc(size);
return p;
}
void Location::operator delete (void *p)
{
cout<<"Overload delete operator called...";
Location* pc=static_cast<Location*>(p);
free(p);
}
};
main()
{
Location *p, a, b(30,40);
p=new Location(1,2);
cout<<endl<<endl<<"Coordinates for Location 1:"<<endl;
a.display();
cout<<endl<<endl<<"After applying overloaded ++ operator on Location 1:"<<endl;
++a;
a.display();
cout<<endl<<endl<<"Coordinates for Location 2:"<<endl;
b.display();
cout<<endl<<endl<<"After applying overloaded -- operator on Location 2:"<<endl;
--b;
b.display();
cout<<endl<<endl;
delete p;
system("pause");
}