| What happens if getAage is called without setAge or the other constructor being done first?|
¿how is that possible?
I found this, the reply about half way down by nobar
seemed the best answer for me.
When the object is created like this:
That calls the implicit default constructor, which doesn't initialise any member variables. So the member variable values are not defined.
However, If the object is created like this:
Apparently the member variables are
Even better advice:
Scott Meyers, Effective C++, item 4 wrote:
|The best way to deal with this seemingly indeterminate state of affairs is to always initialise your objects before you use them. |
Scott also recommends always providing your own copy constructor, copy assignment operator, and destructor - even if they are empty. The reasons why are a whole new kettle of krawldads.
I would strongly recommend Scott's book, but it would be quite tough for a beginner, because one needs to understand all the different aspects of C++ first, otherwise much of it won't make any sense.