need help on c++ program please here\'s code: // TestThreeDVec.cpp // // COSC103
ID: 3827530 • Letter: N
Question
need help on c++ program please
here's code:
// TestThreeDVec.cpp
//
// COSC1030, Sp 2017
// Program 13
//
// Test program to demonstrate and validate desired behavior
// of the new C++ class ThreeDVec .
//
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "ThreeDVec.h"
int main()
{
ThreeDVec A;
cout << "A is " << A << endl << endl;
ThreeDVec X(1,0,0), Y(0,1,0), Z(0,0,1);
cout << "X+Y+Z is " << X+Y+Z << endl << endl;
ThreeDVec B(3,4,5);
cout << "B is " << B << endl;
cout << "||B|| is " << B.mag() << endl;
cout << "B dot Y is " << B*Y << endl << endl;;
ThreeDVec C(X+Y+Z);
cout << "C is " << C << endl;
cout << "B cross C is " << (B^C) << endl;
cout << "B dot (B cross C) is " << B*(B^C) << endl << endl;
ThreeDVec D;
cout << "Enter your vector coefficients as x y z: ";
cin >> D;
cout << "D is " << D << " with magnitude " << D.mag() << endl;
ThreeDVec E=D*(1/D.mag()); // This is a scalar multiplying a vector
cout << "Normalized D is |" << E << "| = " << E.mag() << endl << endl;
return 0;
}
Explanation / Answer
//ThreeDVec.h file
#ifndef THREEDVEC_H_INCLUDED
#define THREEDVEC_H_INCLUDED
#include <iostream>
using namespace std;
class ThreeDVec
{
float x,y,z; //the three coordinates of the vector
public:
ThreeDVec(); //default constructor
ThreeDVec(float l,float m,float n); //constructor
//overload << & >> operators, these must be global and as they need to access private data members they mus also be friends
friend ostream & operator << (ostream &out,const ThreeDVec &t);
friend istream & operator >> (istream &in, ThreeDVec &t);
ThreeDVec operator + (ThreeDVec t); //add two vectors
ThreeDVec operator ^ (ThreeDVec t); //cross product of two vectors
double operator * (ThreeDVec t); //multiply two vectors and return a scalar
double mag(); //magnitude of the vector
ThreeDVec operator *(double k); //multiply the vector by the given scalar and return a vector
void operator = (ThreeDVec t); //overload the assignment operator
};
#endif // THREEDVEC_H_INCLUDED
//threeDVec.cpp file
#include <iostream>
#include <cmath>
#include "ThreeDVec.h"
ThreeDVec::ThreeDVec() //default constructor
{
x=y=z=0;
}
ThreeDVec::ThreeDVec(float l, float m,float n) //constructor
{
x=l;
y=m;
z=n;
}
double ThreeDVec::mag() //magnitude of the vector
{
double res=sqrt(x*x + y*y + z*z);
return res;
}
ThreeDVec ThreeDVec::operator *(double k) //multiply the vector by the given scalar and return a vector
{
ThreeDVec temp(x*k,y*k,z*k);
return temp;
}
double ThreeDVec::operator * (ThreeDVec t) //multiply two vectors and return a scalar
{
double res;
res=x*t.x + y*t.y + z*t.z;
return res;
}
ThreeDVec ThreeDVec::operator + (ThreeDVec t) //add two vectors
{
ThreeDVec res;
res.x=x + t.x;
res.y=y + t.y;
res.z=z + t.z;
return res;
}
ThreeDVec ThreeDVec::operator ^ (ThreeDVec t) //cross product of two vectors
{
ThreeDVec res;
res.x=y*t.z - z*t.y;
res.y=z*t.x - x*t.z;
res.z=x*t.y - y*t.x;
return res;
}
void ThreeDVec::operator=(ThreeDVec t) //overload assignment operator
{
x=t.x;
y=t.y;
z=t.z;
}
//overload << & >> operators, these must be global and as they need to access private data members they mus also be friends
istream & operator >> (istream &in, ThreeDVec &t)
{
in>>t.x;
in>>t.y;
in>>t.z;
return in;
}
ostream & operator << (ostream &out,const ThreeDVec &t)
{
out<<t.x;
out<<t.y;
out<<t.z;
return out;
}
//TestThreeDVec.cpp
// TestThreeDVec.cpp
//
// COSC1030, Sp 2017
// Program 13
//
// Test program to demonstrate and validate desired behavior
// of the new C++ class ThreeDVec .
//
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "ThreeDVec.h"
#include "ThreeDVec.cpp"
int main()
{
ThreeDVec A;
cout << "A is " << A << endl << endl;
ThreeDVec X(1,0,0), Y(0,1,0), Z(0,0,1);
cout << "X+Y+Z is " << X+Y+Z << endl << endl;
ThreeDVec B(3,4,5);
cout << "B is " << B << endl;
cout << "||B|| is " << B.mag() << endl;
cout << "B dot Y is " << B*Y << endl << endl;;
ThreeDVec C(X+Y+Z);
cout << "C is " << C << endl;
cout << "B cross C is " << (B^C) << endl;
cout << "B dot (B cross C) is " << B*(B^C) << endl << endl;
ThreeDVec D;
cout << "Enter your vector coefficients as x y z: ";
cin >> D;
cout << "D is " << D << " with magnitude " << D.mag() << endl;
ThreeDVec E=D*(1/D.mag()); // This is a scalar multiplying a vector
cout << "Normalized D is |" << E << "| = " << E.mag() << endl << endl;
return 0;
}
Hope this helps!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.