Question: C++ Vector Class Design a 2D vector class called Vec.h (Note, this is not meant to represent the data structure vector, but the mathematical concept
C++
Vector Class
Design a 2D vector class called "Vec.h" (Note, this is not meant to represent the data structure vector, but the mathematical concept of vector used for, among other things, Cartesian coordinates.)
Your class Vec must contain the following: - two float data members called x and y - a default constructor (initialize variables to 0) - a constructor from two floats - a "set" method which takes two floats - an "add" method, which takes another Vec and adds its x and y to the local instance's x and y - a "print" method which prints out the values of x and y
Your Vec class must allow the vectors.cpp file to run without generating any error messages.
THE OUTPUT YOU ARE LOOKING FOR: Test 1 passed! Test 2 passed! (10, 20) Test 3 passed! Finished!
vectors.cpp
#include
#include "Vec.h"
using namespace std;
Vec nullvec( 0.0f, 0.0f );
bool equal( const Vec & u, const Vec& v )
{
return u.x == v.x && u.y == v.y;
}
int main( int argc, const char* argv[] )
{
Vec u;
Vec v;
Vec w( 1.0f, 2.0f );
if( equal( u, nullvec ) == true ) cout << "Test 1 passed! ";
else cout << "error1 ";
v.set( 1.0f, 2.0f );
if( equal( v, w ) == true ) cout << "Test 2 passed! ";
else cout << "error2 ";
for( int i = 0; i < 10; i++ )
u.add( w );
u.print();
if( equal( u, Vec( 10.0f, 20.0f ) ) == true ) cout << "Test 3 passed! ";
else cout << "error3 ";
cout << "Finished! ";
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
