This section of code:
{Line l(5);}
This line defines the variable l of type Line and calls its constructor. Braces, sometimes called wavy brackets, are used to create a separate block of code. A block of code defines the scope of the variables that are declared within it. If you do not manually call the destructor of a class then its destructor is called automatically when it goes out of scope. Including the wavy brackets around this line causes its destructor to be called when the "}" is reached.
The code calls the following method in the line class:
Line::Line(int len)
{
cout<<"Simple constructor"<<endl;
p=new int;
*p=len;
}
This method will point p to a new int storage then assign 5 to the contents of p.
The 1,2,3 output in the console window are from the cout statements and are just there to show where the program is in its execution. It would be more informative to output descriptive text instead of just a number.
Some of the more confusing things for people new to C++ are the & and * operators. The copy constructor is designed to make a copy of an object of the Line class to another instance of a Line object. The parameters of the Line copy constructor, "(const Line &obj)", says that the method takes the address of an instance of the Line class. An instance of a class is also known as an object so the parameter is called "obj" which is short for object.
p is initialized to hold an integer by the following line:
p=new int;
Let's analyze the next line
*p=*obj.p;
"*obj" means the contents of obj. obj holds an address pointing to a Line object; the contents of that address is a Line object.
".p" means the member "p" of the object before the "." therefore "*obj.p" means the member p of the object whose address was passed to the method.
Then that value of p is assigned to the contents of the local p via "*p=".