summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--6p11.cpp22
-rw-r--r--6p12.cpp25
-rw-r--r--6p13.cpp16
-rw-r--r--6p14.cpp18
-rw-r--r--6p15.cpp35
5 files changed, 116 insertions, 0 deletions
diff --git a/6p11.cpp b/6p11.cpp
new file mode 100644
index 0000000..6e37076
--- /dev/null
+++ b/6p11.cpp
@@ -0,0 +1,22 @@
+#include <iostream>
+
+/*
+ *
+ * 6.11
+ *
+ *
+ */
+
+void resettozero(int &i) {
+
+ i = 0;
+}
+
+int main () {
+
+ int i = 2132;
+ std::cout << i << std::endl;
+ resettozero(i);
+ std::cout << i << std::endl;
+ return 0;
+}
diff --git a/6p12.cpp b/6p12.cpp
new file mode 100644
index 0000000..2453e76
--- /dev/null
+++ b/6p12.cpp
@@ -0,0 +1,25 @@
+#include <iostream>
+
+/*
+ *
+ * 6.12
+ *
+ *
+ */
+
+void intswapperREF(int &i1, int &i2) {
+
+ int tmp1 = i1;
+ i1 = i2;
+ i2 = tmp1;
+}
+
+int main () {
+
+ int i1 = 121212;
+ int i2 = 323;
+ std::cout << i1 << " " << i2 << std::endl;
+ intswapperREF(i1, i2);
+ std::cout << i1 << " " << i2 << std::endl;
+ return 0;
+}
diff --git a/6p13.cpp b/6p13.cpp
new file mode 100644
index 0000000..e6cc1e7
--- /dev/null
+++ b/6p13.cpp
@@ -0,0 +1,16 @@
+
+/*
+ *
+ * 6.13
+ *
+ *
+ */
+
+int main () {
+
+ //void f(T): It means that the variable is copied from the arguments passed to the function
+ //void f(T&): It means that the variable is a reference to the variable passed to the function.
+ // So basically, when we use the variable in the function it is directly a reference to the variable
+ // that we passed so that variable is changed rather than a copy
+ return 0;
+}
diff --git a/6p14.cpp b/6p14.cpp
new file mode 100644
index 0000000..f391697
--- /dev/null
+++ b/6p14.cpp
@@ -0,0 +1,18 @@
+
+/*
+ *
+ * 6.14
+ *
+ *
+ */
+
+int main () {
+
+ // It should be a reference when we just want read access to the object and we aren't changing it, we dont have to copy it
+ // It should be a reference when we want to change the object outside of the function
+ // It could also be a reference when the object is a type that holds a lot of data and maybe it matters a lot for performance
+ // If we want to return more information than just one object we could return information through a reference
+
+ // It should not be a reference when we want to change the object inside the function but not outside it
+ return 0;
+}
diff --git a/6p15.cpp b/6p15.cpp
new file mode 100644
index 0000000..be65157
--- /dev/null
+++ b/6p15.cpp
@@ -0,0 +1,35 @@
+
+/*
+ *
+ * 6.15
+ *
+ *
+ */
+
+int main () {
+
+ /*
+ Why is s a reference to const?
+ Because we just need read access to it, we are checking the size
+
+ Why is occurs a plain reference?
+ Because we want to return information through occurs so we need write access
+
+ Why is c not a reference?
+ 'c' is the character we are looking for. I suspect it's not a reference because
+ if we accidentally change c then we could have some serious bugs. It would probably
+ be fine if we did const reference to char but it really does not matter much.
+
+ What if we made s a plain reference?
+ I don't think it would matter much with the function as it is right now but it would
+ make it so if someone modifies the function, they could make a mistake and somehow
+ change the contents of 's'. Because we have const reference we ensure that we can only
+ read and not write.
+
+ What if we made occurs a reference to const?
+ The function would not work and perhaps not even compile. We would not be able to return
+ the information through the reference
+
+ */
+ return 0;
+}