等边梯形是一个四边形,当它的上底和下底相等时,就变成矩形,所以可以说矩形是一种特殊的等边梯形。当矩形的长和宽相等时,变成一个正方形,所以可以认为正方形是一种特殊的矩形。它们之间存在继承关系。按照如下设计完成程序开发,只需要实现这个设计框架,不能修改,不能增加或者删除成员函数和成员变量。
#include<iostream>
#include<cmath>
using namespace std;
class Trapezoid{
protected:
double len1,len2,width;
public:
Trapezoid(double _len1,double _len2,double _width){
len1=_len1;len2=_len2;width=_width;
cout<<"Hi,Trapezoid!"<<endl;
}
double getArea(){
double a=0;
double b=0;
if(len2>len1){
a=(len2-len1)/2;
b=(len1+len2)*1/2*sqrt(width*width-a*a);
}
if(len1>len2){
a=(len1-len2)/2;
b= (len1+len2)*1/2*sqrt(width*width-a*a);
}
if(len2==len1) b= len1*width;
return b;
};
void setLen1(double _len1){
len1=_len1;
}
void setLen2(double _len2){
len2=_len2;}
void setWidth(double _width){
width=_width;
}
~Trapezoid(){
cout<<"Bye,Trapezoid!"<<endl;
}
};
class Rectangle: public Trapezoid{
public:
Rectangle(double _len,double _width):Trapezoid(_len,_len,_width){
cout<<"Hi,Rectangle!"<<endl;
}
void setLen(double _len){len1=_len;len2=_len;
}
~Rectangle(){cout<<"Bye,Rectangle!"<<endl;
}
};
class Square: public Rectangle{
public:
Square(double _side):Rectangle(_side,_side){cout<<"Hi,Square!"<<endl;
}
void setSide(double _side){len1=_side;len2=_side;
}
~Square(){cout<<"Bye,Square!"<<endl;}
};
istream& operator>>(istream&in,Trapezoid&a){
double _len1,_len2,_width;
in>>_len1>>_len2>>_width;
a.setLen1(_len1);a.setLen2(_len2);a.setWidth(_width);
return in;
}
istream& operator>>(istream&in,Rectangle&a){
double _len,_width;
in>>_len>>_width;
a.setWidth(_width);
a.setLen(_len);
return in;
}
istream& operator>>(istream&in,Square&a){
double _side;
in>>_side;
a.setSide(_side);a.setWidth(_side);
return in;
}
int main(){
char a;
cin>>a;
if(a=='T'){Trapezoid t(1,1,1);cin>>t;cout<<t.getArea()<<endl;
}
if(a=='R'){Rectangle r(1,1);cin>>r;cout