我必须创建一个具有以下属性的球体类:
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/