samedi 30 mars 2019

Are all the data members initialized to 0 or are they assigned random values by the constructor which is called automatically?

I tried to check what values are assigned to the data members when we do not call a constructor manually. I got 0 for both a and b, but I got 1 for c, so how are the data members initialized? Randomly or to 0? And if they are initialized to 0, why am I seeing 1 as the value for c?

#include<iostream>
using namespace std;

class Class
{
    public:
        int a,b,c;      
};

int main()
{
    Class obj;

    cout<<obj.a;
    cout<<"\n";
    cout<<obj.b;
    cout<<"\n";
    cout<<obj.c;

    return 0;
}

The output is 0 0 1

But I expected 0 0 0

Aucun commentaire:

Enregistrer un commentaire