diff options
Diffstat (limited to '')
-rw-r--r-- | simple-classes.cpp | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/simple-classes.cpp b/simple-classes.cpp new file mode 100644 index 0000000..c21837b --- /dev/null +++ b/simple-classes.cpp @@ -0,0 +1,59 @@ +#include "simple-classes.hpp" + +#include <iostream> + + +// implementation for the default constructor +// math::vec3i::vec3i() { +// x = 0; +// y = 0; +// z = 0; +// } + +// here is a modern implementation that +// uses a C++11 feature called `member initializers' +math::vec3i::vec3i() : x(0), y(0), z(0) { + // does nothing special +} + +// implementation for the copy constructor, that +// copies the other object +math::vec3i::vec3i(const math::vec3i& other) { + // in this case we copy only the components + x = other.x; + y = other.y; + z = other.z; +} + +// impl for a custom constructor +math::vec3i::vec3i(int x_, int y_, int z_) { + x = x_; + y = y_; + z = z_; +} + + +void math::vec3i::zero() { + x = 0; + y = 0; + z = 0; +} + +int math::vec3i::dot(const vec3i& other) const { + return x * other.x + y * other.y + z * other.z; +} + + +int main(int argc, char *argv[]) { + + // a vector with zeroes + math::vec3i v; + // a copy of v + const math::vec3i u(v); + // a vector initialized + math::vec3i w(1, 2, 3); + + std::cout << u.dot(w) << std::endl; + + return 0; +} |