Friday 19 December 2014

Q.Define a point class for 2-D points(x,y).Include a default constructor , a copy constructor ,
a negate() function to transform the point into its negative , a norm() function to return the
point's distance from the point's distance from the origin(0,0),and a print() function besides
the functions to input and display the co-ordinates of the point.

#include<iostream.h>
#include<conio.h>
#include<math.h>
class Point
{
     private:
     int x,y;
     public:
     Point()
     {
    x=0;
    y=0;
     }
     Point(Point&Q)
     {
    x=Q.x;
    y=Q.y;
     }
     void negate()
     {
    x=-1*x;
    y=-1*y;
     }
     float norm()
     {
    float num;
    num=sqrt(x*x+y*y);
    return num;
     }
     void getinfo()
     {
    cout<<"Enter co-ordinates of the point:";
    cin>>x>>y;
     }
     void print()
     {
    cout<<"("<<x<<","<<y<<")";
     }
};
void main()
{
     clrscr();
     Point P;
     P.getinfo();
     Point(P);
     cout<<endl<<"Co-ordinate of the point:";
     P.print();
     float t=P.norm();
     cout<<endl<<"Distance from the origin:"<<t;
     P.negate();
     cout<<endl<<"The negative of the co-ordinates:";
     P.print();
     getch();
}

No comments:

Post a Comment