TDDE18 & 726G77 Course Introductjon Christofger Holm - - PowerPoint PPT Presentation

tdde18 726g77
SMART_READER_LITE
LIVE PREVIEW

TDDE18 & 726G77 Course Introductjon Christofger Holm - - PowerPoint PPT Presentation

TDDE18 & 726G77 Course Introductjon Christofger Holm Department of Computer and informatjon science 1 Course Informatjon 2 C++ basics 3 IO 4 Variables 5 More IO 6 Streams 7 Basic constructs 8 Files 2 / 76 Course Informatjon


slide-1
SLIDE 1

TDDE18 & 726G77

Course Introductjon

Christofger Holm

Department of Computer and informatjon science

slide-2
SLIDE 2

1 Course Informatjon 2 C++ basics 3 IO 4 Variables 5 More IO 6 Streams 7 Basic constructs 8 Files

slide-3
SLIDE 3

2 / 76

Course Informatjon

Personnel

‚ Examiner: Klas Arvidsson ‚ Course leader: Christofger Holm ‚ Course assistant: Mladen Nikic ‚ Assistant: August Karlsson ‚ Assistant: David Ångström ‚ Assistant: Elin Frankell ‚ Assistant: Emil Erkgärds ‚ Assistant: Jesper Jonsson

slide-4
SLIDE 4

3 / 76

Course Informatjon

Aim (syllabus)

‚ Prerequisites: Skills in one programming language ‚ C++ ‚ Usage of standard Linux/UNIX systems ‚ Problem solving

slide-5
SLIDE 5

4 / 76

Course Informatjon

Content

‚ Basic constructs ‚ Pointers and memory ‚ Object‐oriented programming ‚ Inheritance and polymorphism ‚ Standard library ‚ Templates

slide-6
SLIDE 6

5 / 76

Course Informatjon

Examinatjon

‚ Labs ‚ Exam

slide-7
SLIDE 7

5 / 76

Course Informatjon

Examinatjon

‚ Labs ‚ 6 lab assignments ‚ Sofu deadlines (1 per lab) ‚ Demonstrate your work to the assistant ‚ Complementary work ‚ Bonus for exam ‚ Exam

slide-8
SLIDE 8

5 / 76

Course Informatjon

Examinatjon

‚ Labs ‚ Work in pairs ‚ Two tjme slots, group A or B ‚ Register on WebReg before fjrst lab! ‚ the two groups must be of equal size due to resources, so try to register to the group with fewer students ‚ Exam

slide-9
SLIDE 9

5 / 76

Course Informatjon

Examinatjon

‚ Labs ‚ Exam ‚ Computer exam ‚ 5 assignments ‚ Grading ‚ Complementary work

slide-10
SLIDE 10

6 / 76

Course Informatjon

Organizatjon

‚ Lectures ‚ Lab sessions ‚ Teaching session

slide-11
SLIDE 11

7 / 76

Course Informatjon

Online resources

‚ http://ida.liu.se/~TDDE18 ‚ http://cppreference.com ‚ The library part of cppreference will be available during the exam!

slide-12
SLIDE 12

8 / 76

Course Informatjon

Register to the lab

Register to the labs on WebReg: https://www.ida.liu.se/webreg-beta/ TDDE18-2020-1/LAB1

slide-13
SLIDE 13

1 Course Informatjon 2 C++ basics 3 IO 4 Variables 5 More IO 6 Streams 7 Basic constructs 8 Files

slide-14
SLIDE 14

10 / 76

C++ basics

What is C++?

‚ Programming language ‚ Is based on C ‚ Defjned by a commituee

slide-15
SLIDE 15

11 / 76

C++ basics

What is C++?

‚ Gives programmer control Broad applicatjon area Highly optjmized

slide-16
SLIDE 16

11 / 76

C++ basics

What is C++?

‚ Gives programmer control ‚ Broad applicatjon area Highly optjmized

slide-17
SLIDE 17

11 / 76

C++ basics

What is C++?

‚ Gives programmer control ‚ Broad applicatjon area ‚ Highly optjmized

slide-18
SLIDE 18

12 / 76

C++ basics

What is C++?

‚ C++ is not a specifjc set of programs ‚ C++ is not an editor ‚ C++ is not a compiler ‚ It is simply a language that can be passed to a compiler

slide-19
SLIDE 19

13 / 76

C++ basics

A fjrst program

program.cc

#include <iostream> using namespace std; int main() { cout << "A C++ program" << endl; return 0; }

slide-20
SLIDE 20

14 / 76

C++ basics

A fjrst program

‚ main is the start point ‚ When the program starts, every line of code in main will be executed in order ‚ We signal the end of a line with ; ‚ cout prints text to the console ‚ return 0 tells the program to exit ‚ #include <iostream> and using namespace std makes cout available

slide-21
SLIDE 21

15 / 76

C++ basics

Compiling

program.cc

slide-22
SLIDE 22

15 / 76

C++ basics

Compiling

program.cc Compiler

slide-23
SLIDE 23

15 / 76

C++ basics

Compiling

program.cc Compiler Executable file

slide-24
SLIDE 24

16 / 76

C++ basics

Compiling

‚ A compiler is a special program ‚ It converts source code fjles into executable fjles ‚ A source code fjle is a text fjle that contains your code ‚ An executable fjle is a fjle that contains machine code which the computer can run ‚ There are many compilers for C++, in this course we use

  • ne called g++
slide-25
SLIDE 25

17 / 76

C++ basics

Compiling $ ls

slide-26
SLIDE 26

17 / 76

C++ basics

Compiling $ ls program.cc

slide-27
SLIDE 27

17 / 76

C++ basics

Compiling $ ls program.cc $ g++ program.cc

slide-28
SLIDE 28

17 / 76

C++ basics

Compiling $ ls program.cc $ g++ program.cc $

slide-29
SLIDE 29

17 / 76

C++ basics

Compiling $ ls program.cc $ g++ program.cc $ $ ls

slide-30
SLIDE 30

17 / 76

C++ basics

Compiling $ ls program.cc $ g++ program.cc $ $ ls a.out program.cc

slide-31
SLIDE 31

17 / 76

C++ basics

Compiling $ ls program.cc $ g++ program.cc $ $ ls a.out program.cc $ ./a.out

slide-32
SLIDE 32

17 / 76

C++ basics

Compiling $ ls program.cc $ g++ program.cc $ $ ls a.out program.cc $ ./a.out A C++ program

slide-33
SLIDE 33

18 / 76

C++ basics

Compiling

‚ To compile your source fjle program.cc run the following in the console: g++ program.cc ‚ If nothing is printed then the compilatjon was successful ‚ This will produce an executable fjle called a.out ‚ To run your program you write: ./a.out in the console

slide-34
SLIDE 34

19 / 76

C++ basics

Compiler fmags g++ -Wall -Wextra -Wpedantic -std=c++17 program.cc

slide-35
SLIDE 35

20 / 76

C++ basics

Compiler fmags

‚ Flags can be used to enable or confjgure certain features in the compiler ‚ -Wall -Wextra -Wpedantic will add more warnings from the compiler which helps us write betuer programs ‚ -std=c++17, -std=c++14 or -std=c++11 allows us to pick a certain version of C++, default should be C++17 ‚ Recommended: Create an alias

slide-36
SLIDE 36

21 / 76

C++ basics

Creatjng alias

echo "alias w++17='g++ -std=c++17 -Wall -Wextra -Wpedantic'" >> ~/.bashrc

slide-37
SLIDE 37

22 / 76

C++ basics

Creatjng alias

‚ This will add an alias to your system. ‚ Allows us to use w++17 as our compiler to automatjcally get all the fmags ‚ Example: w++17 program.cc will now be the same as

g++ -std=c++17 -Wall -Wextra -Wpedantic program.cc.

slide-38
SLIDE 38

1 Course Informatjon 2 C++ basics 3 IO 4 Variables 5 More IO 6 Streams 7 Basic constructs 8 Files

slide-39
SLIDE 39

24 / 76

IO

Idea

program

slide-40
SLIDE 40

25 / 76

IO

Idea

‚ most (if not all) programs requires some kind of interactjon with the user ‚ modern computer systems involves GUI (Graphical User Interface) ‚ but before GUI was a thing, everything was done through a terminal ‚ this is where we begin; printjng to and reading input from a terminal

slide-41
SLIDE 41

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; }

slide-42
SLIDE 42

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; }

slide-43
SLIDE 43

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text!\n

slide-44
SLIDE 44

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text!\n

slide-45
SLIDE 45

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text!

slide-46
SLIDE 46

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text!

slide-47
SLIDE 47

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text! Some text!\n

slide-48
SLIDE 48

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text! Some text!\n

slide-49
SLIDE 49

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text! Some text!\nMore!\n

slide-50
SLIDE 50

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text! Some text!\nMore!\n

slide-51
SLIDE 51

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text! Some text! More!

slide-52
SLIDE 52

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text! Some text! More!

slide-53
SLIDE 53

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text! Some text! More! The

slide-54
SLIDE 54

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text! Some text! More! The

slide-55
SLIDE 55

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text! Some text! More! The

slide-56
SLIDE 56

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text! Some text! More! The

slide-57
SLIDE 57

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text! Some text! More! The End!\n

slide-58
SLIDE 58

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text! Some text! More! The End!\n

slide-59
SLIDE 59

26 / 76

IO

Printjng

program.cc

#include <iostream> using namespace std; int main() { cout << "Text!" << endl; cout << "Some text!\n"; cout << "More!" << endl; cout << "The " << flush; cout << "End!" << endl; } Text! Some text! More! The End!

slide-60
SLIDE 60

27 / 76

IO

Printjng

‚ cout prints to a bufger ‚ the bufger is printed when it is flushed ‚ usually the bufger is fmushed when a newline (\n) is printed (not guaranteed though) ‚ however to guarantee a fmush we can use endl instead (which also inserts a newline) ‚ to fmush without adding a newline we can use flush

slide-61
SLIDE 61

28 / 76

IO

What about reading?

program

slide-62
SLIDE 62

29 / 76

IO

Storing things

‚ When reading things from the terminal we must be able to store these things in our program ‚ Everything entered into a terminal is text. ‚ But computers work with numbers, how do we read numbers? ‚ We can specify how the computer should interpret the things read by specifying a so called data type

slide-63
SLIDE 63

1 Course Informatjon 2 C++ basics 3 IO 4 Variables 5 More IO 6 Streams 7 Basic constructs 8 Files

slide-64
SLIDE 64

31 / 76

Variables

Basics int main() { int x{3}; double y{3.14}; char z{'c'}; }

slide-65
SLIDE 65

31 / 76

Variables

Basics int main() { int x{3}; double y{3.14}; char z{'c'}; } int main() { int x{3}; cout << "x = " << x << endl; }

slide-66
SLIDE 66

32 / 76

Variables

Basics

‚ Variables are used to store and access data ‚ Have difgerent types; integers, decimal numbers, characters etc. ‚ Types determines what kind of values can be stored inside the variables. ‚ A variable can never change type ‚ Most variables can be printed to cout

slide-67
SLIDE 67

33 / 76

Variables

string

#include <iostream> #include <string> using namespace std; int main() { string str {"hello"}; cout << str << endl << str.size() << endl << str.front() << endl; }

slide-68
SLIDE 68

33 / 76

Variables

string

#include <iostream> #include <string> using namespace std; int main() { string str {"hello"}; cout << str << endl << str.size() << endl << str.front() << endl; }

$ ./a.out hello 5 h

slide-69
SLIDE 69

34 / 76

Variables

string

‚ string is defjned in #include <string> ‚ can either be accessed with using namespace std; or by calling it std::string instead of just string ‚ Represents text (a sequence of characters). ‚ Has alot of builtjn functjonality that other types do not.

slide-70
SLIDE 70

35 / 76

Variables

const int x{5}; x = 7; int const y{7}; y = 9; // will not compile const int z{9};

slide-71
SLIDE 71

36 / 76

Variables

const

‚ Variables can be marked as read‐only by adding the keyword const ‚ This means that the value of the variable can never change. ‚ You can place the const before or afuer the data type. ‚ I recommend that you place it afuer the type, why will become apparent in later lectures.

slide-72
SLIDE 72

1 Course Informatjon 2 C++ basics 3 IO 4 Variables 5 More IO 6 Streams 7 Basic constructs 8 Files

slide-73
SLIDE 73

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "" number = 0 letter = '\0'

slide-74
SLIDE 74

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "" number = 0 letter = '\0'

slide-75
SLIDE 75

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "" number = 0 letter = '\0'

slide-76
SLIDE 76

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "" number = 0 letter = '\0' programming 10

slide-77
SLIDE 77

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "" number = 0 letter = '\0' programming 10ê

slide-78
SLIDE 78

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "" number = 0 letter = '\0' programming 10ê

slide-79
SLIDE 79

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "" number = 0 letter = '\0'

Programming 10\n

slide-80
SLIDE 80

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "" number = 0 letter = '\0'

Programming 10\n

slide-81
SLIDE 81

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 0 letter = '\0'

10\n

slide-82
SLIDE 82

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 0 letter = '\0'

10\n

slide-83
SLIDE 83

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 0 letter = '\0'

10\n

slide-84
SLIDE 84

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 0 letter = '\0'

10\n

slide-85
SLIDE 85

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 10 letter = '\0'

\n

slide-86
SLIDE 86

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 10 letter = '\0'

\n

slide-87
SLIDE 87

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 10 letter = '\0'

\n

slide-88
SLIDE 88

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 10 letter = '\0'

slide-89
SLIDE 89

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 10 letter = '\0'

slide-90
SLIDE 90

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 10 letter = '\0' a

slide-91
SLIDE 91

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 10 letter = '\0' aê

slide-92
SLIDE 92

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 10 letter = '\0' aê

slide-93
SLIDE 93

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 10 letter = '\0' aê

a\n

slide-94
SLIDE 94

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 10 letter = '\0'

a\n

slide-95
SLIDE 95

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 10 letter = '\0'

a\n

slide-96
SLIDE 96

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 10 letter = 'a'

\n

slide-97
SLIDE 97

38 / 76

More IO

Reading

program.cc

#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a word and number: "; string word{}; int number{}; char letter{}; cin >> word; cin >> number; cin >> letter; }

word = "programming" number = 10 letter = 'a'

\n

slide-98
SLIDE 98

39 / 76

More IO

Reading

‚ Reading values from the terminal into variables is done using cin. ‚ There is a bufger which the reading will be done from in fjrst‐hand. ‚ If the bufger is empty then, and only then will a prompt appear in the terminal. ‚ cin will read from this bufger untjl it is empty again. ‚ Most data types can be read from cin.

slide-99
SLIDE 99

40 / 76

More IO

Reading

‚ Whitespace characters are such characters as space, newline and other characters that doesn’t have a glyph. ‚ While reading from the bufger, cin will ignore all whitespaces untjl it reaches a non‐whitespace character. ‚ If cin fjnds a whitespace character (or any character that is nonsensical for the data type) while reading a value, the reading is done.

slide-100
SLIDE 100

41 / 76

More IO

getline string line; getline(cin, line); cin.ignore(1000, '\n');

This is a line\nAnother line\n

line = ""

slide-101
SLIDE 101

41 / 76

More IO

getline string line; getline(cin, line); cin.ignore(1000, '\n');

This is a line\nAnother line\n

line = ""

slide-102
SLIDE 102

41 / 76

More IO

getline string line; getline(cin, line); cin.ignore(1000, '\n');

This is a line\nAnother line\n

line = ""

slide-103
SLIDE 103

41 / 76

More IO

getline string line; getline(cin, line); cin.ignore(1000, '\n');

\nAnother line\n

line = "this is a line"

slide-104
SLIDE 104

41 / 76

More IO

getline string line; getline(cin, line); cin.ignore(1000, '\n');

Another line\n

line = "this is a line"

slide-105
SLIDE 105

41 / 76

More IO

getline string line; getline(cin, line); cin.ignore(1000, '\n');

Another line\n

line = "this is a line"

slide-106
SLIDE 106

41 / 76

More IO

getline string line; getline(cin, line); cin.ignore(1000, '\n');

Another line\n

line = "this is a line"

slide-107
SLIDE 107

41 / 76

More IO

getline string line; getline(cin, line); cin.ignore(1000, '\n');

\n

line = "this is a line"

slide-108
SLIDE 108

41 / 76

More IO

getline string line; getline(cin, line); cin.ignore(1000, '\n');

line = "this is a line"

slide-109
SLIDE 109

41 / 76

More IO

getline string line; getline(cin, line); cin.ignore(1000, '\n');

line = "this is a line"

slide-110
SLIDE 110

42 / 76

More IO

getline #include <iostream> #include <string> using namespace std; int main() { string line; cout << "Enter a line: "; getline(cin, line); cout << "Your line was: " << line << endl; }

slide-111
SLIDE 111

43 / 76

More IO

getline

‚ getline is how we read entjre lines instead of words. ‚ We give it cin and a string we want to read into. ‚ It will read untjl it fjnds a newline character (\\n) and store it into the string. ‚ Then it will remove the newline from the bufger.

slide-112
SLIDE 112

44 / 76

More IO

ignore

‚ cin.ignore will remove things from the stream ‚ we give cin.ignore two things; how many characters to ignore and what the delimiter is ‚ cin.ignore will ignore either the specifjed amount of character or untjl it fjnds the delimiter character, whichever occurs fjrst!

slide-113
SLIDE 113

45 / 76

More IO

The complete picture

program

cout << ... cin >> ...

slide-114
SLIDE 114

1 Course Informatjon 2 C++ basics 3 IO 4 Variables 5 More IO 6 Streams 7 Basic constructs 8 Files

slide-115
SLIDE 115

47 / 76

Streams

Reading from fjles #include <fstream> #include <string> using namespace std; int main() { ifstream in{"data.txt"}; string line; int x; in >> x; getline(in, line); in.ignore(1000, '\n'); }

slide-116
SLIDE 116

48 / 76

Streams

What are streams?

‚ Streams are how we communicate with external resources ‚ cout and cin are streams that communicate with the terminal ‚ But there are other external resources such as fjles, memory, network etc. ‚ C++ lets us communicate with fjles through ifstream (reading from) and ofstream (writjng to)

slide-117
SLIDE 117

49 / 76

Streams

What are streams?

‚ ifstream and ofstream are defjned in

#include <fstream>

‚ All streams work according to the same principles we learned from cin and cout ‚ so all operatjons we have talked about work exactly the same way for all streams

slide-118
SLIDE 118

50 / 76

Streams

Formattjng output streams

#include <iostream> #include <iomanip> using namespace std; int main() { cout << setw(10) << "hello" << '|' << right << setw(10) << "world" << endl; cout << setfill('-') << setw(21) << "The end!" << endl; }

$ ./a.out hello | world

  • ------------The end!
slide-119
SLIDE 119

51 / 76

Streams

Formattjng output streams

‚ For more advanced formattjng we can include

#include <iomanip>

‚ setw(10) will ensure that the next item printed will print at least 10 characters. ‚ If the printed item prints less than 10 characters the rest will be fjlled with spaces afuer the item untjl it is 10 characters.

slide-120
SLIDE 120

52 / 76

Streams

Formattjng output streams

‚ We can modify propertjes of setw ‚ right places the spaces before the item instead of afuer. ‚ setfill will replace the spaces with some other character. ‚ Both right and setfill are stjcky meaning they will stay in efgect untjl they are replaced. ‚ There are a lot more features in #include <iomanip>. Look at cppreference: https: //en.cppreference.com/w/cpp/io/manip

slide-121
SLIDE 121

1 Course Informatjon 2 C++ basics 3 IO 4 Variables 5 More IO 6 Streams 7 Basic constructs 8 Files

slide-122
SLIDE 122

54 / 76

Basic constructs

Conditjonal statements if (some logical statement) { // do this } else if (some other logical statement) { // do this instead } else { // when all else fails, do this }

slide-123
SLIDE 123

55 / 76

Basic constructs

bool int main() { bool statement{false}; if (statement) { // will not run } else { // will run } }

slide-124
SLIDE 124

55 / 76

Basic constructs

bool int main() { bool statement{false}; if (!statement) { // will run } else { // will not run } }

slide-125
SLIDE 125

56 / 76

Basic constructs

bool

‚ bool is a data type that represents logical results ‚ Can be true or false. ‚ Represents the results of conditjonal statements. ‚ Can be inverted with ! (or the keyword not).

slide-126
SLIDE 126

57 / 76

Basic constructs

Comparison and logical operators

‚ a == b ‚ a != b ‚ a < b ‚ a <= b ‚ a > b ‚ a >= b

slide-127
SLIDE 127

57 / 76

Basic constructs

Comparison and logical operators

‚ a == b ‚ a != b ‚ a < b ‚ a <= b ‚ a > b ‚ a >= b ‚ a == b and c != b ‚ a == b or a == c

slide-128
SLIDE 128

57 / 76

Basic constructs

Comparison and logical operators

‚ a == b ‚ a != b ‚ a < b ‚ a <= b ‚ a > b ‚ a >= b ‚ a == b && c != b ‚ a == b || a == c

slide-129
SLIDE 129

58 / 76

Basic constructs

Loops #include <iostream> using namespace std; int main() { int x{}; cout << "Enter number (1-10): "; cin >> x; while (x < 1 || x > 10) { cout << "Enter number (1-10): "; cin >> x; } }

slide-130
SLIDE 130

59 / 76

Basic constructs

Loops #include <iostream> using namespace std; int main() { int x{}; do { cout << "Enter number (1-10): "; cin >> x; } while (x < 1 || x > 10); }

slide-131
SLIDE 131

60 / 76

Basic constructs

Loops

‚ Two ways to loop based on a conditjon ‚ while‐loops will run 0 or more tjmes ‚ do‐while‐loops will run 1 or more tjmes

slide-132
SLIDE 132

61 / 76

Basic constructs

for‐loop #include <iostream> using namespace std; int main() { for (int i{0}; i < 10; ++i) { cout << "Iteration #" << i << endl; } }

slide-133
SLIDE 133

62 / 76

Basic constructs

for‐loop

‚ for‐loops are the third way to loop ‚ This is used whenever we know how many tjmes we are going to loop

slide-134
SLIDE 134

63 / 76

Basic constructs

Arithmetjc operatjons

‚ a + b (additjon) ‚ a - b (subtractjon) ‚ a * b (multjplicatjon) ‚ a / b (division) ‚ a % b (modulus)

slide-135
SLIDE 135

63 / 76

Basic constructs

Arithmetjc operatjons

‚ a + b (additjon) ‚ a - b (subtractjon) ‚ a * b (multjplicatjon) ‚ a / b (division) ‚ a % b (modulus) ‚ -a (negatjon) ‚ ++a (prefjx increment) ‚ a++ (postgix increment) ‚ --a (prefjx decrement) ‚ a-- (postgix decrement)

slide-136
SLIDE 136

64 / 76

Basic constructs

Prefjx vs. Postgix int a{0}; a += 2; // a = a + 2 ++a; // a = a + 1 a++; // a = a + 1 int b{++a}; int c{a++}; // what is a, b and c?

slide-137
SLIDE 137

65 / 76

Basic constructs

Prefjx vs. Postgix

‚ ++a will increment a and then give back the new value

  • f a.

‚ a++ will increment a and then give back the old value of

a.

‚ Example:

int a{0}; // a = 0 int b{++a}; // a = 1 and b = 1 int c{a++}; // a = 2 and c = 1

slide-138
SLIDE 138

66 / 76

Basic constructs

Prefjx vs. Postgix

Rule of thumb: the placement of ++ determines when the increment is performed (before or afuer we read the value)

slide-139
SLIDE 139

67 / 76

Basic constructs

Type castjng

‚ 3 / 2 = 1

3 / 2.0 = 1.5 3.0 / 2 = 1.5 3.0 / 2.0 = 1.5

slide-140
SLIDE 140

67 / 76

Basic constructs

Type castjng

‚ 3 / 2 = 1 ‚ 3 / 2.0 = 1.5

3.0 / 2 = 1.5 3.0 / 2.0 = 1.5

slide-141
SLIDE 141

67 / 76

Basic constructs

Type castjng

‚ 3 / 2 = 1 ‚ 3 / 2.0 = 1.5 ‚ 3.0 / 2 = 1.5

3.0 / 2.0 = 1.5

slide-142
SLIDE 142

67 / 76

Basic constructs

Type castjng

‚ 3 / 2 = 1 ‚ 3 / 2.0 = 1.5 ‚ 3.0 / 2 = 1.5 ‚ 3.0 / 2.0 = 1.5

slide-143
SLIDE 143

68 / 76

Basic constructs

Type castjng

‚ When performing operatjons on values C++ will always make sure that the result is the same result as the

  • perands

‚ So all operatjons on integers will give us integers ‚ If there are two difgerent operands it will always convert the less accurate one to the more accurate type and then perform the operatjon

slide-144
SLIDE 144

69 / 76

Basic constructs

Type castjng int a{3}; int b{2}; cout << a / b << endl; // will output 1 cout << static_cast<double>(a) / b << endl; // will output 1.5

slide-145
SLIDE 145

70 / 76

Basic constructs

Type castjng

‚ static_cast can be used to convert an expression into another type ‚ Will only work if the conversion is sensical ‚ Should be used as litule as possible ‚ But sometjmes it is unavoidable

slide-146
SLIDE 146

1 Course Informatjon 2 C++ basics 3 IO 4 Variables 5 More IO 6 Streams 7 Basic constructs 8 Files

slide-147
SLIDE 147

72 / 76

Files

Reading all of a fjle ifstream ifs{"data.txt"}; string s{}; while (...) { ... }

slide-148
SLIDE 148

72 / 76

Files

Reading all of a fjle ifstream ifs{"data.txt"}; string s{}; while (ifs >> s) { ... }

slide-149
SLIDE 149

73 / 76

Files

Reading all of a fjle

‚ Once the end of the fjle is reached cin >> s will return false ‚ This allows us to read word by word untjl the end of the fjle ‚ Works with getline as well!

slide-150
SLIDE 150

74 / 76

Files

Reading all of a fjle ifstream ifs{"data.txt"}; string line{}; while (getline(ifs, line)) { ... }

slide-151
SLIDE 151

75 / 76

Files

UNIX console

‚ cd to change directory ‚ mkdir to create a directory ‚ ls view fjles in current directory ‚ rm remove a fjle ‚ rm -r remove a directory ‚ mv to rename a fjle or directory ‚ cp to copy a fjle

slide-152
SLIDE 152

76 / 76

Register on WebReg!

slide-153
SLIDE 153

www.liu.se