LearnFrenzy
Sign in Take a free test

Abstract Classes and Interfaces · Predict the Output · Question 3

A pure virtual destructor with a definition Answer, explanation & discussion

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

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

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

struct Base {
    virtual ~Base() = 0;
};
Base::~Base() { std::cout << "B"; }

struct Mid : Base { ~Mid() override { std::cout << "M"; } };
struct Leaf : Mid { ~Leaf() override { std::cout << "L"; } };

int main() {
    std::unique_ptr<Base> p = std::make_unique<Leaf>();
    p.reset();
    std::cout << '\n';
    return 0;
}
  • A LMB Correct
  • B BML
  • C B
  • D L
Correct Answer: Option A

The pure virtual destructor makes Base abstract but still runs last – which is why it must be defined.

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