c++中抽象基类

来源:互联网 发布:产业结构优化理论 编辑:程序博客网 时间:2024/06/03 06:08
#include <iostream>using namespace std;class Polygon {  protected:    int width, height;  public:    Polygon (int a, int b) : width(a), height(b) {}    virtual int area (void) =0;    void printarea()      { cout << this->area() << '\n'; }};class Rectangle: public Polygon {  public:    Rectangle(int a,int b) : Polygon(a,b) {}    int area()      { return width*height; }};class Triangle: public Polygon {  public:    Triangle(int a,int b) : Polygon(a,b) {}    int area()      { return width*height/2; }};int main () {  Polygon * ppoly1 = new Rectangle (4,5);  Polygon * ppoly2 = new Triangle (4,5);  ppoly1->printarea();  ppoly2->printarea();  delete ppoly1;  delete ppoly2;  return 0;}