|
|
|
@ -0,0 +1,41 @@
|
|
|
|
|
//声明抽象基类Shape,包含2个虚函数shapeName( )和perimeter( ),分别返回形状名和周长。由它派生出1个子类:Trapezoid(等腰梯形)
|
|
|
|
|
//用一个非类内函数printPerimeter(Shape& s),以基类引用作为函数形式参数,输出周长,图形的数据在创建对象时给定。
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
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 "<<s.shapeName( )<<" = "<<s.perimeter( )<<endl;}
|
|
|
|
|
|
|
|
|
|
int main( )
|
|
|
|
|
{ double a=0, b=0, c=0;
|
|
|
|
|
cin >> a >> b >> c;
|
|
|
|
|
Trapezoid trap(a,b,c);
|
|
|
|
|
printPerimeter(trap);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|