Solved

Please clarify confusing construction example!

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

622 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