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

exercise 1.1 remarks Constructor/Destructor

parent a7a921f4
......@@ -36,17 +36,65 @@ Point::~Point()
//---------------------------------------------- Line
/*
C
{
Point p; //definition without initialization
point_birth(&p,5,5);
..
..
..
..
point_death(&p);
}
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
{
FileOutputStream output=new FileOutputStream("fileName");
..
output.println("Hello");
...
if (...)
{
output.close(); //must be done
return;
}
output.close(); //must be done
}
*/
int main(int argc,char**args)
{
Point p(3.1,4.7); //definition and initialization
// | |----------- y-coordinate
// |--------------- x-coordinate
// p is an instance of the class Point
Point q(7.3,2.4); //definition and initialization
// | |----------- y-coordinate
// |--------------- x-coordinate
// q is an instance of the class Point
Point p(1,1); //constructor
Point q(2,2); //constructor
Point r(3,3);//constructor
return 0;
//destructor of q
//destructor of p
//destructor r
//destructor q
//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