#include<iostream.h>
#include<conio.h>
class loc
{
int longitude, latitude;
public:
loc(){};
loc(int lg, int lt)
{
longitude = lg;
latitude = lt;
}
void show()
{
cout<<longitude<<"\t";
cout<<latitude<<endl;
}
loc operator+(loc op2);
loc operator, (loc op2);
};
// overload comm. for loc
loc loc :: operator,(loc op2)
{
loc temp;
temp.longitude = op2.longitude;
temp.latitude = op2.latitude;
cout<<op2.longitude <<"\t"<< op2.latitude<<"\n";
return temp;
}
// Overload + for loc
loc loc::operator+(loc op2)
{
loc temp;
temp.longitude = op2.longitude + longitude;
temp.latitude = op2.latitude + latitude;
return temp;
}
int main()
{
clrscr();
loc obj1(10, 20), obj2(5, 30), obj3(1, 1);
obj1.show();
obj2.show();
obj3.show();
cout<<endl;
obj1=(obj1, obj2+obj3, obj3);
obj1.show(); // display 1 1, the value of obj3
getch();
return 0;
}
|
|

| This program that illustrates the effect of overloading the comma operator. |
|
| |
|
|
|
|