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

Franchesca homework (1 and 2) #1

Merged
merged 4 commits into from
Aug 3, 2018

Conversation

FranchescaMullin
Copy link
Contributor

No description provided.

Vector::Vector()
{
mX = 0;
mY = 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, even though in this case it would result into the exact same machine code.

Vector::Vector(int x, int 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.

I think it was a good choice to not use default values for parameters - what is even better is to call this constructor from the previous one, see delegating constructors: https://en.cppreference.com/w/cpp/language/initializer_list#Delegating_constructor

mY = y;
}

int Vector::GetX()
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 does not modify the object state, it should be const

return mX;
}

int Vector::GetY()
Copy link
Owner

Choose a reason for hiding this comment

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

Same about this one

return mY;
}

void Vector::Add(Vector& vector)
Copy link
Owner

Choose a reason for hiding this comment

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

vector is not modified: it should be const Vector&

class Vector
{
public:
Vector();
Copy link
Owner

Choose a reason for hiding this comment

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

The indentation mixes tabs and spaces

@FranchescaMullin FranchescaMullin changed the title homework 01 Franchesca homework (1 and 2) Jul 30, 2018
Copy link
Owner

@david-grs david-grs left a comment

Choose a reason for hiding this comment

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

I approve it, but please fix the few things I commented on!

mY(y)
{}

const int Vector::GetX()
Copy link
Owner

Choose a reason for hiding this comment

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

The method should be const, not the returned value. We will discuss about this Thursday.

@@ -0,0 +1,24 @@
#include "vector.h"

Vector::Vector(): Vector(0, 0){}
Copy link
Owner

Choose a reason for hiding this comment

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

Optiver's style would be to write

Vector::Vector() :
    Vector(0, 0)
{}

{
mX += rhs.mX;
mY += rhs.mY;
return *this;
Copy link
Owner

Choose a reason for hiding this comment

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

Rule of thumb: only one liners in header files,the rest goes to the cc!

@@ -0,0 +1,39 @@
#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.

Is this needed?

Vector operator+(const Vector& rhs) const
{
return Vector(mX + rhs.mX, mY + rhs.mY);
}
Copy link
Owner

Choose a reason for hiding this comment

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

This perfectly works. Did you consider this alternative (free function)?

Vector operator+(Vector lhs, const Vector& rhs)
{
    lhs += rhs;
    return lhs;
}

return *this;
}

friend std::ostream& operator<<(std::ostream& stream, const Vector& rhs)
Copy link
Owner

Choose a reason for hiding this comment

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

Does it need to be friend? Wouldn't you be able to write this code without?

david-grs pushed a commit that referenced this pull request Aug 3, 2018
@david-grs david-grs merged commit f7128c7 into david-grs:master Aug 3, 2018
david-grs pushed a commit that referenced this pull request Nov 19, 2018
david-grs pushed a commit that referenced this pull request Sep 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants