aboutsummaryrefslogtreecommitdiff
path: root/cpp07
diff options
context:
space:
mode:
Diffstat (limited to 'cpp07')
-rw-r--r--cpp07/ex00/main.cpp28
-rw-r--r--cpp07/ex00/whatever.hpp25
-rw-r--r--cpp07/ex01/iter.hpp35
-rw-r--r--cpp07/ex01/main.cpp40
-rw-r--r--cpp07/ex02/main.cpp32
5 files changed, 143 insertions, 17 deletions
diff --git a/cpp07/ex00/main.cpp b/cpp07/ex00/main.cpp
index 1c04faf..26ec956 100644
--- a/cpp07/ex00/main.cpp
+++ b/cpp07/ex00/main.cpp
@@ -6,7 +6,7 @@
/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/14 14:41:39 by cacharle #+# #+# */
-/* Updated: 2020/12/14 15:05:32 by cacharle ### ########.fr */
+/* Updated: 2020/12/15 11:10:07 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,6 +14,23 @@
#include <string>
#include "whatever.hpp"
+NotAnInt::NotAnInt() : m_n(0) {}
+NotAnInt::NotAnInt(int n) : m_n(n) {}
+NotAnInt::NotAnInt(NotAnInt const& other) : m_n(other.m_n) {}
+NotAnInt& NotAnInt::operator=(NotAnInt const& other) { m_n = other.m_n; return *this; }
+
+bool NotAnInt::operator==(NotAnInt const& other) const { return m_n == other.m_n; }
+bool NotAnInt::operator!=(NotAnInt const& other) const { return m_n != other.m_n; }
+bool NotAnInt::operator>(NotAnInt const& other) const { return m_n > other.m_n; }
+bool NotAnInt::operator<(NotAnInt const& other) const { return m_n < other.m_n; }
+bool NotAnInt::operator>=(NotAnInt const& other) const { return m_n >= other.m_n; }
+bool NotAnInt::operator<=(NotAnInt const& other) const { return m_n <= other.m_n; }
+
+int NotAnInt::getN() const { return m_n; }
+
+std::ostream& operator<<(std::ostream& out, NotAnInt const& n) { out << n.getN(); return out; }
+
+
int main()
{
{
@@ -41,6 +58,8 @@ int main()
float bf = 4.5f;
char ac = 'a';
char bc = 'b';
+ NotAnInt ai(34);
+ NotAnInt bi(43);
std::cout << "=================== SWAP ================" << std::endl;
::swap(af, bf);
std::cout << "af = " << af << ", bf = " << bf << std::endl;
@@ -48,6 +67,8 @@ int main()
std::cout << "ad = " << ad << ", bd = " << bd << std::endl;
::swap(ac, bc);
std::cout << "ac = " << ac << ", bc = " << bc << std::endl;
+ ::swap(ai, bi);
+ std::cout << "ai = " << ai << ", bi = " << bi << std::endl;
}
std::cout << std::endl;
@@ -58,7 +79,8 @@ int main()
float bf = 4.5f;
char ac = 'a';
char bc = 'b';
-
+ NotAnInt ai(54);
+ NotAnInt bi(53);
int same1 = 4;
int same2 = 4;
@@ -67,6 +89,7 @@ int main()
std::cout << "min(af, bf) = " << ::min(af, bf) << std::endl;
std::cout << "min(ad, bd) = " << ::min(ad, bd) << std::endl;
std::cout << "min(ac, bc) = " << ::min(ac, bc) << std::endl;
+ std::cout << "min(ai, bi) = " << ::min(ai, bi) << std::endl;
int &minsame = ::min(same1, same2);
std::cout << "min(same1, same2) = " << minsame << ", address same2 = " << &same2 << ", address minsame = " << &minsame << std::endl;
@@ -76,6 +99,7 @@ int main()
std::cout << "max(af, bf) = " << ::max(af, bf) << std::endl;
std::cout << "max(ad, bd) = " << ::max(ad, bd) << std::endl;
std::cout << "max(ac, bc) = " << ::max(ac, bc) << std::endl;
+ std::cout << "max(ai, bi) = " << ::max(ai, bi) << std::endl;
int &maxsame = ::max(same1, same2);
std::cout << "max(same1, same2) = " << maxsame << ", address same2 = " << &same2 << ", address maxsame = " << &maxsame << std::endl;
diff --git a/cpp07/ex00/whatever.hpp b/cpp07/ex00/whatever.hpp
index 8768a44..53953f8 100644
--- a/cpp07/ex00/whatever.hpp
+++ b/cpp07/ex00/whatever.hpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/14 19:46:56 by charles #+# #+# */
-/* Updated: 2020/12/14 15:05:32 by cacharle ### ########.fr */
+/* Updated: 2020/12/15 11:08:51 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -33,4 +33,27 @@ T& max(T& a, T& b)
return a > b ? a : b;
}
+class NotAnInt
+{
+public:
+ NotAnInt();
+ NotAnInt(int n);
+ NotAnInt(NotAnInt const& other);
+ NotAnInt& operator=(NotAnInt const& other);
+
+ bool operator==(NotAnInt const& other) const;
+ bool operator!=(NotAnInt const& other) const;
+ bool operator>(NotAnInt const& other) const;
+ bool operator<(NotAnInt const& other) const;
+ bool operator>=(NotAnInt const& other) const;
+ bool operator<=(NotAnInt const& other) const;
+
+ int getN() const;
+
+private:
+ int m_n;
+};
+
+std::ostream& operator<<(std::ostream& out, NotAnInt const& n);
+
#endif
diff --git a/cpp07/ex01/iter.hpp b/cpp07/ex01/iter.hpp
index df1a37a..8118060 100644
--- a/cpp07/ex01/iter.hpp
+++ b/cpp07/ex01/iter.hpp
@@ -6,7 +6,7 @@
/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/14 15:06:48 by cacharle #+# #+# */
-/* Updated: 2020/12/14 15:18:09 by cacharle ### ########.fr */
+/* Updated: 2020/12/15 11:23:16 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,9 +23,42 @@ void iter(T* ptr, size_t len, void (*f)(T const& x))
}
template<typename T>
+void iter(T* ptr, size_t len, void (*f)(T& x))
+{
+ for (size_t i = 0; i < len; i++)
+ f(ptr[i]);
+}
+
+template<typename T>
+void iter(T* ptr, size_t len, void (*f)(T x))
+{
+ for (size_t i = 0; i < len; i++)
+ f(ptr[i]);
+}
+
+template<typename T>
void timeTwo(T const& x)
{
std::cout << "timeTwo function " << x << " * 2 = " << x * 2 << std::endl;
}
+template<typename T>
+void timeThree(T& x)
+{
+ std::cout << "timeThree function " << x << " * 3 = " << x * 3 << std::endl;
+}
+
+class NotAnInt
+{
+public:
+ NotAnInt();
+ NotAnInt(int n);
+ NotAnInt(NotAnInt const& other);
+ NotAnInt& operator=(NotAnInt const& other);
+ int getN() const;
+
+private:
+ int m_n;
+};
+
#endif
diff --git a/cpp07/ex01/main.cpp b/cpp07/ex01/main.cpp
index 6ac8e82..3815f77 100644
--- a/cpp07/ex01/main.cpp
+++ b/cpp07/ex01/main.cpp
@@ -6,13 +6,29 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/14 20:02:19 by charles #+# #+# */
-/* Updated: 2020/12/14 15:16:32 by cacharle ### ########.fr */
+/* Updated: 2020/12/15 11:26:57 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#include "iter.hpp"
+NotAnInt::NotAnInt() : m_n(0) {}
+NotAnInt::NotAnInt(int n) : m_n(n) {}
+NotAnInt::NotAnInt(NotAnInt const& other) : m_n(other.m_n) {}
+NotAnInt& NotAnInt::operator=(NotAnInt const& other) { m_n = other.m_n; return *this; }
+int NotAnInt::getN() const { return m_n; }
+
+void intOnly(int n)
+{
+ std::cout << "intOnly() " << n << std::endl;
+}
+
+void notAnIntOnly(NotAnInt& n)
+{
+ std::cout << "notAnIntOnly() " << n.getN() << std::endl;
+}
+
int main()
{
int intArray[] = {1, 2, 3, 4, 10, 20, 30, -1, -2};
@@ -21,6 +37,8 @@ int main()
std::cout << intArray[i] << ", ";
std::cout << std::endl;
iter(intArray, intArraySize, timeTwo<int>);
+ iter(intArray, intArraySize, timeThree<int>);
+ iter(intArray, intArraySize, intOnly);
std::cout << "--------------------------------------" << std::endl;
@@ -30,6 +48,7 @@ int main()
std::cout << floatArray[i] << ", ";
std::cout << std::endl;
iter(floatArray, floatArraySize, timeTwo<float>);
+ iter(floatArray, floatArraySize, timeThree<float>);
std::cout << "--------------------------------------" << std::endl;
@@ -39,6 +58,25 @@ int main()
std::cout << uintArray[i] << ", ";
std::cout << std::endl;
iter(uintArray, uintArraySize, timeTwo<unsigned int>);
+ iter(uintArray, uintArraySize, timeThree<unsigned int>);
+ std::cout << "--------------------------------------" << std::endl;
+
+ const int cintArray[] = {1, 2, 3, 4};
+ size_t cintArraySize = sizeof(cintArray) / sizeof(const int);
+ for (size_t i = 0; i < cintArraySize; i++)
+ std::cout << cintArray[i] << ", ";
+ std::cout << std::endl;
+ iter(cintArray, cintArraySize, timeTwo<const int>);
+ iter(cintArray, cintArraySize, timeThree<const int>);
+
+ std::cout << "--------------------------------------" << std::endl;
+
+ NotAnInt nintArray[] = {NotAnInt(1), NotAnInt(2), NotAnInt(3), NotAnInt(4)};
+ size_t nintArraySize = sizeof(nintArray) / sizeof(NotAnInt);
+ for (size_t i = 0; i < nintArraySize; i++)
+ std::cout << nintArray[i].getN() << ", ";
+ std::cout << std::endl;
+ iter(nintArray, nintArraySize, notAnIntOnly);
return 0;
}
diff --git a/cpp07/ex02/main.cpp b/cpp07/ex02/main.cpp
index 2f53f00..58a698a 100644
--- a/cpp07/ex02/main.cpp
+++ b/cpp07/ex02/main.cpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/14 20:25:44 by charles #+# #+# */
-/* Updated: 2020/12/14 15:29:50 by cacharle ### ########.fr */
+/* Updated: 2020/12/15 11:38:16 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,47 +15,55 @@
int main()
{
+ std::cout << "==================== DEFAULT" << std::endl;
Array<int> a;
std::cout << a.size() << std::endl;
try { std::cout << a[0] << std::endl; }
catch (std::exception e) { std::cout << e.what() << std::endl; }
- std::cout << "------------------------" << std::endl;
+ std::cout << "==================== LENGTH" << std::endl;
Array<int> b(3);
std::cout << b.size() << std::endl;
std::cout << b[0] << ", " << b[1] << ", " << b[2] << std::endl;
b[0] = 1;
b[1] = 2;
b[2] = 3;
+ int const& ref1 = b[0];
+ (void)ref1;
std::cout << b[0] << ", " << b[1] << ", " << b[2] << std::endl;
try { std::cout << b[3] << std::endl; }
catch (std::exception e) { std::cout << e.what() << std::endl; }
- std::cout << "------------------------" << std::endl;
+ std::cout << "==================== LENGTH" << std::endl;
Array<float> c(3);
std::cout << c.size() << std::endl;
+ std::cout << c[0] << ", " << c[1] << ", " << c[2] << std::endl;
c[0] = 1.3;
c[1] = 2.2;
c[2] = 3.1;
+ float const& ref2 = c[0];
+ (void)ref2;
std::cout << c[0] << ", " << c[1] << ", " << c[2] << std::endl;
try { std::cout << c[3] << std::endl; }
catch (std::exception e) { std::cout << e.what() << std::endl; }
- std::cout << "------------------------" << std::endl;
+ std::cout << "==================== COPY" << std::endl;
Array<float> d(c);
std::cout << d.size() << std::endl;
d[0] = 10.12;
- std::cout << d[0] << " <> " << c[0] << std::endl;
- std::cout << d[1] << " <> " << c[1] << std::endl;
- std::cout << d[2] << " <> " << c[2] << std::endl;
- std::cout << "------------------------" << std::endl;
+ float const& ref3 = d[0];
+ (void)ref3;
+ std::cout << d[0] << " | " << c[0] << std::endl;
+ std::cout << d[1] << " | " << c[1] << std::endl;
+ std::cout << d[2] << " | " << c[2] << std::endl;
- Array<float> f;
+ std::cout << "==================== ASSIGNMENT" << std::endl;
+ Array<float> f(10);
std::cout << f.size() << std::endl;
f = c;
std::cout << f.size() << std::endl;
f[0] = 10.12;
- std::cout << f[0] << " <> " << c[0] << std::endl;
- std::cout << f[1] << " <> " << c[1] << std::endl;
- std::cout << f[2] << " <> " << c[2] << std::endl;
+ std::cout << f[0] << " | " << c[0] << std::endl;
+ std::cout << f[1] << " | " << c[1] << std::endl;
+ std::cout << f[2] << " | " << c[2] << std::endl;
}