HELP please with operators on vectors!

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 );

return result;
}

// opposite

Vector3D Vector3D::operator- () const {
Vector3D result(-x, -y, -z);
return result ;
}

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!
multiplication by a scalar.
Multiply x, y and z on given value
and the norm of a vector.
sqrt( x*x + y*y + z*z )
scalar product of 2 vectors
x1*x2 + y1*y2 + z1*z2
cross product of 2 vectors
x = y1z2 - z1y2
y = z1x2 - x1z2
z = x1y2 - y1x2

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.
Last edited on
i know what they are i just dont get how to code it
examples for norm and scalar product:
1
2
3
4
5
6
7
8
9
double Vector3D::norm()
{
    return std::sqrt( x*x + y*y + z*z )
}

double Vector3D::scalar(const Vector3D& rhs)
{
    return x*rhs.x + y*rhs.y + z*rhs.z;
}
Topic archived. No new replies allowed.