Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Please clarify confusing construction example!

Posted on 2013-06-10
2
Medium Priority
?
209 Views
Last Modified: 2013-06-10
Following is the link that I'm using to learn constructor:

http://www.exforsys.com/tutorials/c-plus-plus/class-constructors-and-destructors-in-c.html

The 29 line example seems to be confusing.  Please explain each line to code.  The output doesn't seem to match the source code.   Thank you!
0
Comment
Question by:naseeam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 20

Accepted Solution

by:
Peter Hutchison earned 2000 total points
ID: 39236294
Line 2: This defines the scope for the program. Just in case there are other similar classes and contructors. So this is a standard (std) namespace. Optional but here fore completion.
Line 3. Defines the new class called Exforsys which contains variables and methods (aka subprograms and functions).
Line 5 and 6. Declares an integer variable called 'a' which is private, which means it is not accessible outside the class.
Line 7. Declares following methods are public and accessible from outside the class, e.g. main.
Line 8-9. Declares an empty constructor method. If no arguments are given, then nothing is performed.
Line 10-13. Declares another constructor with an integer argument (w). The argument  assigns the value from w to the class' 'a' value for that instance.
Lines 14-18. Declares another constructor which has the address of type class Exforsys. The value from that instance's a value is passed to this instance's a value (copies it basically).
Lines 19-22. The funtion result prints out the instances' value of 'a'.
Line 24. Main program starts here
Line 26. Creates an instance of class Exforsys called 'e1' and pass a value of 50 (this will use the second constructor as the number and type of arguments matches that).
Line 27. Creates an instance of class Exforsys called e1 and passes the address of instance e1 to it. This will use the third contructor as the argument matches that. The value from e1's instance of 'a' will be 50 so that is copied to e3's instance of 'a' and makes that 50.
Line 28. Prints ne3= 50 as that is result of the function of called result of instance e3.

Hope that explains it.
0
 
LVL 1

Author Closing Comment

by:naseeam
ID: 39236388
Thank you!  Yes, it helped a lot.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question