Commit bb4bdcaf authored by Hans Buchmann's avatar Hans Buchmann

instance method distance

parent ccba646e
......@@ -13,6 +13,7 @@ class Point
Point(double x,double y); //non-default constructor no return value
~Point(); //destructor never with parameters no return value
static double distance(Point p,Point q);
double distance(Point other); //non static instance method
private:
double xc; //x-coordinate
double yc; //y-coordinate
......@@ -38,10 +39,15 @@ double Point::distance(Point p,Point q)
// |----|----|----|----------------private
}
double Point::distance(Point other)
{
return std::hypot(other.xc - xc,
other.yc - yc);
}
/*
constructor Point 5,7
constructor Point 8,11
destructor Point 5,7
destructor Point 8,11
d=5
destructor Point 8,11
......@@ -53,7 +59,9 @@ int main(int argc,char** args)
Point p(5, 7);
Point q(8,11);
//distance p -> q sqrt(3*3 + 4*4)=sqrt(25)=5
auto d=Point::distance(p,q);
//auto d=Point::distance(p,q);
auto d=p.distance(q);
// |----------|------------- instance
std::cout<<"d="<<d<<"\n";
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment