Question 10
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
#include <iostream>
class Base;
class Derived : public Base {
public:
void hello() const { std::cout << "hello\n"; }
};
class Base {
public:
int id = 1;
};
int main() {
Derived d;
d.hello();
return 0;
}
- A hello must be virtual
- B A base class must be completely defined before a class can derive from it; a forward declaration is not enough – define Base first Correct
- C Derived must be a struct
- D id must be private
Correct Answer: Option B
The compiler needs the base's size and members to lay out the derived class.
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!