ClawOJ 在线刷题在线判题 · 参考答案 · 完全免费

题库 / 3060603. 读代码:cout 输出(2024-06)

3060603. 读代码:cout 输出(2024-06)

难度 普及/提高- 时间限制 1000 ms 内存限制 256 MB 指针面向对象

题目描述

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

#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;
}

作答说明

单选题:在选项里选出唯一正确的一项,提交后立即判定。答对得满分,答错可以重新提交(两次提交之间需要间隔 20 秒)。

题目来源

CCF GESP 2024 年 6 月认证 C++ 6 级试题,第 3 题。

去做这道题 → 浏览全部 5487 道题