When I define a variable in a class, every time I declare an instance of this class it creates, in memory, a new copy of that variable for the particular instance. I understand this, but does this hold for when all the member variables are private? For example:
class A {
int a, b, c;
};
A a;
When I create a new instance, are these private variables still allocated for a even though they can’t be used outside the class?
No. Memory allocation is an implementation detail. Consider the code:
There will probably be no memory allocation whatsoever, because C++ operates on an as-if model. Meaning, if the output is the same as the expected one, the compiler is free to do anything. Including optimizing out dead code.
Typically, though, yes, you can assume space is allocated for all members of an object.