You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
//声明抽象基类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 ;
}