-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from FranchescaMullin/master
Franchesca homework (1 and 2)
- Loading branch information
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |