Skip to content

Commit

Permalink
Merge pull request #1 from FranchescaMullin/master
Browse files Browse the repository at this point in the history
Franchesca homework (1 and 2)
  • Loading branch information
david-grs authored Aug 3, 2018
2 parents 8b15c94 + 4eae220 commit f7128c7
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
3 changes: 3 additions & 0 deletions homework01/fran/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++14")

add_executable(vector vector.h vector.cc vector_tests.cc)
24 changes: 24 additions & 0 deletions homework01/fran/vector.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "vector.h"

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

Vector::Vector(int x, int y):
mX(x),
mY(y)
{}

const int Vector::GetX()
{
return mX;
}

const int Vector::GetY()
{
return mY;
}

void Vector::Add(const Vector& vector)
{
mX += vector.mX;
mY += vector.mY;
}
39 changes: 39 additions & 0 deletions homework01/fran/vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>

class Vector
{
public:
Vector();
Vector(int x, int y);

bool operator==(const Vector& rhs) const
{
return mX == rhs.mX && mY == rhs.mY;
}

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

Vector& operator+=(const Vector& rhs)
{
mX += rhs.mX;
mY += rhs.mY;
return *this;
}

friend std::ostream& operator<<(std::ostream& stream, const Vector& rhs)
{
stream << "x:" << rhs.mX << " y:" << rhs.mY;
return stream;
}

const int GetX();
const int GetY();
void Add(const Vector&);

private:
int mX;
int mY;
};
38 changes: 38 additions & 0 deletions homework01/fran/vector_tests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "vector.h"

#include <iostream>
#include <cassert>

int main()
{
#ifdef NDEBUG
#error Compile the code in debug mode!
#endif

Vector v1;
Vector v2(5, 6);

assert(v2.GetX() == 5);
assert(v2.GetY() == 6);

v1 = v2;
assert(v1.GetX() == 5);
assert(v1.GetY() == 6);
assert(v1 == v2);

Vector v3(1, 2);
v1.Add(v3);
assert(v1.GetX() == 6);
assert(v1.GetY() == 8);

v3 = v2 + Vector(1, 1);
assert(v3.GetX() == 6);
assert(v3.GetY() == 7);

v3 += v3;
assert(v3.GetX() == 12);
assert(v3.GetY() == 14);

v3 = v1 + v2;
std::cout << v1 << " + " << v2 << " = " << v3 << std::endl;
}

0 comments on commit f7128c7

Please sign in to comment.