Write a 3D vector class cvector3d, which must contain three constructor types and perform operations such as copy, dot product, cross product, modulo and unitization, and implement "+;-; =; =; + =;-=; *; [] "and other operators.
Note: Since the member variables corresponding to 3D data are not stored in arrays during definition, it is meaningless to overload the subscript operator.
========================================================== ==========
# Include <iostream>
# Include <cmath>
Using namespace STD;
Template <class T>
Class cvector3d
{
Public:
Cvector3d ();
// Cvector3d (t x = 0, t y = 0, T z = 0 );
Cvector3d (t x, t y, t Z );
~ Cvector3d (){};
// Basic operation
Cvector3d (cvector3d & avector); // copy constructor
Double dotproduct (cvector3d & avector); // vector dot product
Cvector3d crossproduct (cvector3d & avector); // vector Cross Product
Cvector3d normalized (); // vector unitization
Double vectormodulo (); // vector modulo operation
Double vectorangle (cvector3d & avector); // vector angle
Void vectorprint ();
// Operator overload
Cvector3d operator + (const cvector3d & ob1 );
Cvector3d operator-(const cvector3d & ob1 );
Cvector3d & operator = (const cvector3d & SRC );
Bool operator = (const cvector3d & OBJ );
Cvector3d & operator ++ = (const cvector3d & ob1 );
Cvector3d & operator-= (const cvector3d & ob1 );
// Cvector3d & operator * (); // overload of pointer Operators
Void operator * (); // reload of pointer Operators
PRIVATE:
T x_offset;
T y_offset;
T z_offset;
};
Template <class T>
Void cvector3d <t>: vectorprint ()
{
Cout <"(" <x_offset <"," <y_offset <"," <z_offset <")" <Endl;
}
Template <class T>
Cvector3d <t>: cvector3d ()
{
X_offset = 0;
Y_offset = 0;
Z_offset = 0;
}
Template <class T>
Void cvector3d <t>: Operator *()
{
Cout <"(" <x_offset <"," <y_offset <"," <z_offset <")" <Endl;
}
// Template <class T>
// Cvector3d <t> & cvector3d <t>: Operator *()
//{
// Return * this;
//}
Template <class T>
Cvector3d <t> & cvector3d <t>: Operator-= (const cvector3d & ob1)
{
X_offset-= ob1.x _ offset;
Y_offset-= ob1.y _ offset;
Z_offset-= ob1.z _ offset;
Return * this;
}
Template <class T>
Cvector3d <t> & cvector3d <t>: Operator ++ = (const cvector3d & ob1)
{
X_offset + = ob1.x _ offset;
Y_offset + = ob1.y _ offset;
Z_offset + = ob1.z _ offset;
Return * this;
}
Template <class T>
Bool cvector3d <t>: Operator = (const cvector3d & OBJ)
{
If (x_offset = obj. x_offset & y_offset = obj. y_offset & z_offset = obj. z_offset)
Return true;
Else
Return false;
}
Template <class T>
Cvector3d <t> & cvector3d <t>: Operator = (const cvector3d & SRC)
{
If (this = & SRC)
Return * this;
X_offset = SRC. x_offset;
Y_offset = SRC. y_offset;
Z_offset = SRC. z_offset;
Return * this;
}
Template <class T>
Cvector3d <t> cvector3d <t>: Operator-(const cvector3d & ob1)
{
Cvector3d temp;
Temp. x_offset = x_offset-ob1.x _ offset;
Temp. y_offset = y_offset-ob1.y _ offset;
Temp. z_offset = z_offset-ob1.z _ offset;
Return temp;
}
Template <class T>
Cvector3d <t> cvector3d <t>: Operator + (const cvector3d & ob1)
{
Cvector3d temp;
Temp. x_offset = x_offset + ob1.x _ offset;
Temp. y_offset = y_offset + ob1.y _ offset;
Temp. z_offset = z_offset + ob1.z _ offset;
Return temp;
}
Template <class T>
Cvector3d <t> cvector3d <t>: normalized ()
{
Double Moudle = This-> vectormodulo ();
Cvector3d temp;
Temp. x_offset = x_offset/Moudle;
Temp. y_offset = y_offset/Moudle;
Temp. z_offset = z_offset/Moudle;
Return temp;
}
Template <class T>
Cvector3d <t> cvector3d <t>: crossproduct (cvector3d <t> & avector)
{
Cvector3d temp;
Temp. x_offset = y_offset * avector. z_offset-z_offset * avector. y_offset; // y1z2-z1y2
Temp. y_offset = z_offset * avector. x_offset-x_offset * avector. z_offset; // z1x2-x1z2
Temp. z_offset = x_offset * avector. y_offset-y_offset * avector. x_offset; // x1y2-y1x2
Return temp;
}
Template <class T>
Double cvector3d <t>: vectorangle (cvector3d & avector)
{
Return (x_offset * avector. x_offset + y_offset * avector. y_offset + z_offset * avector. z_offset)
/This-> vectormodulo () * avector. vectormodulo ();
}
Template <class T>
Double cvector3d <t>: vectormodulo ()
{
// Add long double
Return SQRT (Long double (x_offset * x_offset + y_offset * y_offset + z_offset * z_offset ));
}
Template <class T>
Double cvector3d <t >:: dotproduct (cvector3d & avector)
{
Return x_offset * avector. x_offset + y_offset * avector. y_offset + z_offset * avector. z_offset;
}
Template <class T>
Cvector3d <t>: cvector3d (cvector3d & avector)
{
This-> x_offset = avector. x_offset;
This-> y_offset = avector. y_offset;
This-> z_offset = avector. z_offset;
}
Template <class T>
Cvector3d <t>: cvector3d (t x, t y, t Z)
: X_offset (X)
, Y_offset (y)
, Z_offset (z)
{
}
Int main ()
{
Cvector3d <int> A (5, 6, 7), B (8, 9, 10 );
Cvector3d <int> C; // No parameter construction
* C; // the pointer operator '*' to overload the call.
C = a + B; // '+'
* C;
C + = B; // '+ ='
* C;
Cout <C. dotproduct (B) <Endl;
Cout <C. vectormodulo () <Endl;
Cvector3d <int> d = C. crossproduct ();
* D;
Cvector3d <int> E = D; // '='
* E;
Return 1;
}