Point Class Example C++

Define a point class, where each object represents point in Cartesian coordinates (x, y). Define objects of this class and calculate the distance between the 2 points through a friend function

#include<iostream>
#include<math.h>
using namespace std;
class point
{
float x;
float y;
public:
point()
{
x=0;
y=0;
}
point(int z)
{
x=y=z;
}
point(int a,int b)
{
x=a;
y=b;
}

point(const point &c)
{
x=c.x;
y=c.y;
}


void get_point()
{
cin>>x>>y;
}

void print_point()
{
cout<<x<<y;
cout<<"\n";
}
friend float dist_point(point p1,point p2);
};

float dist_point(point p1,point p2)
{
float result;
result=sqrt(((p2.x-p1.x)*(p2.x-p1.x))+((p2.y-p1.y)*(p2.y-p1.y)));
return result;
}
int main()
{
point c,c1,c2,c3;
float distance;
c.print_point();
c1.print_point();
c2.print_point();
c3.print_point();
c.get_point();
c1.get_point();
c2.get_point();
c3.get_point();
c.print_point();
c1.print_point();
c2.print_point();
c3.print_point();
distance=dist_point(c2,c3);
cout<<distance;
return 0;
}



Comments

Popular posts from this blog

bb.utils.contains yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig

PR, PN and PV Variable in Yocto