Multiple choice

What will be the size (in bytes)of an object of class child in the code given above?


class super {
  int a;

 public:
  int b : protected : int c;
};
class parent : public super {
  float d;

 protected:
  char e;
};
class child : private parent {
  int f;

 public:
  float g;
};

Format! Style:

C++ online code formatter © 2014 by KrzaQ

Powered by vibe.d, the D language and clang-format

  1. 6

  2. 4

  3. 17

  4. 8

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The size of the object is the total size of all the variables declared in all the classes, irrespective of their access specifier and the inheritance visibility mode. Therefore, if you calculate the size of all the variables in the three classes with int as 2 bytes, float 4 bytes and char 1 byte, you will see there are 4 integers (4x2 = 8), 1 char (1x1=1) and 2 floats (4x2=8).