Saturday, 23 June 2012

write a c++ program to consider a class point containing x,y coordinates.

/*write a c++ program to consider a class point  containing x,y coordinates. write necessary
function for the following cases
1. to accept a point
2.to display it
3.to find distance between two point using operator overloading(-)
(use friend function)
*/
#include<conio.h>
#include<iostream.h>
int ans1,ans2;
class point
{
int x,y;
public:void accept()
       {
       cout<<"enter two coordinates\nx=";
       cin>>x;
       cout<<"y=";
       cin>>y;
       }
       friend void display()
       {
       cout<<"the difference is:-\nx="<<ans1
       <<"\ny="<<ans2;
       }
       void operator -(point &p2);
};
void point::operator -(point &p2)
{
ans1=x-p2.x;
ans2=y-p2.y;
}
int main()
{
point p1,p2;
clrscr();
p1.accept();
p2.accept();
p1-p2;
display();
getch();
return 0;
}
/*
enter two coordinates
x=6
y=9
enter two coordinates
x=3
y=2
the difference is:-
x=3
y=7
*/

No comments: