Ich habe eine Kugel Klasse mit den folgenden Eigenschaften zu schaffen:Seltsame Ausgaben? Benutzerdefinierte Klassen (C++)
eine Klasse Sphere Schreiben mit den folgenden Eigenschaften:
Privat Attribute: (1) X, Y, Z Koordinaten der Mitte (2) Radius
Accessor und Mutator Methoden
• Set und die X erhalten, Y- und Z-Koordinaten
• Set und den Radiuserhalten • Holen Sie sich das Volumen und die Fläche Bereich, wenn eine Kugel.Für eine Kugel,
Volumen = 4πr3/3
Fläche = 4πr2
ein Haupt Programm Schreiben Sie die Kugel-Klasse zu testen.
Ich habe noch nie zuvor mit Klassen gearbeitet. Ich denke, ich habe das richtig gemacht. Meine Ausgabe für mein Volume und meine Oberfläche kommt jedoch wirklich komisch aus. Unten ist mein Programm und meine Ausgabe.
PROGRAMM
#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;
}
OUTPUT Egal, was ich Eingabe als Radius, werde ich bekommen:
Diese berechnet auf ein Volumen von -5.18547e + 24 Einheiten in Würfel geschnitten, und eine Fläche Fläche von 1,4488e + 17.
Was mache ich falsch ?? Auch andere Ratschläge zum Aufräumen meiner Klasse wären hilfreich!
Sie berechnen/rufen 'Volume' und' SurfaceArea' vor Koordinaten und R werden initialisiert. –