表面积/体积

Surface Area / Volume

本文关键字:体积 表面积      更新时间:2023-10-16

我正试图让这段代码用于计算盒子的表面积和体积。它编译但不输出正确的数据。我想也许问题出在了虚空的盒子里::零件却撞到了墙上。或者它可以是Height=Height,但我无法让它以任何其他方式运行而不出错。

这是代码:

#include <iostream>
#include "Box.hpp"
using namespace std;

int main()
{
    Box box;
    double boxHeight, boxWidth, boxLength;
    cout << "This program will tell you the surface area and volume of a box" << endl;
    cout << "Please enter the height " << endl;
    cin >> boxHeight;
    cout << "Please enter the width " << endl;
    cin >> boxWidth;
    cout << "Please enter the length" << endl;
    cin >> boxLength;
    box.setHeight();
    cout << "The surface area is " << box.getSurfaceArea() << endl;
    cout << "The volume is " << box.getVolume() << endl;

    return 0;
}
void Box::setHeight(){
    if(Height< 0)
        cout << "Error.  Must enter a positive number " << endl;
    else
        Height = Height;
    }

void Box::setLength(){
   if(Length< 0)
        cout << "Error.  Must enter a positive number " << endl;
    else
        Length = Length;
    }

void Box::setWidth(){
   if(Width< 0)
        cout << "Error.  Must enter a positive number " << endl;
    else
        Width = Width;
    }

double Box::getSurfaceArea(){
    return 2*(Length*Width) + 2*(Length*Height) + 2*(Width + Height);
}

double Box::getVolume(){
    return Length*Width*Height;
}

hpp文件是这样的:

#ifndef BOX_HPP_INCLUDED
#define BOX_HPP_INCLUDED
class Box
{
public:
    void setHeight();
    void setWidth();
    void setLength();
    double getVolume();
    double getSurfaceArea();
private:
    double Length;
    double Width;
    double Height;
};

#endif 

首先,您需要传递要在setters中设置的值:

void setHeight(double height);
void setWidth(double width);
void setLength(double length);
void Box::setHeight(double height)
{
    if(height < 0)
        cout << "Error.  Must enter a positive number " << endl;
    else
        Height = height;
}
void Box::setWidth(double width)
{
    if(width < 0)
        cout << "Error.  Must enter a positive number " << endl;
    else
        Width = width;
}
void Box::setLength(double length)
{
    if(length < 0)
        cout << "Error.  Must enter a positive number " << endl;
    else
        Length = length;
}

此外,您不仅需要设置高度,还需要设置盒子的宽度和长度:

box.setHeight(boxHeight);
box.setWidth(boxWidth);
box.setLength(boxLength);

这就行了。

box.setHeight();

想想你是怎么告诉这个盒子设定高度的。更具体地说,它应该将其高度设置为什么。通常,你会告诉它这样的东西:

box.setHeight (h);

其中h是要将其设置为的高度。这意味着将一个参数传递给一个看起来像的函数

void Box::setHeight(double newH){
    if(newH < 0)
        cout << "Error.  Must enter a positive number " << endl;
    else
        Height = newH; // assuming Height is a member variable.
    }
}

目前的情况:

Height = Height;

只需将高度保留为以前的任何值。

当然,其他两个维度也是如此。


还有一件事我想提一下,类实际打印一些东西来指示问题是不寻常的,因为这通常是调用者应该决定的。您不希望您的应用程序输出被输出信息的通用类搞砸。

最好是返回一个错误代码或抛出一个异常,让调用者决定该做什么。或者甚至可以无声地使用参数的绝对值,比如:

if (val < 0) val = -val;
Height = val;

尽管有些人会说这违反了最不惊讶的原则,但这实际上取决于你认为什么是最好的行为。

在这种情况下可能没问题,因为无论立方体在空间中的方向或位置如何,它仍然具有相同的表面积和体积。

您的方法中存在问题,因为用户输入的值不可用于设置函数,所以您只是用垃圾初始化维度,所以要么使用带参数的设置函数,如setHeight(int height),要么重新构造程序,使其不需要参数。

问题是,您的set函数没有设置任何内容:

  • 如果其对应的成员变量低于0,则不执行任何操作
  • 否则,他们将相应的成员变量分配回自己,也就是什么都不做

您想告诉set函数它们应该将值设置为什么

例如,如果你想告诉"将宽度设置为100",那么在像setWidth();这样的调用中缺少一条重要信息——它不知道你想将宽度设置成100。

为什么?

因为你没有告诉它。你想以某种方式将信息传递给你的set函数。函数参数就是这样的:

CCD_ 7声明CCD_ 8接受CCD_ 9作为参数。

现在你可以这样称呼它:setWidth(100),它就会知道你想把宽度设置为100。或者你可以做:

int width;
cin >> width;
box.setWidth(width);

以向用户询问CCD_ 11的值。


那么现在剩下的唯一事情就是在函数体中使用该函数参数变量来检查NewWidth是否低于零,或者将NewWidth赋值给Width

问题是用户输入的值永远无法访问类的私有成员。必须在方法setHeight setWidth中提供一个参数,才能实际设置私有成员HeightWidth(长度也类似)。

在您当前的代码中,您没有提供构造函数,因此实际上是在处理私有成员Height、Width和Length的垃圾值。

编辑:hpp文件的public中的构造函数应该看起来像:

Box()
{
Height=0;
Width=0;
Length=0;
}