LearnFrenzy
Sign in Take a free test

Abstract Classes and Interfaces · Predict the Output · Question 11

An intermediate abstract class with common code Answer, explanation & discussion

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

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

main.cpp
#include <iostream>

struct Account {
    virtual double rate() const = 0;
    virtual double fee() const = 0;
    virtual ~Account() = default;
};
struct BasicAccount : Account {              // still abstract: implements fee() only
    double fee() const override { return 100; }
};
struct Savings : BasicAccount { double rate() const override { return 0.04; } };
struct Salary  : BasicAccount {
    double rate() const override { return 0.035; }
    double fee() const override { return 0; }
};

int main() {
    Savings s;
    Salary p;
    const Account* a[] = {&s, &p};
    for (auto* x : a) std::cout << 10000 * x->rate() - x->fee() << ' ';
    std::cout << '\n';
    return 0;
}
  • A 300 350 Correct
  • B 400 350
  • C 300 250
  • D 400 250
Correct Answer: Option A

Savings inherits BasicAccount's fee (100): 400 - 100 = 300. Salary overrides both: 350 - 0 = 350.

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