so i am doing this program on vectors using operators.
I have managed to use the addition and subtraction operators but i can't seem to do the more complicated ones....could someone please give me the code for the following ones? It shouldn't be too hard but i am so dreadful at this! so sorry,
basically in my vector.h file i have:
class Vector3D
{
public:
Vector3D() : x(0.0), y(0.0), z(0.0){}
Vector3D (double X , double Y, double Z) : x(X) , y(Y) , z (Z) { }
void display() const;
bool operator== (Vector3D const& b ) const ;
Vector3D operator+ (Vector3D const& V ) const ;
Vector3D operator- (Vector3D const& V ) const ;
Vector3D operator- () const;
double X () const;
double Y () const;
double Z () const ;
private:
double x, y, z;
};
and in my vector.cc file i have in part of my code:
for ADDITION the operator was
Vector3D Vector3D::operator+ (Vector3D const& V ) const {
double V1 ( x + V.X () ) ;
double V2 ( y + V.Y() );
double V3 ( z + V.Z() );
Vector3D result ( V1 , V2 , V3 ) ;
return resultat ;
}
// substraction
Vector3D Vector3D::operator- (Vector3D const& V ) const {
double V1 ( x - V.X () ) ;
double V2 ( y - V.Y () ) ;
double V3 ( z - V.Z () ) ;
Vector3D result ( V1, V2, V3 );
those are the 3 i've done. I can't seem to do : multiplication by a scalar.
scalar product of 2 vectors
cross product of 2 vectors
the square of the vector norm
and the norm of a vector.
i have a test vector file too and it runs in the terminal for the addition, opposite, and substraction. e.g. cout << vect1 << " + " << vect2 << " = " << vect1+vect2 << endl;
how would i write them for the norm, scalar, and cross? i know for a scalar i would write cout << vect1 << "*" << 3 << "=" << vect1*3 << endl; once i've written the operator for scalar but for the other 3 i don't know how..
so please let me know how i would write the operators for the operations mentioned above! thanks!
For vector products you cannot use operators, so you shoud use member- or standalone functions: Vector3D scalar(const Vector3D& lhs, const Vector3D& rhs)
etc.
norm and squared norm should probably be member function without parameters.