5.OperationsOnData Slides
5.OperationsOnData Slides
Section :Operations
on data
1
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
2
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Operations on Data
3
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
divide
add
subtract
multiply
modulus decrement
Built in increment
utilities Formatting
4
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
5
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Basic Operations
6
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
divide
add
subtract
modulus
multiply
7
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Addition
8
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Subtraction
9
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Multiplication
10
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Division
11
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
31
31/10
10 10 10
12
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Modulus
13
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
31
31%10
10 10 10
14
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
15
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Precedence and Associativity
16
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
a + b * c -d/e -f + g
17
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Precedence : which operation to do first
Associativity : which direction or which order
18
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
a/b*c +d - e + f
19
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
It is better to make the intent in your code as clear as possible by clearly
using () to indicate which operations you want done first
20
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
21
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Prefix and Postfix + and -
22
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Regular Increment/Decrement
23
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Postfix Increment/Decrement
24
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Postfix Increment/Decrement
//5
//6
25
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Postfix Increment/Decrement
//5
//6
//5
//4
26
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Prefix Increment/Decrement
27
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Prefix Increment/Decrement
//6
28
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Prefix Increment/Decrement
//6
//6
29
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Prefix Increment/Decrement
//6
//6
//4
30
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Prefix Increment/Decrement
//6
//6
//4
//4 31
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• Prefix and postfix increment/decrement operators are cool.
32
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
33
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Compound Operators
34
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
35
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
36
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Relational Operators :
Comparing Stuff
37
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
38
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
39
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Logical Operators
40
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
AND &&
OR ||
NOT !
41
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
a b a && b
false false false
AND false true false
true false false
true true true
42
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
a b a || b
false false false
OR false true true
true false true
true true true
43
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
a !a
NOT true false
false true
44
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
45
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
46
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
47
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
48
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Combine logical operators in expression
49
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Logical and Relational combined in expressions
50
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
51
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Output Formatting
52
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
53
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
54
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
#include <ios> #include <iomanip>
55
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::flush
std::endl std::right std::boolalpha
std::showpoint
std::left
std::setfill()
std::setw() std::hex
std::internal
std::fixed
std::setprecision() std::dec std::scientific
std::oct
std::showbase std::uppercase
std::showpos
56
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std:endl
57
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
causes immediate sending of data to
std:flush the device connected to the stream
58
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::cout
std:flush
59
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::cout
std:flush
Hello
60
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::cout
std:flush
Hello
World
61
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::cout
std:flush
Hello
World
How
Are
You
62
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::cout
std:flush
Hello
World
How
Are
You
63
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std:setw()
64
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Right justified
Head Head
1 2
65
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Left justified
Head Head
1 2
66
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Internal justified
Head Head
- 3.33 2
- 34.79
67
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::setfill
68
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::boolalpha
69
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::showpos
70
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::dec std::oct and std::hex
71
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::showbase
72
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::uppercase
73
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::scientific – std::fixed
74
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::scientific – std::fixed (contd)
75
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::setprecision
76
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::showpoint
77
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Manipulator(s) header Purpose
std::endl <ostream> Insert new line character
std::flush <ostream> Flush the output stream
std::setw() <iomanip> changes the width of the
next input/output field
std::left, std::right, <ios> Value Justification
std::internal
std::boolalpha and <ios> Bool output format
std::noboolalpha
std::showpos and <ios> Show + sign for positive
std::noshowpos numbers
std::dec, std::hex, <ios> Controls the default number
std::oct system
std::showbase and <ios> Include prefix to show base
std::noshowbase
std::uppercase and <ios> Show digits in uppercase
std::nouppercase
78
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Manipulator(s) header Purpose
std::fixed and <ios> Controls floating point output
std::scientific format
std::setprecision() <iomanip> Number of digits used to
represent a floating point
type
std::setfill() <iomanip> Changes the fill character
std::showpoint and <ios> Controls whether trailing
std::noshowpoint zeros are shown
79
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Reference Doc https://en.cppreference.com/w/cpp/io/manip
80
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
81
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Numeric Limits
82
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
#include <limits>
83
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::numeric_limits<T>::min()
std::numeric_limits<T>::max()
std::numeric_limits<T>::lowest()
84
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Floating point
85
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Signed Integer
0 -32768 32767
86
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Unsigned Integer(Short)
0 0 65535
87
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Reference Doc https://en.cppreference.com/w/cpp/types/numeric_limits
88
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
89
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Math Functions
90
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
#include <cmath>
91
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::abs() std::cos()
std::floor() std::ceil()
std::sin() std::tan()
std::sqrt() std::round
92
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Reference Doc https://en.cppreference.com/w/cpp/header/cmath
93
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
94
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
95
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
𝑥
exp x = e
96
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
97
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
98
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
99
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
100
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
101
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
102
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Weird Integral Types
103
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Integral types less than 4 bytes in size
don’t support arithmetic operations
104
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
char short int
105
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
106
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Conversion to int
107
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
The same behavior is present on other operators
like bitwise shift operators ( >> and << ) .
108
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Operations on data : Summary
109
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
110
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
111
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya