summaryrefslogtreecommitdiffstats
path: root/simple-classes.cpp
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-12-08 12:58:04 +0100
committerNao Pross <naopross@thearcway.org>2018-12-08 12:58:04 +0100
commit05f2df34290af477b0fee49b75e5f56e1d6c83f9 (patch)
tree66a7214321c874be02d1a4a9b4fd3f489dc09ad8 /simple-classes.cpp
downloadcplusplus-05f2df34290af477b0fee49b75e5f56e1d6c83f9.tar.gz
cplusplus-05f2df34290af477b0fee49b75e5f56e1d6c83f9.zip
Initial commit with kinda crappy unnumbered examples
Diffstat (limited to '')
-rw-r--r--simple-classes.cpp59
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;
+}