Я пытаюсь заставить этот код работать, чтобы вычислить площадь поверхности и объем коробки. Он компилирует, но не выводит правильные данные. Я думаю, может быть, проблема находится в пределах void Box :: parts, но попала в стену. Или это может быть высота = высота, но я не мог заставить ее работать без ошибок любым другим способом.Площадь поверхности/том
Вот код:
#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
Как вы думаете, что ваши методы 'set' будут правильно устанавливать что-либо, если вы не предоставите значение для их установки? Например, 'setHeight()': что вызовет этот вызов, чтобы установить высоту? – emlai
В качестве побочного примечания в вашей 'getSurfaceArea' есть небольшая ошибка. – emlai
Вы сделали ту же ошибку [за неделю до] (http://stackoverflow.com/q/28257307/560648) ... –