Class data default initialization

13,914

Solution 1

Neither int's nor char's are automatically initialized to 0. The fact that it happened is just luck.

You need to add a constructor that does the initialization:

Base() : i(0), ch(0) {}

Solution 2

None. You're just getting "lucky". Fundamental types remain uninitialized, so your i and ch, as the program stands, could very well not always be 0.

It just so happens adding that public member "messes it up". To correct your class, initialize the members in the initialization list of the constructor:

class Base
{
private:
    int i;
    char ch;
public:
    Base(void) :
    i(0), ch(0) //, pub_data(0)
    {}

    void showdata()
    {
        cout<<"Int:"<<i<<endl;
        cout<<"Char:"<<ch<<endl;
    }
    //int pub_data ;
} ;

Now when a Base gets constructed i, ch, and (when uncommented) pub_data will be properly initialized to meaningful values.

Share:
13,914
mukeshkumar
Author by

mukeshkumar

I'm a software professional having close to 7 years experience in telecommunications software industry. Have a keen interest in C++ and like to read about it a lot In free time, I watch English movies, play some strategy games.

Updated on June 07, 2022

Comments

  • mukeshkumar
    mukeshkumar almost 2 years

    I have following code:

    #include <iostream>
    using namespace std;
    
    class Base
    {
    private:
        int i;
        char ch;
    public:
        void showdata()
        {
            cout<<"Int:"<<i<<endl;
            cout<<"Char:"<<ch<<endl;
        }
        //int pub_data ;
    } ;
    
    int main()
    {
        Base ob;
        ob.showdata() ;
        //cout<<"Public Data:"<<ob.pub_data<<endl;
        return 0;
    }
    

    This program compiles and runs fine. The output shows that i is initialized with 0 and ch is initialized with '\0'.
    If you notice i have commented out 2 statements in this program. First the declaration of public data pub_data and second the line inside main printing this public data.
    Now here the problem is, if i uncomment these two lines, the data members of class i.e. i, ch, pub_data do not seem to be initialized and when printed, they display junk values.
    So my question is what difference public data makes here?
    I'm using g++ 3.4.6

  • mukeshkumar
    mukeshkumar over 14 years
    For the user defined types, the default constructor is called. Going by that, why shouldn't the fundamental types be default initialized? Any specific reason?
  • Jon Reid
    Jon Reid over 14 years
    C++ was not created in a vacuum, but was designed to be an extension of C. Therefore it does not initialize the fundamental types. (Remember, a class is just a struct.)
  • GManNickG
    GManNickG over 14 years
    @hype: They aren't for historical reasons, as Jon points out. I don't know the exact reason, but it probably had to do with speed. In "classic" C, all variables were declared at the top of the function. So now imagine we left the function halfway through: we would have wasted time initializing the variables. Compilers nowadays wouldn't waste time with those things, being much better, but this was then, and it stuck around. I personally find it annoying but that's how it is.
  • Kristupas A.
    Kristupas A. about 7 years
    Calling constructor does not initialise memory to 0: Base ob(); It still requires that memory is explicitly initialised inside the constructor of the class.