c++继承题目

等边梯形是一个四边形,当它的上底和下底相等时,就变成矩形,所以可以说矩形是一种特殊的等边梯形。当矩形的长和宽相等时,变成一个正方形,所以可以认为正方形是一种特殊的矩形。它们之间存在继承关系。按照如下设计完成程序开发,只需要实现这个设计框架,不能修改,不能增加或者删除成员函数和成员变量。

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学习令我充实

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值