测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A32247. 运行下列代码,屏幕上输出( )。#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 << "paren…

单选题 困难

题目描述

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

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

选项(单选)

上一题 下一题