首页 > 解决方案 > 如何计算创建空心立方体所需的材料体积

问题描述

我想计算创建一个 10mm 10mm 10mm 厚度为 1mm 的空心立方体所需的材料体积。我可以使用以下代码获得空心立方体的体积,但我不知道如何获得创建空心立方体所需的材料体积。我认为这是一个愚蠢的问题,但我没有合适的答案。任何建议将不胜感激。谢谢

#include <iostream>

using namespace std;

class Volume           // Class definition to calculate the volume of the hollow 
{
    public:

    Volume(){};

    double cubeVolume(double length)
    {
        double volume= length * length * length;
        return volume;
    }

    double cube(double num)
    {
        return (num*num*num);
    }

    double getHollowCubeVolume(double length, double thickness)
    {
        double outerVol=cubeVolume(length);
        double innerVol= cube((length-(thickness+thickness)));
    
        double HollowCubeVolume = (cube(outerVol)-cube(innerVol));
    
        return HollowCubeVolume;
    }
};

int main()
{
   cout << "Volume of the material required for hollow cube" << endl; 

   Volume volObj;
   cout<<"Volume of material used to create a hallow cube: "<<volObj.getHollowCubeVolume(10,1)<<endl;

   return 0;
}

标签: c++

解决方案


首先关注评论。看来这只是一个愚蠢的错误。但是错误出现在以下函数中-

double getHollowCubeVolume(double length, double thickness)
{
    double outerVol=cubeVolume(length);
    double innerVol= cube(length-(thickness+thickness));
    
    double HollowCubeVolume = outerVol-innerVol;  //Just take the difference.
                                                  // No need to pass them in cube function again.
    
    return HollowCubeVolume;
}

推荐阅读