题库 C++/C语言题库 题目列表 运行下列代码,屏幕上输出( )。1 #include&nbs...
单选题

运行下列代码,屏幕上输出( )。

1 #include <iostream>
2 using namespace std;
3
4 class shape {
5 protected:
6  int width, height;
7 public:
8  shape(int a = 0, int b = 0) {
9   width = a;
10   height = b;
11  }
12  virtual int area() {
13   cout << "parent class area: " <<endl;
14   return 0;
15  }
16 }; 
17
18 class rectangle: public shape {
19 public:
20  rectangle(int a = 0, int b = 0) : shape(a, b) { }
21
22  int area () {
23   cout << "rectangle area: ";
24   return (width * height);
25  }
26 };
27
28 class triangle: public shape {
29 public:
30  triangle(int a = 0, int b = 0) : shape(a, b) { }
31
32  int area () {
33   cout << "triangle area: ";
34   return (width * height / 2);
35  }
36 };
37
38 int main() {
39  shape *pshape;
40  rectangle rec(10, 7);
41  triangle tri(10, 5);
42
43  pshape = &rec;
44  pshape->area();
45
46  pshape = &tri;
47  pshape->area();
48  return 0;
49 }
A.
rectangle area: triangle area:
B.
parent class area: parent class area:
C.
运行时报错
D.
编译时报错
题目信息
C++语言等级考试真题 2024年 六级 选择题
-
正确率
0
评论
30
点击