C++ Class Members and Objects Explained

To better understand the reasons why we could not initialize a class member variable during the declaration phase, it is essential for us to first understand how and why classes are designed the way they are. To put it in the simplest of terms, classes are similar to that of a project blueprint, and therefore, it does not naturally reserve or allocate any memory spaces on our compiler or computer to store any variables. Furthermore, in programming, the term initialization simply means to save or record a particular set of data into its respective and predetermined memory location. Now, if you are still confused, that’s totally fine. Let us place this concept into a real-world analogy. The processes are almost identical to what happens inside a typical car production plant. Classes, in this case, are simply the blueprints of the various car parts that the engineers designed for a specific model of vehicle. Hence, multiple blueprints are necessary to produce a fully functioning and market-ready automobile, or in this case, a complete, fully executable program. On the other hand, variables and functions are the specifications of this car model. However, at this stage, everything is still on paper. To complete this assembling project and make it into a market-ready product, we would need to give this paper blueprint to the people on the assembly line, and this process of handing over the blueprints to production staff is called object handling. Therefore, in order for us to assign or allocate memory space for class variables, it would have to be done inside of an object or, in this analogy, on the production line. Thus, to summarize this analogy, we cannot just simply print the specifications of the parts onto a paper and call it a finished product. Instead, we will have to consult with our production workers to help us with that, and this is the piece that we are missing, and these production staffs are the “objects” that ultimately help us achieve this tedious assembling project. On a side note, though, exception handling and memory management topics are also applicable to this car production plant analogy, and I will show you how that works when that time comes.

Leave a comment