LearnFrenzy
Sign in Take a free test

Abstract Classes and Interfaces · Predict the Output · Question 6

Size of a class implementing two interfaces Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>

struct I1 { virtual void a() = 0; virtual ~I1() = default; };
struct I2 { virtual void b() = 0; virtual ~I2() = default; };
struct One : I1 { void a() override {} };
struct Two : I1, I2 { void a() override {} void b() override {} };

int main() {
    std::cout << sizeof(I1) << ' ' << sizeof(One) << ' ' << sizeof(Two) << '\n';
    return 0;
}
  • A 8 8 16 Correct
  • B 8 8 8
  • C 0 8 16
  • D 8 16 24
Correct Answer: Option A

Each polymorphic base subobject needs its own vptr, so implementing two interfaces costs two pointers.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links