表面积/体积

时间:2015-02-08 05:20:09

标签: c++

我试图让这段代码用来计算盒子的表面积和体积。它编译但不输出正确的数据。我想也许问题是在void Box :: parts之内,但是已经碰壁了。或者它可能是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 

5 个答案:

答案 0 :(得分:2)

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

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);

这应该可以解决问题。

答案 1 :(得分:1)

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;

虽然有人会认为这违反了最不惊讶的原则,但这实际上取决于你认为最好的行为。

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

答案 2 :(得分:0)

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

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

编辑:hpp文件的public中的构造函数应如下所示:

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

答案 3 :(得分:0)

你的方法有一个问题,因为用户输入的值不能用于设置函数,所以你只是用垃圾来初始化维度,所以要么使用有争议的set函数,比如setHeight(int height),要么重构程序以便它不需要参数。

答案 4 :(得分:0)

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

  • 如果相应的成员变量低于0,则他们什么都不做。
  • 否则,他们将相应的成员变量分配给自己,也就是什么都不做。

您想告诉set函数他们应该将值设置为

例如,如果您想告诉&#34;将宽度设置为100&#34;,那么setWidth();这样的调用中缺少一条重要的信息 - 它没有知道你想要将宽度设置为100。

为什么?

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

void setWidth(int NewWidth)声明setWidth接受int作为参数。

现在您可以这样调用它:setWidth(100)并且它会知道您要将宽度设置为100.或者您可以这样做:

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

向用户询问width的值。


然后现在唯一剩下的就是在函数体中使用该函数参数变量来检查NewWidth是否低于零,否则,将NewWidth分配给Width

相关问题