views:

98

answers:

3

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

+2  A: 

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.

GMan
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?
hype
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.)
Jon Reid
@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.
GMan
+3  A: 

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) {}
R Samuel Klatchko
A: 

As opposed to Java or C#, where memory allocated for a newly created objects ALWAYS set to zero, this NOT happends in C++. There are several rules that describe when object initialization is guaranteed to take place and when it isn't.

Consider folowing example:

class Base
{
private:
    int i;
    char ch;
    std::string str;
public:
    Base()
      : i(0) //built-in fields remains unitialized. We should initialize it manually
      , ch('\0') //another built-in field
      //, str() //this call is redundant due automatic default constructors calls for all user-defined types
    {}
    void showdata()
    {
        cout<<"Int:"<<i<<endl; //valid only after manual initialization
        cout<<"Char:"<<ch<<endl; //valid only after manual initialization
        cout<<"String:"<<str<<endl; //always valid 
    }
    //int pub_data ;
} ;

You should remember, that ALL buit-in fields you should initialized manually in class constructor.

P.S. The fact, that in the first case your code works - is pure accident.

Sergey Teplyakov