LearnFrenzy
Sign in Take a free test

Abstract Classes and Interfaces · Find the Error · Question 6

Error: missing const leaves the class abstract Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

struct Shape {
    virtual double area() const = 0;
    virtual ~Shape() = default;
};

struct Circle : Shape {
    double r = 1;
    double area() { return 3.14 * r * r; }
};

int main() {
    Circle c;
    std::cout << c.area() << '\n';
    return 0;
}
  • A r must be private
  • B Circle::area() lacks const, so it does not override Shape::area() const; Circle is still abstract and cannot be created Correct
  • C area must return float
  • D Shape needs a constructor
Correct Answer: Option B

Writing override on Circle::area would have pointed straight at the missing const.

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