Gitlab will be upgraded to new release 13.9.6 on Monday 19th April at 22:00 until 23:00.

Commit 6a686933 authored by Hans Buchmann's avatar Hans Buchmann

other as pointer

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