Commit f75afecc authored by Hans Buchmann's avatar Hans Buchmann

c++ reference

parent 9db5a3f6
......@@ -14,8 +14,8 @@ class Point
Point(const Point& other); //copy constructor
~Point(); //destructor never with parameters no return value
static double distance(Point p,Point q);
double distance(Point* other); //non static instance method
// |------------- pointer
double distance(Point& other); //non static instance method
// |------------- c++ reference
private:
double xc; //x-coordinate
double yc; //y-coordinate
......@@ -49,24 +49,22 @@ double Point::distance(Point p,Point q)
// |----|----|----|----------------private
}
double Point::distance(Point* other)
double Point::distance(Point& other)
{
/* hack
other->xc=0;
other->yc=0;
other.xc=0; like by value but in fact by reference
other.yc=0;
*/
return std::hypot((*other).xc - xc,
other->yc - yc);
/* other notation
(*other).yc <---> other->yc
*/
return std::hypot(other.xc - xc,
other.yc - yc);
}
/*
no copy constructor
constructor Point 5,7
constructor Point 8,11
constructor Point 8,11
d=5
destructor Point 8,11
destructor Point 5,7
......@@ -77,8 +75,7 @@ int main(int argc,char** args)
{
Point p(5, 7);
Point q(8,11);
auto d=p.distance(&q); //other = pointer to q
//|--------------- address of
auto d=p.distance(q); //other = c++ reference to 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