📄 9-4-1.cpp
字号:
#include <iostream.h>
const float Pi = 3.14156;
class Figure {
public:
virtual float area() = 0; // 纯虚函数
};
class Circle:public Figure {
private:
float radius;
public:
Circle(float r) {radius = r;}
float area() {return radius*radius*Pi;}
};
class Triangle:public Figure {
protected:
float high, wide;
public:
Triangle(float h, float w) {high = h; wide = w;}
float area() {return high * wide;}
};
class Rectangle:public Triangle {
public:
Rectangle(float h, float w):Triangle(h, w) {}
float area() {return high * wide;}
};
float total(Figure *pf[], int n)
{ float sum = 0;
for(int i=0; i < n; i ++)
sum += pf[i]->area();
return sum;
}
void main()
{ Figure *pf[4];
pf[0] = new Triangle(3.0, 4.0);
pf[1] = new Rectangle(2.0, 3.5);
pf[2] = new Rectangle(5.0, 1.0);
pf[3] = new Circle(10.0);
cout << "Total Area:"<<total(pf, 4)<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -