Solved

Please clarify confusing construction example!

Posted on 2013-06-10
2
193 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 18

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
 

Author Closing Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now