奇怪的输出?用户定义 Class (C++)

Strange Outputs? User-Defined Class (C++)

我必须创建一个具有以下属性的球体 class:

Write a class Sphere with the following properties:

Private attributes: (1) X, Y, Z coordinates of the center (2) Radius

Accessor and mutator methods to
• Set and get the X, Y, and Z coordinates
• Set and get the radius
• Get the volume and surface area if a sphere.

For a sphere,
Volume = 4πr3/3
Surface Area = 4πr2
Write a main program to test the sphere class.

我以前从未使用过 classes。我想我做对了。但是,我的 Volume 和 Surface Area 的输出结果真的很奇怪。下面是我的程序和我的输出。

程序

#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>

using namespace std;

class Sphere {
private:
    float X;
    float Y;
    float Z;
    float R;
    float Volume;
    float SurfaceArea;
public:
    float DefineCoordinates(float x, float y, float z);
    void DefineRadius(float radius);
    double GetVolume()
    {
        return (((4 * M_PI*pow(R, 3))) / 3);
    }
    double GetSurfaceArea()
    {
        return (4 * M_PI*pow(R, 2));
    }
    float GetX();
    float GetY();
    float GetZ();


};

float Sphere::GetX() {
    return X;
}

float Sphere::GetY() {
    return Y;
}

float Sphere::GetZ() {
    return Z;
}

float Sphere::DefineCoordinates(float x, float y, float z) {
    X = x;
    Y = y;
    Z = z;
    return 0;
}

void Sphere::DefineRadius(float radius) {
    R = radius;
}


int main() {
    float inputr, radius, x, y, z;
    Sphere sphere;
    double Volume = sphere.GetVolume();
    double SurfaceArea = sphere.GetSurfaceArea();
    char open = '(';
    char close = ')';
    char comma = ',';
    cout << "Please input the center of the sphere in the fashion (X,Y,Z) and press enter: ";
    cin >> open >> x >> comma >> y >> comma >> z >> close;
    cout << "Please define the radius of the sphere: ";
    cin >> inputr;
    sphere.DefineCoordinates(x, y, z);
    sphere.DefineRadius(inputr);
    cout << "This sphere has a center of (" << sphere.GetX() << ", " << sphere.GetY() << ", " << sphere.GetZ() << ")." << endl;
    cout << "This sphere has a radius of " << inputr << "." << endl;
    cout << "This computes to a volume of " << Volume << " units cubed, and a surface area of " << SurfaceArea << "." << endl;
}

输出 无论我输入什么作为半径,我都会得到:

This computes to a volume of -5.18547e+24 units cubed, and a surface area of 1.4488e+17.

我做错了什么??此外,任何其他清理我的 class 的建议都会有所帮助!

Sphere 有一个默认构造函数,它不会初始化 XYZ 成员。构造完成后,您立即尝试根据这些不定值计算体积和表面。

换句话说,

double Volume = sphere.GetVolume();

没有定义数学关系(似乎你认为它定义了),它是命令式的。体积会被计算一次(用于Volume的初始化)并且它的值不会随着你改变sphere的属性而改变。在调用 Sphere::DefineCoordinatesSphere::DefineRadius.

之后 进行计算

或者,使构造这样一个无效的 Sphere 对象成为不可能。了解如何使用构造函数,并时常使用一些 const 限定符。

为了完整起见,Sphere::VolumeSpehre::SurfaceArea 有什么用?为什么 Sphere::DefineCoordinates return 是 float(总是 0),为什么我的答案不在顶部?

您调用 GetVolume() 方法的时间过早。在实际从用户那里获取半径后调用它。

int main() {
float inputr, radius, x, y, z;
Sphere sphere;
double SurfaceArea = sphere.GetSurfaceArea();
char open = '(';
char close = ')';
char comma = ',';
cout << "Please input the center of the sphere in the fashion (X,Y,Z) and press enter: ";
cin >> open >> x >> comma >> y >> comma >> z >> close;
cout << "Please define the radius of the sphere: ";
cin >> inputr;
sphere.DefineCoordinates(x, y, z);
sphere.DefineRadius(inputr);
double Volume = sphere.GetVolume();
cout << "This sphere has a center of (" << sphere.GetX() << ", " << sphere.GetY() << ", " << sphere.GetZ() << ")." << endl;
cout << "This sphere has a radius of " << inputr << "." << endl;
cout << "This computes to a volume of " << Volume << " units cubed, and a surface area of " << SurfaceArea << "." << endl;
}

如上。

简单, 在定义半径之前读取体积和表面。

您的问题是,您在读取输入之前调用了 GetVolume()GetSurfaceArea() 函数。您的计算基于未初始化的值。

您应该在 DefineCoordinates()DefineRadius()

调用之后移动函数调用

您的主要内容应与此类似:

int main() {
    float inputr, radius, x, y, z;
    Sphere sphere;
    char open = '(';
    char close = ')';
    char comma = ',';
    cout << "Please input the center of the sphere in the fashion (X,Y,Z) and press enter: ";
    cin >> open >> x >> comma >> y >> comma >> z >> close;
    cout << "Please define the radius of the sphere: ";
    cin >> inputr;
    sphere.DefineCoordinates(x, y, z);
    sphere.DefineRadius(inputr);
    double Volume = sphere.GetVolume();
    double SurfaceArea = sphere.GetSurfaceArea();
    cout << "This sphere has a center of (" << sphere.GetX() << ", " << sphere.GetY() << ", " << sphere.GetZ() << ")." << endl;
    cout << "This sphere has a radius of " << inputr << "." << endl;
    cout << "This computes to a volume of " << Volume << " units cubed, and a surface area of " << SurfaceArea << "." << endl;
}