9-4-1.cpp

来自「学习c++的ppt」· C++ 代码 · 共 40 行

CPP
40
字号
#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 + =
减小字号Ctrl + -
显示快捷键?