085-C++

来源:互联网 发布:js改变color 编辑:程序博客网 时间:2024/06/06 15:52

C++接口

#include <iostream> using namespace std; // 基类class Shape {public:   // 提供接口框架的纯虚函数   virtual int getArea() = 0;   void setWidth(int w)   {      width = w;   }   void setHeight(int h)   {      height = h;   }protected:   int width;   int height;}; // 派生类class Rectangle: public Shape{public:   int getArea()   {       return (width * height);    }};class Triangle: public Shape{public:   int getArea()   {       return (width * height)/2;    }}; int main(void){   Rectangle Rect;   Triangle  Tri;    Rect.setWidth(5);   Rect.setHeight(7);   // 输出对象的面积   cout << "Total Rectangle area: " << Rect.getArea() << endl;    Tri.setWidth(5);   Tri.setHeight(7);   // 输出对象的面积   cout << "Total Triangle area: " << Tri.getArea() << endl;     return 0;}


原创粉丝点击