Reputation: 843
I'm new to programming, and I'm currently learning C++. I came across this in the tutorials I am using. My question: How can the variable 'name' be used in the getName and setName functions before it is even declared which happens later under private? Isn't C++ a top to down sequential language?enter image description here
Upvotes: 1
Views: 102
Reputation: 35408
It works, because the C++ compiler is clever and initially loads the entire class declaration (together with any definitions it might have) "separates" the class members, builds up internal structures about the class and its content, and at a later stage the class methods are being compiled, when the entire class structure is already known. For example this is also how the parent class's methods and variables are accessible ... without full knowledge of the class and its entire hierarchy this would not work.
Since you are just beginning to learn C++, I'm not going to bore you with compiler internals, however a good book on this subject is https://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools or "Modern Compiler Implementation in C" (https://www.cs.princeton.edu/~appel/modern/c/)
Upvotes: 4