问答题

简答题

阅读以下说明和C++代码,填入 (n) 处。
以下C++代码使用虚函数实现了同一基类shape派生出来的Class rectangle、Class triangle、Class circle实现了计算矩形、圆形面积的计算。仔细阅读以下代码,将 (n) 处语句补充完整。
[代码5-1]
#include<iostream.h>
#define PI 3.14159
class shape {//基类
protected:
(1)
public:
(2)
(3)
};
[代码5-2]
class rectangle: public shape {
public:
rectangle (int x2,int y2,int r2): (4) {};
double area ( ) {return x*y; };
};
class circle: public shape {
public:
circle (int x3,int y3,int r3): (5) {};
double area ( ) {return r*r*PI; };
};
[代码5-3]
void main ( )
{
rectangle r (10,20,0);
circle c (0,0,30);
shape (6)
cout<<"长方形面积="<<s1->area ( ) <<endl;
cout<<"圆形面积="<<s2->area ( ) <<endl;
}
[运行结果]
长方形面积=200
圆形面积=2827.43

【参考答案】

(1)intx,y,r;
(2)shape (int x1,int y1,int r1): x(x1),y(y......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)