2

This may be very simple question ,but I cannot find the reason of this ,so at first I want to say that I am sorry.

class A{

 public:

    printA(void){

        cout << "\nA is the first letter of the alphabet";
    }

 private:

    double x;
};



class B : public  A{

 public:

    printB(void){

        cout << "\nB is the second letter of the alphabet";
        cout << "\nSize of y = " << sizeof(y);
    }

 private:

    float y;
};


int main(void){


   A firstObj;

   B secondObj;

   cout << "\nSize of firstObj = " << sizeof(firstObj);

   secondObj.printB();

   cout << "\nSize of secondObj = " << sizeof(secondObj);


   return 0;
}

I expected that size of obj1 is 8 because of double data member and my expectation is validated by the function main().

However, size of obj2 is equal to 16. Actually, I hope that It become 12 because of 8 + 4.

Why is size of obj2 equal to 16 ?

gktg1414
  • 107
  • 2
  • 9
  • I found the answer by the title "Why isn't sizeof for a struct equal to the sum of sizeof of each member? " . Thank you so much. – gktg1414 Mar 30 '17 at 11:50

0 Answers0