Commit 9d049324 authored by Hans Buchmann's avatar Hans Buchmann

with c++ reference

parent 6a686933
......@@ -15,7 +15,7 @@ class Point
//non default constructor
Point(const Point& other); //copy constructor
~Point(); //destructor no parameters
double distance(Point* other); //other is a pointer
double distance(Point& other); //other is a c++ reference
static double distance(Point p,Point q);
private:
......@@ -50,14 +50,16 @@ double Point::distance(Point p,Point q) //is not a member of Point
// |private
}
double Point::distance(Point* other) //is a member of Point
double Point::distance(Point& other) //is a member of Point
// |----------c++ reference
{
return std::hypot(xc-(*other).xc, //deref with *
yc- other->yc); //deref with ->
/* other notation
return std::hypot(xc-other->xc,
yc-other->yc);
*/
/* hack
other.xc=0;
other.yc=0;
*/
return std::hypot(xc- other.xc,
yc- other.yc);
}
//---------------------------------------------- Line
......@@ -90,6 +92,14 @@ d=5
destructor of Point 6,8 q
destructor of Point 3,4 p
with other as c++ reference
constructor of Point 3,4
constructor of Point 6,8
d=5
destructor of Point 6,8
destructor of Point 3,4
*/
......@@ -100,7 +110,7 @@ int main(int argc,char**args)
/*
distance (3,4) -> (6,8) = sqrt(3*3+4*4)=sqrt(25)=5
*/
auto d1=p.distance(&q); //address of q *no* copy
auto d1=p.distance(q); //address of q c++ reference no copy
std::cout<<"d="<<d1<<"\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