Say I have a class (say number) and declare a private variable (say int num) and have a public member function(say int getnum() ) that retrieves the private variable. When I in the main() without initializing the private variable int num (of course after declaring the class variable say test) call cout<<test.getnum() it gives me a very random number -for me negative 858993350. what is this number? Is it whatever was stored there before it was allocated as an int? thx

Say I have a class (say number) and declare a private variable (say int num) and have a public member function(say int getnum() ) that retrieves the private variable. When I in the main() without initializing the private variable int num (of course after declaring the class variable say test) call cout<<test.getnum() it gives me a very random number -for me negative 858993350. what is this number? Is it whatever was stored there before it was allocated as an int? thx

Most likely, yes, it is whatever was in that memory location before it was dedicated to that variable. Some compilers will automatically initialize the variable to 0 if you don't initialize it, but most won't do anything with it. The fact that it is a data member of a class and that it is private is most likely irrelevant. This holds true for any declared, but uninitialized variable. Its contents remains what they were before allocation until it is initialized.

This question has already been answered. Start a new discussion instead.