LearnFrenzy
Sign in Take a free test

Encapsulation · Find the Error · Question 8

Error: calling a base member hidden by private inheritance Answer, explanation & discussion

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

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

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

class Stack : private std::vector<int> {
public:
    void push(int x) { push_back(x); }
    using std::vector<int>::size;
};

int main() {
    Stack s;
    s.push(1);
    s.push(2);
    s.insert(s.begin(), 0);
    std::cout << s.size() << '\n';
    return 0;
}
  • A push must be virtual
  • B With private inheritance, std::vector's members are private in Stack; only size was re-exposed with using, so insert and begin are inaccessible Correct
  • C Stack must call the vector constructor
  • D size cannot be re-exposed
Correct Answer: Option B

That is the point: a stack must not allow insertion in the middle. Only the deliberately exposed members are public.

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