Computer Science, asked by katharpranali98, 2 months ago

#include<iostream>
using namespace std;

class A {
public:
int data;
virtual void fun()
{
}
virtual void fun1()
{
}
virtual void fun2()
{
}
};
int main()
{
A a;
cout << sizeof(a) << end1;
return 0;
}​

Answers

Answered by dreamrob
0

Output:

16

Explanation:

4 bytes → int data

8 bytes → virtual function()     (size of VTPR)

4 bytes → padding     (compiler adds for data member alignment)

Size of class = non-static data + padding

Size of class = 4 bytes + 4 bytes + 8 bytes = 16 bytes.

So, the output is 16

Practice question:

#include<iostream>

using namespace std;

class A  

{

private:

    static int i;

    int a;

    char b;

};

int main()

{

A a;

cout<<sizeof(a);

return 0;

}

Similar questions