Solved

Please clarify confusing construction example!

Posted on 2013-06-10
2
199 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
2 Comments
 
LVL 19

Accepted Solution

by:
Peter Hutchison earned 500 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 clear a vector as well as how to detect empty vectors in C++.

770 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