Commit 5a6815e6 authored by Hans Buchmann's avatar Hans Buchmann

methods

 - static distance with two parameters points
 - not static with on parameter
parent 41a6211e
......@@ -14,6 +14,9 @@ class Point
Point(double x,double y); //constructor x/y position in the plane
//non default constructor
~Point(); //destructor no parameters
double distance(Point other);
static double distance(Point p,Point q);
private:
double xc; //x-coordinate
double yc; //y-coordinate
......@@ -34,68 +37,29 @@ Point::~Point()
std::cout<<"destructor of Point "<<xc<<","<<yc<<"\n";
}
//---------------------------------------------- Line
/*
C
double Point::distance(Point p,Point q) //is not a member of Point
{
Point p; //definition without initialization
point_birth(&p,5,5);
..
..
..
..
point_death(&p);
return std::hypot(q.xc-p.xc,q.yc-p.yc);
// |private
}
RAII Resource Allocation Is Initialization
C++
life cycle instance
{
Point p(5,5); //instance of Point birth
..
..
..
..
<-------------- death of p
}
p not available anymore
typical example
{
std::ofstream output("fileName");
output<<"hello\n";
...
if (...)
{
return; //death of output destructor called
}
output<<1234;
//death of output destructor called
}
Java
double Point::distance(Point other) //is a member of Point
{
FileOutputStream output=new FileOutputStream("fileName");
..
output.println("Hello");
...
if (...)
{
output.close(); //must be done
return;
}
output.close(); //must be done
return std::hypot(xc-other.xc,
yc-other.yc);
}
*/
//---------------------------------------------- Line
int main(int argc,char**args)
{
Point p(1,1); //constructor
{
Point q(2,2); //constructor
{
Point r(3,3);//constructor
}//destructor r
}//destructor q
Point p(3,4);
Point q(6,8);
/*
distance (3,4) -> (6,8) = sqrt(3*3+4*4)=sqrt(25)=5
*/
auto d0=Point::distance(p,q); //with static
auto d1=p.distance(q); //non static
std::cout<<"d="<<d1<<"\n";
return 0;
} //destructor p
}
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