【问题标题】:why can't i use the value of area in volume? i get Volume: -1405824736为什么我不能使用体积中的面积值?我得到音量:-1405824736
【发布时间】:2021-04-13 03:36:07
【问题描述】:

创建一个名为 Rectangle 的类,具有长宽和面积。使用适当的成员函数从用户那里读取长度和宽度并计算矩形的面积。从类 rectangle 中创建一个名为 Box 的派生类。使用适当的成员函数来读取高度并计算体积。

#include <iostream>
using namespace std;

class Rectangle
{
    protected:
        int length;
        int breadth;
        int area;
    public:
        int input();
        int calc();
};

int Rectangle::input()
{
    cout<<"Enter the length and breadth:"<<endl;
    cin>>length>>breadth;

    return 0;
}

int Rectangle::calc()
{
    area=length*breadth;
    cout<<"Area: "<<area<<endl;

    return 0;
}

class Box:public Rectangle
{
    int height;

    public:
        int input();
        int vol();
};

int Box::input()
{
    cout<<"Enter the height:";
    cin>>height;

    return 0;
}

int Box::vol()
{
    cout<<"Volume: "<<area*height<<endl;

    return 0;
}

int main()
{
    Rectangle r;
    Box b;

    r.input();
    r.calc();
    cout<<endl;
    b.input();
    b.vol();

    return 0;
}

【问题讨论】:

  • 可能是溢出。输入较小的数字,例如 20 和 50。
  • 在 2 或 3 上都不起作用。在派生类中传递区域值时,传递的区域值为 -919949024。
  • Rectangle r; - 你不需要这个,Box b; 已经包含一个 Rectangle 子对象。 r.input(); r.calc(); - 将其替换为 b.Rectangle::input(); b.calc();。尽管重新考虑类的设计会更好,这样您就不必每次使用它们时都记住所有这些舞蹈。
  • 读取未初始化的变量会让你的程序拥有undefined behavior

标签: c++


【解决方案1】:

您对程序输出的惊讶向我表明您认为对象rbRectangle 子对象在某种程度上是同一件事。他们不是。

您的代码存在以下问题。

  1. 您没有正确初始化类的成员变量。
  2. Box::input 不准确。它需要确保Rectangle子项目的成员也被输入。
  3. 您的程序具有未定义的行为,因为它使用了未初始化的成员变量。

从编程指南的角度来看,最好:

  1. 使用operator&gt;&gt;(std::istream&amp;, Box&amp;)operator&gt;&gt;(std::istream&amp;, Rectangle&amp;) 代替input() 成员函数。这将允许您从任何流中读取数据,而不仅仅是std::cin
  2. 删除成员变量area。将其替换为返回计算面积的函数 area()
  3. 更新vol()函数以返回对象的体积,而不是将其打印到std::cin

这是我建议的应该可以运行的程序版本。

#include <iostream>
using namespace std;

class Rectangle
{
    protected:
        int length = 0;
        int breadth = 0;
    public:
        int area() const;

        friend std::istream& operator>>(std::istream& in, Rectangle& r);
};

int Rectangle::area() const
{
   return length*breadth;
}

std::istream& operator>>(std::istream& in, Rectangle& r)
{
    return in >> r.length >> r.breadth;
}

class Box: public Rectangle
{
    int height = 0;

    public:
        int volume() const;

    friend std::istream& operator>>(std::istream& in, Box& b);
};

std::istream& operator>>(std::istream& in, Box& b)
{
   // Read the Rectangle sub-object of the Box and then the
   // height of the Box..
   Rectangle& r = b;
   return in >> r >> b.height;
}

int Box::volume() const
{
    return this->area()*height;
}

int main()
{
    Box b;

    cout<<"Enter the length, breadth, and height: ";
    cin >> b;

    cout << "Area of the Rectangle sub-object: " << b.area() << std::endl;
    cout << "Volume of the Box: " << b.volume() << std::endl;

    return 0;
}

【讨论】:

    【解决方案2】:

    在这种情况下,您的问题似乎是您没有为变量b 设置长度和宽度。 Box::input 仅初始化 height 如果您打算使用与 r 相同的输入,那么此代码可能更容易,

    //modify Box::input to:
    int Box::input()
    {
        Rectangle::input(); // will initialise length, breadth
        cout<<"Enter the height:";
        cin>>height;
    
        return 0;
    }
    
    ...
    
    //Then in main
    int main()
    {
        Box b;
    
        b.input();
        b.calc();
        cout<<endl;
        b.vol();
    
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      这是因为您在调用Rectangle::calc() 时正在计算面积。但是当你调用Box::vol() 时,你的变量area 会迎来初始化。你得到的可能是一些垃圾价值。在vol,重新计算面积或拨打this-&gt;calc。而且你也缺少长度和宽度。

      将成员初始化为默认构造函数是一个很好的做法。一个好处是你不会得到垃圾值。你应该有一个像这样的矩形的默认构造函数:

      Rectangle::Rectangle()
      : length(0), breadth(0), area(0)
      {
      }
      

      在 Box 中,你应该有这样的构造函数。

      Box::Box()
      : Rectangle::Rectangle ()
      {
      }
      

      另外,我不认为在类成员函数中输入是一个好主意。你应该有Rectangle::Rectangle(int length, int breadth) 或者像Rectangle::setLength(int l) 这样的二传手。像这样接受输入并设置值。

      【讨论】:

        猜你喜欢
        • 2022-11-26
        • 2017-01-01
        • 1970-01-01
        • 2021-02-02
        • 2012-04-17
        • 2018-10-15
        • 2022-11-28
        • 2018-09-17
        • 1970-01-01
        相关资源
        最近更新 更多