u

Thursday, 5 January 2012

C++ Program to define a class point and implement operations


C++ Program to define a class point and implement operations.

 #include<iostream.h>
#include<math.h>
#include<conio.h>
class Point{
public:
     Point();
void setPoint(float,float);
void printPoint();
float getx();
float gety();

private:
   float x;
   float y;

      };
  Point::Point(){x=0.0;y=0.0;}
void Point::setPoint(float a,float b)
  {x=a;y=b;}
void Point::printPoint()
     {
     cout<<'('<<x<<","<<y<<')';
     }
float Point::getx(){return x;}
float Point::gety(){return y ;}

  void main()
{
clrscr();
Point p1,p2;
char ch;

cout<<"Initially,the points are :";
  p1.printPoint();

cout<<",";

  p2.printPoint();
cout<<endl;
p1.setPoint(3.5,4.5);
p2.setPoint(7.5,9.5);

  cout<<"After setting the points are:";

p1.printPoint();
cout<<",";
p2.printPoint();
cout<<endl;
  float d=0.0;
d=sqrt((p2.getx()-p1.getx())*(p2.getx()-p1.getx())+(p2.gety()-p1.gety())*(p2.gety()-p1.gety()));
cout<<"d="<<d<<endl;

cout<<"Enter any key to continue";
getch();
}

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More