diff --git a/3.cpp b/3.cpp new file mode 100644 index 0000000..8ebccc5 --- /dev/null +++ b/3.cpp @@ -0,0 +1,41 @@ +//声明抽象基类Shape,包含2个虚函数shapeName( )和perimeter( ),分别返回形状名和周长。由它派生出1个子类:Trapezoid(等腰梯形) +//用一个非类内函数printPerimeter(Shape& s),以基类引用作为函数形式参数,输出周长,图形的数据在创建对象时给定。 +#include +#include +using namespace std; +class Shape +{public: + virtual string shapeName( ){} + virtual double perimeter( ){} +}; + + +/**************补充Trapezoid类定义**************/ +class Trapezoid:public Shape +{ +public: + Trapezoid(float i,float j,float k):a(i),b(j),c(k){} + string shapeName() + { + return "Trapezoid"; + } + double perimeter() + { + return a+b+2*c; + } +private: + float a,b,c; +}; + +/**************补充Trapezoid类定义**************/ + +void printPerimeter(Shape& s) //以基类引用作为函数形参 +{cout<<"perimeter of "<