Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please provide C++ code for chapter 8 practice problem number 5: Write the defin

ID: 3657594 • Letter: P

Question

Please provide C++ code for chapter 8 practice problem number 5: Write the definition for a class named Vector2D that stores information about a two-dimensional vector. The class should have methods to get and set the x component and the y component, where x and y are integers. Next, overload the * operator so that it returns the dot product of two vectors. The dot product of two-dimensional vectors A and B is equal to (Ax * Bx) + (Ay * By) . Finally, write a main subroutine that tests the * operation.

Explanation / Answer

# include 002 # include 003 # include 004 005 006 class Vector2D 007 { 008 public: 009 Vector2D(void); 010 Vector2D(double nx, double ny); 011 Vector2D(bool v); 012 void normalize(void); 013 double magnitude(void); 014 void full_dump(void) const; 015 bool valid; 016 double x,y; 017 018 }; 019 020 double dot(Vector2D v, Vector2D w); 021 022 Vector2D operator+(Vector2D v, Vector2D w); 023 024 Vector2D operator*(Vector2D v, Vector2D w); 025 Vector2D operator*(Vector2D v, double w); 026 Vector2D operator*(double w, Vector2D v); 027 028 029 inline Vector2D::Vector2D(void) 030 { 031 x=y=0; 032 valid=true; 033 } 034 035 inline Vector2D::Vector2D(double nx, double ny) 036 { 037 x=nx; 038 y=ny; 039 valid=true; 040 } 041 042 inline Vector2D::Vector2D(bool v) 043 { 044 x=y=0; 045 valid=v; 046 } 047 048 049 inline Vector2D operator+(Vector2D v, Vector2D w) 050 { 051 return Vector2D( 052 v.x + w.x, 053 v.y + w.y 054 ); 055 } 056 057 058 059 inline Vector2D operator*(Vector2D v, Vector2D w) 060 { 061 return Vector2D( 062 v.x * w.x, 063 v.y * w.y 064 ); 065 } 066 067 inline Vector2D operator*(Vector2D v, double w) 068 { 069 return Vector2D( 070 v.x * w, 071 v.y * w 072 ); 073 } 074 075 inline Vector2D operator*(double w, Vector2D v) 076 { 077 return v*w; 078 } 079 080 inline Vector2D operator/(Vector2D v, Vector2D w) 081 { 082 return Vector2D ( 083 v.x / w.x, 084 v.y / w.y 085 ); 086 } 087 088 inline Vector2D operator/(Vector2D v, double w) 089 { 090 return Vector2D ( 091 v.x / w, 092 v.y / w 093 ); 094 } 095 096 inline Vector2D operator/(double w, Vector2D v) 097 { 098 return Vector2D ( 099 w / v.x, 100 w / v.y 101 ); 102 } 103 104 105 106 inline void Vector2D::normalize(void) 107 { 108 double mag=magnitude(); 109 x/=mag; 110 y/=mag; 111 } 112 113 114 inline double Vector2D::magnitude(void) 115 { 116 return sqrt(x*x+y*y); 117 } 118 119 120 121 inline Vector2D cross(Vector2D v, Vector2D w) 122 { 123 124 return Vector2D(0,0); 125 } 126 127 128 129 inline double dot(Vector2D v, Vector2D w) 130 { 131 Vector2D x=w*v; 132 return x.x + x.y; 133 }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote