Class is just a wrapper, the size of class is equal to the sum of the size of all its members in common. An exception is that empty class should not have the size of zero.
For your questions:
Is the memory that is allocated to class instances not at all
dependent on which member variables are initialized? Will I save memory by creating a new class with less variables if the instances don't use those variable?
No, the compiler will not know whether you use those variables or not. It just a garbage value if you do not use them, but it still a value.
Will I save memory by having two classes as above, rather than having
one class with all three different properties?
No, class Dog is inherited from class Animal, thus it will have what Animal has, in this case are NumberOfFeet
and AverageWeight
and it owns members, in this case is Breed
. So the memory is not reduce just because you do not use those two NumberOfFeet
and AverageWeight
That is when your two classes depend on the other (Dog
depends on Animal
). If they are separated (no inheritance), the answer should be yes (since Dog
does not contain NumberOfFeet
and AverageWeight
).
Edited: not all the time the size of class equal to sum of its member variables.