Skip to content
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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions homework01/rudolf/CMakeLists.txt
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)
46 changes: 46 additions & 0 deletions homework01/rudolf/vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "vector.h"

Vector::Vector()
{
mX=0;
Copy link
Owner

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;

mY=0;
}

Vector::Vector(double x, double y): mX(x), mY(y){}
Copy link
Owner

Choose a reason for hiding this comment

The 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

Copy link
Owner

Choose a reason for hiding this comment

The 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
Copy link
Owner

Choose a reason for hiding this comment

The 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 DotProduct though ;)

Copy link
Owner

Choose a reason for hiding this comment

The 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;
Copy link
Owner

Choose a reason for hiding this comment

The 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)
Copy link
Owner

Choose a reason for hiding this comment

The 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)
Copy link
Owner

Choose a reason for hiding this comment

The 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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 things: mixing mX and GetX is slightly disturbing, use one or the other. Also, you simply write:

return mX == v.mX && mY == v.mY;

}
31 changes: 31 additions & 0 deletions homework01/rudolf/vector.h
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually use #pragma once, it's shorter and you cannot have collisions on the name.


#include <iostream>
Copy link
Owner

Choose a reason for hiding this comment

The 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);
Copy link
Owner

Choose a reason for hiding this comment

The 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