Skip to content

Small refinements for the OO chapter #551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
package-lock.json
package.json
node_modules/**

# Temporary latex files:
talk/C++Course.*
talk/_minted
3 changes: 1 addition & 2 deletions talk/objectorientation/advancedoo.tex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions talk/objectorientation/allocations.tex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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}
Expand Down
41 changes: 32 additions & 9 deletions talk/objectorientation/constructors.tex
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -124,6 +145,8 @@
}
Vector::~Vector() { delete[] data; }
\end{cppcode}

\end{overprint}
\end{frame}

\begin{frame}[fragile]
Expand Down Expand Up @@ -203,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) {}
Expand All @@ -224,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;
Expand All @@ -247,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};
Expand All @@ -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);
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions talk/objectorientation/operators.tex
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}
};

Expand Down Expand Up @@ -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};
Expand All @@ -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}
Expand Down Expand Up @@ -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}
Expand Down