开发者社区> 问答> 正文

为什么这段代码中对象rectangle的各个成员函数输出的值是对的,而box的却都是错的

#include
using namespace std;
class rectangle
{
protected:
double length,width,l,w;
public:
void setlength();
void getlength();
void setwidth();
void getwidth();
double area();
double perimeter();
double diagmonal();
};
void rectangle::setlength()
{
cout<<"please enter the length of rectangle(box):";
cin>>l;
}
void rectangle::getlength()
{
length=l;
}
void rectangle::setwidth()
{
cout<<"please enter the width of rectangle(box):";
cin>>w;
}
void rectangle::getwidth()
{
width=w;
}
double rectangle::area()
{
return(length*width);
}
double rectangle::perimeter()
{
return(2*(length+width));
}
double rectangle::diagmonal()
{
double x=length*length+width*width;
return(sqrt(x));
}
class box:protected rectangle
{
protected:
double height,h;
public:
void setheight();
void getheight();
double area();
double perimeter();
double diagmonal();
double volume();
};
void box::setheight()
{
cout<<"please enter the height for box:";
cin>>h;
}
void box::getheight()
{
height=h;
}
double box::area()
{
return(2*(length*width+length*height+width*height));
}
double box::perimeter()
{
return(4*(length+width+height));
}
double box::diagmonal()
{
return(sqrt(length*length+width*width+height*height));
}
double box::volume()
{
return((length*width)*height);
}
int main()
{
rectangle r;
box b;
r.setlength();
r.getlength();
r.setwidth();
r.getwidth();
b.setheight();
b.getheight();
cout<<"the box's volume is:"<<b.volume()<<endl;
}

展开
收起
a123456678 2016-03-06 10:40:18 2479 0
1 条回答
写回答
取消 提交回答
  • #include <iostream>
    #include <math.h>
    using namespace std;
    class rectangle
    {
    protected:
      double length,width,l,w;
    public:
      void setlength();
      void getlength();
      void setwidth();
      void getwidth();
      double area();
      double perimeter();
      double diagmonal();
    };
    void rectangle::setlength()
    {
      cout<<"please enter the length of rectangle(box):";
      cin>>l;
    }
    void rectangle::getlength()
    {
      length=l;
    }
    void rectangle::setwidth()
    {
      cout<<"please enter the width of rectangle(box):";
      cin>>w;
    }
    void rectangle::getwidth()
    {
      width=w;
    }
    double rectangle::area()
    {
      return(length*width);
    }
    double rectangle::perimeter()
    {
      return(2*(length+width));
    }
    double rectangle::diagmonal()
    {
      double x=length*length+width*width;
      return(sqrt(x));
    }
    class box:protected rectangle
    {
    protected:
      double height,h;
    public:
      void setheight();
      void getheight();
      double area();
      double perimeter();
      double diagmonal();
      double volume();
    };
    void box::setheight()
    {
      cout<<"please enter the height for box:";
      cin>>h;
    }
    void box::getheight()
    {
      height=h;
      cout << height << endl;
    }
    double box::area()
    {
      return(2*(length*width+length*height+width*height));
    }
    double box::perimeter()
    {
      return(4*(length+width+height));
    }
    double box::diagmonal()
    {
      return(sqrt(length*length+width*width+height*height));
    }
    double box::volume()
    {
      setlength();
      getlength();
      setwidth();
      getwidth();
      return((length*width)*height);
    }
    int main()
    {
      box * p = new box;
      p->setheight();
      p->getheight();
      cout<<"the box's volume is:"<<p->volume()<<endl;
      delete p;
      p = NULL;
    }

    GET /V2/730458/2149880/1428622663/index.html HTTP/1.1
    Host: cdn.optmd.com
    Connection: keep-alive
    Cache-Control: max-age=0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
    User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36
    Referer: http://d169bbxks24g2u.cloudfront.net/ads/amzn-ads.html?size=300x250
    Accept-Encoding: gzip, deflate, sdch
    Accept-Language: zh-CN,zh;q=0.8
    If-None-Match: "c028a-e5-51353275a5cc0"
    If-Modified-Since: Thu, 09 Apr 2015 23:39:07 GMT

    2019-07-17 18:54:13
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载