From 05f2df34290af477b0fee49b75e5f56e1d6c83f9 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Sat, 8 Dec 2018 12:58:04 +0100 Subject: Initial commit with kinda crappy unnumbered examples --- simple-classes.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 simple-classes.cpp (limited to 'simple-classes.cpp') 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 + + +// 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; +} -- cgit v1.2.1