#include <iostream>
using namespace std;
class Pyramid{
public:
Pyramid();
void set_surfaceArea(double, double, double);
void set_volume(double, double);
double get_surfaceArea();
double get_volume();
void print_sufraceArea();
void print_volume();
private:
double height,
sideLength,
perimeter,
baseArea,
surfaceArea,
volume;
};
Pyramid::Pyramid(){
height = 0;
baseArea = 0;
perimeter = 0;
sideLength = 0;
}
void Pyramid::set_surfaceArea(double baseArea, double sideLength, double perimeter){
baseArea = baseArea;
sideLength = sideLength;
perimeter = perimeter;
}
void Pyramid::set_volume(double baseArea, double height){
baseArea = baseArea;
height = height;
}
double Pyramid::get_surfaceArea(){
return (1/2) * baseArea * perimeter * sideLength;
}
double Pyramid::get_volume(){
x = (1/3) * baseArea * height;
return x;
}
int main(){
double height,
test,
sideLength,
perimeter,
baseArea;
cout << "Enter height: ";
cin >> height;
cout << "Enter side length: ";
cin >> sideLength;
cout << "Enter perimeter: ";
cin >> perimeter;
cout <<"Enter base area: ";
cin >> baseArea;
Pyramid x;
x.set_surfaceArea(baseArea, sideLength, perimeter);
x.set_volume(baseArea, height);
test = x.get_volume();
cout << "test: " << test << endl;
}
當我運行此代碼(並輸入數字輸入),我得到「測試:0」的輸出。這段代碼是我第一次嘗試從頭開始創建一個類。我相信有人可以很容易地指出爲什麼我的輸出給我'0',無論我放什麼。C++類輸出不正確
謝謝!
在'set_surfaceArea'中,您設置的變量等於它們自己。只需將參數重新命名爲與班級成員相同的內容即可,然後重試。 – ocket8888
但這是我設置的輸入變量的名稱。我應該把它們設置爲別的嗎? – Gprice1148
是的,這就是我的意思。 – ocket8888