class My3Vector {
private: // coordinates, hidden
double x;
double y;
double z;
public:
My3Vector();// The default constructor
My3Vector(double c1, double c2, double c3);// Other constructor
// methods:
double Length(); // get length of vector
// access elements
double X();
double Y();
double Z();
My3Vector Add( My3Vector p ); // add two vectors
};
Vorteile:
#include "My3Vector.h" int main() { My3Vector a, b(1.,1.,-1.), c(0.,2.,1.); // create 3 ThreeVec objects a = b.Add(c); // add ThreeVec b and c, result is stored in a cout << a.Length() << endl; }
Erst muss man Methoden aber noch implementieren:
#include "My3Vector.h" // include declaration of class // now implementation My3Vector::My3Vector() { // default constructor x = 0.; y = 0.; z = 0.; // set coords to 0. } My3Vector::My3Vector( double c1, double c2, double c3 ) { x = c1; y = c2; z = c3; // take args for coords } // get length of vector double My3Vector::Length() { return( sqrt( x*x + y*y +z*z ) ); } // access elements double My3Vector::X() { return x; } double My3Vector::Y() { return y; } double My3Vector::Z() { return z; } // add My3Vector My3Vector::Add( My3Vector p ) { My3Vector t; t.x = x + p.x; t.y = y + p.y; t.z = z + p.z; return( t ); }
GDuckeck 2019-08-01