C++使用纯虚函数完成正方形和圆形的求面积运算。 |
您所在的位置:网站首页 › 编程计算图形的面积,程序可计算圆形 › C++使用纯虚函数完成正方形和圆形的求面积运算。 |
#include using namespace std; class Shape { public: virtual float area()=0; };
class Rectangle:public Shape { private: float length; float withd; public: Rectangle(float _l=0,float _w=0) { length=_l; withd=_w; } float area() { return length*withd; }
};
class Circle:public Shape { private: float radius; public: Circle(float _r=0) { radius=_r; }
float area() { return 3.14*radius*radius; } };
int main(int argc, const char * argv[]) {
Rectangle *rectobj; rectobj=new Rectangle(10.23f,20.33f); Circle *circle; circle=new Circle(102.f); cout |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |