From 888e1256a6dd3bca5ae1833bf26c64bc4926a1a1 Mon Sep 17 00:00:00 2001 From: Stephan Hageboeck Date: Tue, 11 Mar 2025 14:42:12 +0100 Subject: [PATCH 1/3] Small refinements for objectorientation chapter. --- talk/objectorientation/advancedoo.tex | 3 +-- talk/objectorientation/allocations.tex | 4 ++-- talk/objectorientation/constructors.tex | 31 +++++++++++++++++++++---- talk/objectorientation/operators.tex | 10 ++++---- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/talk/objectorientation/advancedoo.tex b/talk/objectorientation/advancedoo.tex index a887ca58..420fcd59 100644 --- a/talk/objectorientation/advancedoo.tex +++ b/talk/objectorientation/advancedoo.tex @@ -244,8 +244,7 @@ \frametitlecpp[11]{{\texttt override} keyword} \begin{block}{Principle} \begin{itemize} - \item when overriding a virtual method - \item the \cppinline|override| keyword should be used + \item when overriding a virtual method, the \cppinline|override| keyword should be used \item the \cppinline|virtual| keyword is then optional \end{itemize} \end{block} diff --git a/talk/objectorientation/allocations.tex b/talk/objectorientation/allocations.tex index 8b16db6d..94d267ca 100644 --- a/talk/objectorientation/allocations.tex +++ b/talk/objectorientation/allocations.tex @@ -37,7 +37,7 @@ \item each thread in a process has its own stack \begin{itemize} \item allocations on the stack are thus ``thread private'' - \item and do not introduce any thread safety issues + \item and do not introduce any thread-safety issues \end{itemize} \end{itemize} \end{block} @@ -77,7 +77,7 @@ \item there is a single, shared heap per process \begin{itemize} \item allows to share data between threads - \item introduces race conditions and thread safety issues! + \item introduces race conditions and thread-safety issues! \end{itemize} \end{itemize} \end{block} diff --git a/talk/objectorientation/constructors.tex b/talk/objectorientation/constructors.tex index 6e210f63..ef135640 100644 --- a/talk/objectorientation/constructors.tex +++ b/talk/objectorientation/constructors.tex @@ -106,6 +106,27 @@ \begin{frame}[fragile] \frametitlecpp[98]{Class Constructors and Destructors} + \begin{overprint} + \onslide<1> + \begin{cppcode} + class Vector { + public: + Vector(int n); + Vector(const Vector &other); + ~Vector(); + private: + int len; int* data; + }; + Vector::Vector(int n) : len(n) { + data = new int[n]; + } + + + + + Vector::~Vector() { delete[] data; } + \end{cppcode} + \onslide<2> \begin{cppcode} class Vector { public: @@ -124,6 +145,8 @@ } Vector::~Vector() { delete[] data; } \end{cppcode} + + \end{overprint} \end{frame} \begin{frame}[fragile] @@ -266,8 +289,8 @@ \begin{block}{After object declaration, arguments within \{\}} \begin{cppcode*}{} struct A { - int a; - float b; + int i; + float f; A(); A(int); A(int, int); @@ -287,8 +310,8 @@ \begin{block}{Arguments are given within (), aka \cpp98 nightmare} \begin{cppcode*}{} struct A { - int a; - float b; + int i; + float f; A(); A(int); A(int, int); diff --git a/talk/objectorientation/operators.tex b/talk/objectorientation/operators.tex index 94d11b98..c1d7a00b 100644 --- a/talk/objectorientation/operators.tex +++ b/talk/objectorientation/operators.tex @@ -7,8 +7,8 @@ float m_real, m_imaginary; Complex(float real, float imaginary); Complex operator+(const Complex& other) { - return Complex(m_real + other.m_real, - m_imaginary + other.m_imaginary); + return Complex{m_real + other.m_real, + m_imaginary + other.m_imaginary}; } }; @@ -57,7 +57,7 @@ struct Complex { float m_real, m_imaginary; Complex operator+(float other) { - return Complex(m_real + other, m_imaginary); + return Complex{m_real + other, m_imaginary}; } }; Complex c1{2.f, 3.f}; @@ -67,7 +67,7 @@ \pause \begin{cppcode*}{firstnumber=10} Complex operator+(float a, const Complex& obj) { - return Complex(a + obj.m_real, obj.m_imaginary); + return Complex{a + obj.m_real, obj.m_imaginary}; } \end{cppcode*} \end{block} @@ -102,7 +102,7 @@ \item They gain access to all private/protected members \item Useful for operators such as \cppinline{a + b} \item Don't abuse friends to go around a wrongly designed interface - \item Avoid unexpected modifications of class state in a friend + \item Avoid unexpected modifications of class state in a friend! \end{itemize} \end{block} \begin{exampleblock}{\texttt{operator+} as a friend} From 0e6c7a82079d8275fb02b616b01506d622876cca Mon Sep 17 00:00:00 2001 From: Stephan Hageboeck Date: Tue, 11 Mar 2025 14:42:38 +0100 Subject: [PATCH 2/3] Add "explicit" keyword for several unary constructors. We recommend to use explicit for unary constructors, but we are not using is in the following slides. Here, it is added in several places where the addition doesn't create space issues on the slide. --- talk/objectorientation/constructors.tex | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/talk/objectorientation/constructors.tex b/talk/objectorientation/constructors.tex index ef135640..ef990ecc 100644 --- a/talk/objectorientation/constructors.tex +++ b/talk/objectorientation/constructors.tex @@ -226,7 +226,7 @@ \begin{cppcode} struct Delegate { int m_i; - Delegate(int i) : m_i(i) { + explicit Delegate(int i) : m_i(i) { ... complex initialization ... } Delegate() : Delegate(42) {} @@ -247,7 +247,7 @@ \begin{exampleblock}{Practically} \begin{cppcode} struct Base { - Base(int a); // ctor 1 + explicit Base(int a); // ctor 1 }; struct Derived : Base { using Base::Base; @@ -270,9 +270,9 @@ \begin{exampleblock}{Practically} \begin{cppcode} struct Base { - int a{5}; // also possible: int a = 5; - Base() = default; - Base(int _a) : a(_a) {} + int a{5}; // or: int a = 5; + Base() = default; + explicit Base(int _a) : a(_a) {} }; struct Derived : Base { int b{6}; From 5ae76b9a18bb77d98ea8be103166a154b3a1fec9 Mon Sep 17 00:00:00 2001 From: Stephan Hageboeck Date: Tue, 11 Mar 2025 14:47:10 +0100 Subject: [PATCH 3/3] Add latex helper files to gitignore. --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 22b9f4ba..c32d5754 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,7 @@ package-lock.json package.json node_modules/** + +# Temporary latex files: +talk/C++Course.* +talk/_minted