LearnFrenzy
Sign in Take a free test

Abstract Classes and Interfaces · Predict the Output · Question 5

std::is_abstract through a class hierarchy Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

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

main.cpp
#include <iostream>
#include <type_traits>

struct A { virtual void f() = 0; virtual void g() = 0; virtual ~A() = default; };
struct B : A { void f() override {} };
struct C : B { void g() override {} };
struct D : C { void f() override = 0; };   // re-declared pure!
struct E { virtual void h() {} };

int main() {
    std::cout << std::is_abstract_v<A> << std::is_abstract_v<B> << std::is_abstract_v<C>
              << std::is_abstract_v<D> << std::is_abstract_v<E> << '\n';
    return 0;
}
  • A 11010 Correct
  • B 11000
  • C 10010
  • D 11110
Correct Answer: Option A

B misses g(); C is concrete; D overrides f() as pure again, making it abstract; E has virtual functions but none pure.

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