Question OOP-POLY-0007
EasyWhat is the output of this code?
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() { cout << "Shape"; }
};
class Circle : public Shape {
public:
void draw() override { cout << "Circle"; }
};
int main() {
Shape* s = new Circle();
s->draw();
delete s;
return 0;
}
- A.
Shape
- B.
Circle
- C.
Show answer and explanation
Answer: B
draw() is virtual, so calling it through a Shape* that points to a Circle runs Circle::draw.