vendredi 16 novembre 2018

What is the value of data members of an object initialized through member initializer list. Specifically What would be values of x and y?

When i run this program the output is "In A" and "In Show". Why cout statements after that are not printing ? I am initializing class B object using member initializer list so the value of of x and y should ideally be 0. Why is this strange behavior ?

class B
    {
        int x,y;
        public:
            B(int a)
            {

            }
            void show() //what would this print ?
            {
                cout<<"In Show";
                cout<<"x = "<<x<<endl;
                cout<<"y ="<<y;
            }
    };
    class A
    {
        B *b;
        public:
            A():b(0)
            {
                cout<<"In A"; 
                b->show();
            }


    };

    int main()
    {
        A a;
        return 0;
    }

Aucun commentaire:

Enregistrer un commentaire