c++ - 奇怪的输出?用户定义类 (C++)

标签 c++ class output

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

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.

我以前从未接触过类(class)。我想我做对了。但是,我的 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)的建议都会有所帮助!

最佳答案

您过早调用 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;
}

如上。

关于c++ - 奇怪的输出?用户定义类 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37093935/

相关文章:

c++ - 为什么 -2147483648 和 (int)-2147483648 不同

c++ - 这个问题中发生了运算符重载,但参数的数量和类型是相同的

c++ - 为什么在 C++ 中,Visual Studio 将我的类构造函数描述为具有 "2+ Overloads"?

c++ - 将文本文件输出组织到列中

c++ - 尝试在 C++ 程序中设置简单的键盘输入。使用变量存储 true 或 false

java - 执行外部Java代码并获取输出

c++ - 友好性和访问级别的继承

c++ - 如何将全局常量变量更改为可以通过函数分配的变量

python - 从其他实例属性派生的类实例属性

java - 如何在 Java 中获取给定类的数组类?