Purpose The purpose of this assignment is to give you some experience in operato
ID: 3529478 • Letter: P
Question
Purpose The purpose of this assignment is to give you some experience in operator overloading. Program You will need to write a single class for this assignment, the Vector3 class. You will need to implement several methods and functions associated with this class. class Vector3 Data members This class contains 3 floating point data values representing the x, y, and z components of the vector. There are a couple of possible ways of implementing the data members, all of which can work for this assignment. The recommended way of storing these values is as an array of three floats. This will make coding the next assignment much easier. Methods and associated functionsExplanation / Answer
#include<iostream>
using namespace std;
class Vector3
{
private:
float a[3];
public:
Vector3(float x=0,float y =0, float z =0)
{
a[0] = x; a[1] = y; a[2] = z;
}
float& operator[]( int index)
{
return a[index];
}
friend ostream &operator<<(ostream &out, Vector3 v) //output
{
out<< " ( " << v[0] << " , " << v[1] << " , " << v[2] << " ) ";
return out;
}
friend Vector3 operator+( Vector3& v1, Vector3& v2);
friend Vector3 operator-( Vector3& v1, Vector3& v2);
friend float operator*( Vector3& v1, Vector3& v2);
friend Vector3 operator*(float k, Vector3& v);
friend Vector3 operator*( Vector3& v,float k);
friend bool operator==( Vector3& v1, Vector3& v2);
};
Vector3 operator+( Vector3& v1, Vector3& v2)
{
return Vector3(v1[0]+v2[0], v1[1]+v2[1],v2[2]+v2[2]);
}
Vector3 operator-( Vector3& v1, Vector3& v2)
{
return Vector3(v1[0]-v2[0], v1[1]-v2[1],v2[2]-v2[2]);
}
float operator*( Vector3& v1, Vector3& v2)
{
return (v1[0]*v2[0] + v1[1]*v2[1] +v2[2]*v2[2]);
}
Vector3 operator*(float k, Vector3& v)
{
return Vector3(k*v[0], k*v[1],k*v[2]);
}
Vector3 operator*( Vector3& v,float k)
{
return Vector3(k*v[0], k*v[1],k*v[2]);
}
bool operator==( Vector3& v1, Vector3& v2)
{
return ( v1[0]==v2[0] && v1[1] == v2[1] && v1[2] == v2[2]);
}
int main()
{
int test = 1;
cout << " Test " << test++ << ": ructor and printing " << endl;
Vector3 v1, v2(1.0, 2.0, 3.0);
cout << "v1: " << v1 << endl;
cout << "v2: " << v2 << endl;
cout << " Test " << test++ << ": Addition and subtraction " << endl;
Vector3 v3(1.0, 2.0, 3.0), v4(-2.0, 3.0, -1.0);
cout << "v3: " << v3 << endl;
cout << "v4: " << v4 << endl << endl;
cout << "v3 + v4 is " << v3 + v4 << endl
; cout << "v3 - v4 is " << v3 - v4 << endl;
cout << " Test " << test++ << ": Vector multiplication " << endl;
cout << "The scalar product of " << v3 << " and " << v4 << " is "; cout << v3 * v4 << endl;
cout << " Test " << test++ << ": Scalar multiplication " << endl;
float k = 2.345;
cout << v3 << " * " << k << " = " << v3 * k << endl;
cout << k << " * " << v4 << " = " << k * v4 << endl;
cout << " Test " << test++ << ": Subscripting " << endl;
Vector3 v5(3.2, -5.4, 5.6); Vector3 v6(1.3, 2.4, -3.1);
cout << "v5: " << v5 << endl;
cout << "v6: " << v6 << endl;
cout << "v5[0] = " << v5[0] << endl;
cout << "v5[1] = " << v5[1] << endl;
cout << "v5[2] = " << v5[2] << endl;
v6[0] = -2.4; v6[1] = -1.3; v6[2] = 17.5;
cout << "v6: " << v6 << endl;
v6 = v5;
cout << "v6: " << v6 << endl;
cout << " Test " << test++ << ": Equality " << endl;
Vector3 v7(-1, 2, -1), v8(-1, 2, -2);
cout << v7 << " and " << v7 << " are ";
if (v7 == v7) cout << "equal" << endl;
else cout << "not equal" << endl;
cout << v7 << " and " << v8 << " are ";
if (v7 == v8)
cout << "equal" << endl;
else cout << "not equal" << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.