-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding completed Vector files #3
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -std=c++11") | ||
|
||
add_executable(vector vector.h vector.cpp vector_tests.cc) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "vector.h" | ||
|
||
Vector::Vector() | ||
{ | ||
mX=0; | ||
mY=0; | ||
} | ||
|
||
Vector::Vector(double x, double y): mX(x), mY(y){} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could call this constructor from the other one, see delgating constructor: https://en.cppreference.com/w/cpp/language/initializer_list#Delegating_constructor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style: we would write this: Vector::Vector(double x, double y) :
mX(x),
mY(y)
{} |
||
|
||
double Vector::dotProduct(const Vector& vec) const | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good to see the argument passed as const ref and the constness of this method! It should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also: in general we like writing as little code as possible - the best code is the one that does not exist. |
||
{ | ||
return mX * vec.mX + mY * vec.mY ; | ||
} | ||
|
||
double Vector::GetX() const | ||
{ | ||
return mX; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would usually define those kind of methods (getters, one liners) in the header file, but it's not wrong to do it like that. |
||
} | ||
|
||
double Vector::GetY() const | ||
{ | ||
return mY; | ||
} | ||
|
||
void Vector::Add(const Vector& v) | ||
{ | ||
mX=mX + v.GetX(); | ||
mY=mY + v.GetY(); | ||
} | ||
|
||
// Overload = operator. | ||
void Vector::operator=(const Vector& v) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? Is it needed? See my comment above about the best code :) |
||
{ | ||
this->mX =v.GetX(); | ||
this->mY =v.GetY(); | ||
} | ||
// Overload == operator. | ||
bool Vector::operator==(const Vector& v) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method should be const |
||
{ | ||
if (mX ==v.GetX() && mY == v.GetY()); | ||
{ | ||
return true; | ||
} | ||
return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2 things: mixing return mX == v.mX && mY == v.mY; |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef __VECTOR_H_INCLUDED__ // if x.h hasn't been included yet... | ||
#define __VECTOR_H_INCLUDED__ // #define this so the compiler knows it has been included | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We usually use |
||
|
||
#include <iostream> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this include, this Vector class does not depend on anything from it. |
||
|
||
class Vector | ||
{ | ||
public: | ||
Vector(); | ||
|
||
Vector(double x, double y); | ||
|
||
double dotProduct(const Vector& vec) const; | ||
|
||
double GetX() const; | ||
double GetY() const; | ||
|
||
void Add(const Vector& v); | ||
|
||
// Overload = operator. | ||
void operator=(const Vector& v); | ||
// Overload == operator. | ||
bool operator==(const Vector& v); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's time to spend some time in the indentation settings of your favorite IDE :) |
||
|
||
private: | ||
double mX; | ||
double mY; | ||
}; | ||
|
||
#endif | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed Thursday, it's good practice to use initializer list. About the style, our (Optiver) style is
mX = 0;