如何编写程序,实现由定义抽象基类Shape(形状)派生出三个派生类
定义抽象基类Shape,由它派生出3个派生类: Circle(圆形)、Rectangle(矩形)、Tr足毂忍珩iangle(三角形),用一个函数printArea分别输出以上三者的面积,3个图形的数据在定义对象时给定。
#include<iostream> usingnamespacestd; classShape { public:
virtualfloatprintArea()const{return0.0;}; }; classCircle:publicShape {
public: Circle(float=0); virtualfloatprintArea()const{return3.14159*radius*radius;}
protected: floatradius; }; Circle::Circle(floatr):radius(r) { } classRectangle:publicShape { public: Rectangle(float=0,float=0);
virtualfloatprintArea()const; protected: floatheight; floatwidth; };
Rectangle::Rectangle(floatw,floath):width(w),height(h){ }
floatRectangle::printArea()const { returnwidth*height; }
classTriangle:publicShape { public: Triangle(float=0,float=0);
virtualfloatprintArea()const; protected: floatheight; floatwidth;