Solved

Please clarify confusing construction example!

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

821 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