CartVec.h file #ifndef CARTVEC_H #define CARTVEC_H class CartVec { public: // co
ID: 3825020 • Letter: C
Question
CartVec.h file
#ifndef CARTVEC_H
#define CARTVEC_H
class CartVec
{
public:
// constructors...don't worry about writing any code for these two constructors
CartVec(){};
CartVec( double newX, double newY ){ set( newX, newY ); }
// get functions
double getX(); // return the y value
double getY(); // return the x value
double mag(); // get the magnitude of the vector
double angle(); // get the angle of the vector
void set( double newX, double newY ); // set the x and y values
CartVec add( const CartVec &otherCartVec ); // add two vectors. DO NOT modify either of them.
void print(); // pretty print this vector
// ( [xval], [yval] ) --> [mag] < [angle]
protected:
private:
double m_x;
double m_y;
};
#endif // CARTVEC_H
Explanation / Answer
/*
* CartVec.cpp
*
* Created on: Apr 25, 2017
* Author: Ravi
*/
#include <iostream>
#include <math.h>
using namespace std;
#include "CartVec.h"
double CartVec::getX() {
// TODO Auto-generated constructor stub
return this->m_x;
}
double CartVec::getY() {
// TODO Auto-generated constructor stub
return this->m_y;
}
double CartVec::mag() {
// TODO Auto-generated constructor stub
return sqrt(this->m_x *this->m_x+ this->m_y *this->m_y) ;
}
double CartVec::angle() {
// TODO Auto-generated constructor stub
return tanh(this->m_x/this->m_y);
}
void CartVec::set( double newX, double newY ) {
// TODO Auto-generated constructor stub
this->m_x = newX;
this->m_y = newY;
}
CartVec CartVec::add( const CartVec &otherCartVec ) {
// TODO Auto-generated constructor stub
CartVec vec;
vec.set(this->m_x+otherCartVec.m_x, this->m_y+otherCartVec.m_y);
return vec;
}
void CartVec::print( ) {
cout << "(" << this->m_x << "," << this->m_y << ") -->" << this->mag() << "<" <<this->angle() << endl;
}
int main() {
CartVec vec1 = CartVec(10,15);
CartVec vec2 = CartVec(20,10);
cout << "Vec 1 :" <<endl;
vec1.print();
cout << "Vec 2 :" <<endl;
vec2.print();
cout << "Sum of two vectors- Vec 3 :" <<endl;
CartVec vec3 = vec1.add(vec2);
vec3.print();
}
---output---
DebugChegg-cpp-current.exe
Vec 1 :
(10,15) -->18.0278<0.582783
Vec 2 :
(20,10) -->22.3607<0.964028
Sum of two vectors- Vec 3 :
(30,25) -->39.0512<0.833655
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.