Commit ccba646e authored by Hans Buchmann's avatar Hans Buchmann

static distance

parent 311962b3
......@@ -12,6 +12,7 @@ class Point
public:
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);
private:
double xc; //x-coordinate
double yc; //y-coordinate
......@@ -31,23 +32,28 @@ Point::~Point()
std::cout<<"destructor Point "<<xc<<","<<yc<<"\n";
}
/*
geo2d-bb.cc:36:22: error: double Point::xc is private within this context
*/
// distance not in class Point
double distance(Point p,Point q)
double Point::distance(Point p,Point q)
{
return std::hypot(q.xc-p.xc,q.yc-p.yc); //<---- error
// |----|----|----|----------------private
}
/*
constructor Point 5,7
constructor Point 8,11
destructor Point 5,7
destructor Point 8,11
d=5
destructor Point 8,11
destructor Point 5,7
*/
//--------------------------- use case
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=distance(p,q);
auto d=Point::distance(p,q);
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