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

Dani AI

Generated

A short, practical follow-up to what asked and what noted: the value you printed is an indeterminate value from memory — reading it is undefined behavior. Private/public or being a class member doesn’t change the rule: built-in types (like int) inside an object are not automatically given a sensible value unless you initialize them.

To avoid this, initialize the member every time. Two common, safe patterns:

class Number {
    int num;
public:
    Number() : num(0) {}
    int getnum() const { return num; }
};

or, with in-class initialization (C++11+):

class Number {
    int num = 0;
public:
    int getnum() const { return num; }
};

When creating objects, prefer value-initialization if you want zeros:

Number test{};        // value-initializes members
Number test2 = Number(); // also value-initializes

Also note differences for new and globals: new int; leaves the int uninitialized, while new int(); yields zero-initialized storage; objects with static storage duration (globals, static) are zero-initialized by default.

If you repeatedly see the same odd large/negative numbers in debug builds, that often comes from compiler/debugger memory-fill patterns (used to make uninitialized reads obvious). To catch these bugs, use tools such as Valgrind or AddressSanitizer; they report reads of uninitialized memory.

For the formal rules and more cases (default- vs value- vs zero-initialization), see the C++ initialization reference: C++ initialization (cppreference).

Practical tips: always initialize members, mark getters const, and consider std::optional<int> when “no value” needs to be represented explicitly.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.