forked from hsf-training/cpluspluscourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators.tex
135 lines (126 loc) · 4.27 KB
/
operators.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
\subsection[Op]{Operator overloading}
\begin{frame}[fragile]
\frametitlecpp[98]{Operator overloading example}
\begin{cppcode}
struct Complex {
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);
}
};
Complex c1{2, 3}, c2{4, 5};
Complex c3 = c1 + c2; // (6, 8)
\end{cppcode}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{Operator overloading}
\begin{block}{Defining operators for a class}
\begin{itemize}
\item implemented as a regular method
\begin{itemize}
\item \small either inside the class, as a member function
\item or outside the class (not all)
\end{itemize}
\item with a special name (replace @ by operators from below)\small
\end{itemize}
\begin{tabular}{llll}
Expression & As member & As non-member \\
\hline
@a & (a).operator@() & operator@(a) \\
a@b & (a).operator@(b) & operator@(a,b) \\
a=b & (a).operator=(b) & cannot be non-member \\
a(b...) & (a).operator()(b...) & cannot be non-member \\
a[b] & (a).operator[](b) & cannot be non-member \\
a-\textgreater & (a).operator-\textgreater() & cannot be non-member \\
a@ & (a).operator@(0) & operator@(a,0) \\
\hline
\end{tabular}
\small
\begin{cppcode*}{linenos=false}
possible operators: + - * / % ^ & | ~ ! = < >
+= -= *= /= %= ^= &= |= << >> >>= <<=
== != <= >= <=> && || ++ -- , ->* -> () []
\end{cppcode*}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{Why have non-member operators?}
\begin{block}{Symmetry}
\begin{cppcode}
struct Complex {
float m_real, m_imaginary;
Complex operator+(float other) {
return Complex(m_real + other, m_imaginary);
}
};
Complex c1{2.f, 3.f};
Complex c2 = c1 + 4.f; // ok
Complex c3 = 4.f + c1; // not ok !!
\end{cppcode}
\pause
\begin{cppcode*}{firstnumber=10}
Complex operator+(float a, const Complex& obj) {
return Complex(a + obj.m_real, obj.m_imaginary);
}
\end{cppcode*}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{Other reason to have non-member operators?}
\begin{block}{Extending existing classes}
\begin{cppcode}
struct Complex {
float m_real, m_imaginary;
Complex(float real, float imaginary);
};
std::ostream& operator<<(std::ostream& os,
const Complex& obj) {
os << "(" << obj.m_real << ", "
<< obj.m_imaginary << ")";
return os;
}
Complex c1{2.f, 3.f};
std::cout << c1 << std::endl; // Prints '(2, 3)'
\end{cppcode}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{Friend declarations}
\begin{block}{Concept}
\begin{itemize}
\item Functions/classes can be declared \cppinline{friend} within a class scope
\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
\end{itemize}
\end{block}
\begin{exampleblock}{\texttt{operator+} as a friend}
\footnotesize
\begin{cppcode*}{}
class Complex {
float m_r, m_i;
friend Complex operator+(Complex const & a, Complex const & b);
public:
Complex ( float r, float i ) : m_r(r), m_i(i) {}
};
Complex operator+(Complex const & a, Complex const & b) {
return Complex{ a.m_r+b.m_r, a.m_i+b.m_i };
}
\end{cppcode*}
\end{exampleblock}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{Operators}
\begin{exercise}{Operators}
Write a simple class representing a fraction and pass all tests
\begin{itemize}
\item go to \texttt{exercises/operators}
\item look at \texttt{operators.cpp}
\item inspect \cppinline{main} and complete the implementation of \cppinline{class Fraction} step by step
\item you can comment out parts of \cppinline{main} to test in between
\end{itemize}
\end{exercise}
\end{frame}