第21188题 单选
运行给定的C++多态示例代码,屏幕上的输出结果是什么?

运行下列C++代码,屏幕上的输出结果为:

#include <iostream>
using namespace std;

class shape {
protected:
  int width, height;
public:
  shape(int a = 0, int b = 0) {
    width = a;
    height = b;
  }
  virtual int area() {
    cout << "parent class area: " << endl;
    return 0;
  }
};

class rectangle: public shape {
public:
  rectangle(int a = 0, int b = 0) : shape(a, b) { }

  int area () {
    cout << "rectangle area: ";
    return (width * height);
  }
};

class triangle: public shape {
public:
  triangle(int a = 0, int b = 0) : shape(a, b) { }

  int area () {
    cout << "triangle area: ";
    return (width * height / 2);
  }
};

int main() {
  shape *pshape;
  rectangle rec(10, 7);
  triangle tri(10, 5);

  pshape = &rec;
  pshape->area();

  pshape = &tri;
  pshape->area();
  return 0;
}
A

rectangle area: triangle area:

B

parent class area: parent class area:

C

运行时报错

D

编译时报错