Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If someone know,let me know ,please :) Thanks in advance :)
Can someone understand that what 1, 2, 3 is? And what {line l(5); } is in this program? One more question that : *p=*obj.p what does this mean? and * means dereference?


C++
#include<iostream>
using namespace std;
class Line{
public:
	int getLength(void);
	Line(int len);//constructor
	Line(const Line&obj);//copy constructor
	~Line();//destructor
private:
	int *p;
};

Line::Line(int len)
{
	cout<<"Simple constructor"<<endl;
	p=new int;
	*p=len;
}
Line::Line(const Line&obj)
{
	cout<<"Copy constructor"<<endl;
	p=new int;
	*p=*obj.p;//object is type of line
}
Line::~Line()
{
	cout<<"Destructor"<<endl;
	delete p;
}
Line::getLength(void)
{
	return *p;//dereference p(int p,not adress p)
}
void show(Line obj)
{
	cout<<"2"<<endl;
	cout<<obj.getLength()<<endl;
}


int main()
{
	{Line l(5);}
	Line line(10);

	cout<<"1"<<endl;
	//cout<<line.getLength()<<endl;
	show(line);
	cout<<"3"<<endl;
	return 0;
}


What I have tried:

I read about copy constructor much,i understood it,but some things that i could not understand in this program,i asked in the question.
Posted
Updated 2-Sep-16 6:55am
v7
Comments
Maciej Los 29-Aug-16 17:29pm    
Is it a joke?
Member 12702056 29-Aug-16 17:32pm    
what did you mean? no,of course. i am beginner in c++,i read many things about work of copy constructor,but some little things that i wrote,i can't understand and i want to know with helping.
Member 12702056 29-Aug-16 17:34pm    
if you mean about uni.lesson,it was by mistake,i wrote it here wrongly .it was my message to my friend.i saw it,i edit it.it was by mistake.
Philippe Mori 2-Sep-16 16:59pm    
Obviously, you should read books and learn to use a debugger.
Philippe Mori 2-Sep-16 17:04pm    
Obviously, 1, 2, 3 are there so that you can see on the output in which order instructions are executed.

Seeing the program executing may help to understand what it is doing.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Advice: Choose which language is this program, no need to list every single possible and impossible language. The program don't compile in C.
 
Share this answer
 
Comments
Member 12702056 29-Aug-16 17:46pm    
Thanks a lot for your answer :),this program is in c++.Ok,i got it know.i'll do .you mean about tag?! I did not know it,but I changed it.
This section of code:
C++
{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:
C++
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:
C++
p=new int;

Let's analyze the next line
C++
*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=".
 
Share this answer
 
v3
Comments
Member 12702056 2-Sep-16 13:08pm    
Thanks a lot :) and what about this question?
*p=*obj.p what does this mean? and * means dereference?if you know answer,please let me know too :)
BillW33 2-Sep-16 14:31pm    
I have updated my solution with additional explanation.
Member 12702056 2-Sep-16 16:02pm    
Thanks again ,it was clear explanation :)
Member 12702056 2-Sep-16 16:07pm    
but one more thing i want to ask you.If i don't use {} these,result will be different.but i don't know "why?"
with {}
result is:
Simple constructor
Destructor
Simple constructor
1
Copy constructor
2
10
Destructor
3
Destructor


without {}
result is like this:
Simple constructor
Simple constructor
1
Copy constructor
2
10
Destructor
3
Destructor
Destructor
Philippe Mori 2-Sep-16 17:03pm    
By adding extra { } it change the scope of variable l and thus cause its destructor to be called earlier. Obviously, it should be very easy to deduce that from the output... or using a debugger.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900