Consider a class point containing x and y coordinates. Write necessary functions for the following cases: 1. to accept a point 2. to display it 3. to find distance between two points using operator overloading (-) (Use friend function)


#include<iostream.h>
#include<conio.h>

class point
{
  int x;
  int y;

  public:
  void accept()
  {
    cout<<endl<<"Enter x,y co-ordinates of point: ";
    cin>>x>>y;
  }

  void display()
  {
    cout<<endl<<"X:"<<x<<"  Y:"<<y;
  }

  friend point operator-(point p1,point p2);
};//End of class

point operator-(point p1,point p2)
{
  point temp;
  temp.x=p1.x-p2.x;
  temp.y=p1.y-p2.y;
  return temp;
}

int main()
{
  point p1,p2,p3;
  clrscr();
  p1.accept();
  p2.accept();
  cout<<endl<<"Point P1 is: ";
  p1.display();
  cout<<endl<<"Point P2 is: ";
  p2.display();
  p3=p1-p2;
  cout<<endl<<"Distance between point P1 and P2: ";
  p3.display();
  getch();
  return 0;
}

No comments:

Post a Comment