brief recap of the first lecture. - network protocols...

64
1

Upload: others

Post on 20-Mar-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

1

Page 2: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Brief recap of the first lecture.

2

Page 3: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

There‟s a concept in computer science called Turing completeness – basically, that if a

language has a minimum set of features (to oversimplify – the basics are having variables

that can be read and modified paired with if statements), you can implement any

algorithm in it. This means that any algorithm you can implement in one Turing

complete language you can implement in another.

This does not mean that all languages are equally suited to all tasks.

Most people in the class are aware of Python. CS115 (a pre-requisite of this course) is

now taught in it.

It‟s worth thinking about the differences between it and C++.

3

Page 4: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

A program is a series of instructions that can be executed by a computer.

4

Page 5: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

The underlying program that the computer runs to manage other programs (and

everything else in the computer).

This is really outside of the subject of this course; the department offers a course on

operating systems.

5

Page 6: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Interpreted code is read in by a program (called an interpreter), then converted to

instructions for the computer after being read, and then executed by the computer.

While interpreted code is slower than compiled code (in real world cases, around 5x

slower is a reasonable ballpark for how much slower), it has advantages – it‟s often more

portable, and usually easier to write. It also tends to have lesser consequences when the

code fails, and is easier to secure since the interpreter knows exactly what the code it‟s

running does.

Modern web development is largely done in interpreted languages (or bytecode languages,

which are something of a special case of interpreted languages, which have some of the

advantages of compiled languages) both because the performance disadvantage is harder

to see when you add in the buffer of network access time and security and reliability tend

to be more important.

6

Page 7: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Compiled code is compiled by a compiler.

(That may be the single worst sentence you‟ll read this year)

But anyway, your compiler takes your code, and compiles it into binary form. In this

binary form, the computer executes it directly.

Most desktop applications use compiled code.

7

Page 8: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Overview of tools we‟ll use in the class.

8

Page 9: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

The IDE is the big fancy program that we use to edit code and launch the compiler.

We‟ll be using Visual Studio.

It‟s important to note that the IDE is not the compiler, linker, or debugger – it just calls

them for you and automates a lot of tasks.

And makes code prettier to look at. Which is pretty handy.

9

Page 10: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

The compiler converts your code into binary format, something the computer can read.

10

Page 11: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

The linker links code to other code – after the compiler finishes with the code, it‟s in a

form the computer can execute, but it‟s also going to need a lot of support code. Writing

a string to the screen seems simple, but there‟s a lot of code that has to be executed to

make it happen; the linker is what connects your function calls in your compiled code to

the implementation of those functions in the libraries.

11

Page 12: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

We‟ll talk a lot more about this later.

The debugger is one of the most important tools for a software developer; it lets you

look at what‟s going on in your code in the middle of the program execution.

I‟ve been asked in the past what it takes to achieve expertise in C++. I‟ve said about six

thousand hours waist-deep in a debugger. A good friend of mine who manages other

programmers claims I‟m wrong, and the real number is around ten thousand. In any

case, this is a critical tool for writing programs, and is the key for pulling off complex

programs.

12

Page 13: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Mistaking these two is a common mistake for new programmers.

C and C++ require the use of header files to let the compiler know which functions in

libraries you can call.

This is distinct from the libraries themselves, which are binary code that the linker

connects your code to.

Using the #include preprocessor directive just adds the header file; the library itself is

handled at the linker stage.

13

Page 14: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

And here we have the C++ hello world program.

14

Page 15: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Result of running this program.

15

Page 16: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

This includes the iostream header. This allows the compiler to call functions and use

classes in the iostream header – cout and endl in the short program we just saw.

16

Page 17: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Function definition for main.

There must be a main function in a C++ program, and this is the standard header. It

returns an int (indicated by the “int” to the left of main) and has two arguments – we‟ll

talk about the arguments later in the class.

17

Page 18: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

These are curly brackets. No, we don‟t have better words for them. They define a block.

Within them is the code contained in the main function.

18

Page 19: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

This is the actual hello world.

This takes cout (console output), and sends the string literal “Hello, world” to it. Then it

sends an end of line character (endl) to the console output as well.

19

Page 20: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

And then we return 0 – which is convention for a program that completed successfully.

20

Page 21: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

This tells us that we‟re using the std namespace. That‟s where things like cout and endl

live; we use this to make the rest of the program a bit less verbose.

This will show up in almost all of our programs.

21

Page 22: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

This is what the program would look like without “using namespace std;”.

Note that instead of “cout” we have “std::cout” and “std::endl” instead of “endl”.

The using namespace line lets us use these simpler forms, and so we‟ll use it in pretty

much all of our programs.

22

Page 23: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Hello world, again.

23

Page 24: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

And what most of our early programs will look like – main function definition, stuff

done, then return 0.

24

Page 25: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

These are comments – text that‟s not read by the compiler. Use them to describe what‟s

going on in the code.

The first one is a single line comment – anything on the line after the // is a comment.

The second one is a multiple line comment – anything between the /* and */ sequences

is treated as a comment, regardless of how many lines it spans.

25

Page 26: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

26

Page 27: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

The next program.

27

Page 28: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Its output.

28

Page 29: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

The program, like all C++ programs, consists of statements.

Statements end with a semicolon.

29

Page 30: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

These declare two constant integer variables and set their values.

Constant variables – noted by “const” in the declaration – cannot be changed after being

set.

“int” declared them as integers

“=“ sets their value.

The semicolon at the end terminates the statement.

30

Page 31: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

This declares another integer variable, and sets it to the two earlier defined variables

multiplied.

31

Page 32: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

And then we send it to cout (console output) again. More string literals (text between “”

pairs), and then the integer value we calculated. And another end of line character

afterwards.

32

Page 33: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

So, we just saw some declared, but let‟s talk about it formally.

33

Page 34: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

“const” comes first if the variable is constant, then you put the variable type (we‟ve only

seen “int” so far, but we‟ll see a lot more shortly), then the name of the variable. Then,

optionally, you can assign a value to it.

Big thing here – if you don‟t assign a value to a scalar value (which basically means a

simple numeric type), the value it holds is undefined. This isn‟t initialized to zero or

anything like that, but literally undefined – you have no way of knowing what‟s going to

be in that variable. So make a habit of assigning values when you declare variables.

34

Page 35: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Table listing the basic scalar types.

The bold ones at the top are the important ones for this class.

Note that the meanings are just “standard” and not exactly defined; you are unlikely to

run into a compiler that does not have 32-bit ints, but it‟s possible as that‟s not strictly

defined by the C++ standard.

35

Page 36: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

So we‟ve got variables of multiple types, so what can we do with them?

Arithmetic!

36

Page 37: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Yes, it should be radius * radius * PI. Oops.

37

Page 38: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

38

Page 39: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Binary operators – take two arguments (e.g., a + b):

= Assignment (sets the variable on the left to the value on the right)

+ Addition

- Subtraction * Multiplication

/ Division

% Modulo – remainder after an integer division is performed

Unary operators – take one argument (e.g., a++):

++ Increment; adds one to the variable

-- Decrement; subtracts one from the variable

39

Page 40: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Note on the division operator:

Most arithmetic operators will perform basically the same operation if done on ints or

floating point values (but all “promote” up to floating point values if one argument is an

int and the other a floating point value). Division works a bit differently.

If both arguments are integers, the result is an integer as well. If either is floating point,

then the result is floating point.

So:

5 / 2 == 2

5.0 / 2 == 2.5

5 / 2.0 == 2.5

5.0 / 2.0 == 2.5

Worth noting, this is a common source of mistakes – getting the wrong type of division.

40

Page 41: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Comparison operators

== Equivalence (tests if the two arguments are the same)

!= Nonequivalence

< Less than

> Greater than

<= Less than or equal to

>= Greater than or equal to

41

Page 42: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Logical operators

! Not – this is a unary operator!

&& Logical and

|| Logical or

Note! && is a different operator from &. && is logical and, & is bitwise and (which we

may go over later in class). Make sure you have the correct operator when performing

logic! There is also | which is bitwise or to be aware of.

42

Page 43: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

These are used primarily for moving data to streams (as shown on the next slide).

They also have meanings as bitwise operators, but we won‟t see that much.

43

Page 44: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Insertion operators in use.

44

Page 45: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Yes, yes they are.

This is a common mistake, not only for new programmers but for veteran programmers

as well. It‟s a poor design decision of C++!

45

Page 46: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Another strange (to the modern viewer!) design decision of C/C++…

46

Page 47: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

And some history!

47

Page 48: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Well, a lot of it is because it‟s based on an earlier language – C. And it‟s difficult (as in,

expensive) to switch over to a new language, so we have a ton of stuff that is in C and

C++, and it‟s awfully expensive to switch for something as relatively minor as what a

couple of operators look like…

48

Page 49: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

But they‟re awfully easy to confuse…

49

Page 50: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

50

Page 51: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

This is the ancient history of computing.

The first Turing complete computer was built in 1945; in Germany by Konrad Zuse.

The first “modern” programming language (as in, something that was not direct machine

code or assembly, which is code that just serves as mnemonics for binary machine code)

was FORTRAN, which first appeared in 1957.

51

Page 52: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

This is an IBM 704. A breakthrough computer of its era from 1954; IBM sold more than

they were expecting – all told, 123 by the time the model was discontinued. In today‟s

dollars, they cost around $7 million.

The dude in the picture being in a suit isn‟t an accident; these machines were used mostly

for nuclear weapons research and by huge corporations. The only people who used them

tended to be very senior engineers, due to their scarcity – there were, after all, only 123 of

them. And you couldn‟t just go to Best Buy and buy a new one.

52

Page 53: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

If the „40s and „50s were ancient history, the late „60s through the „70s was the Roman

Empire of computer science.

This was the era in which computers (see next slide) really became available to university

research, and a lot of decisions that impact everything we do in computer science today

were made.

UNIX and VMS both appeared in this era; as it turns out, knockoffs of these two

operating systems (Linux and Windows respectively) currently dominant computing.

And Dennis Ritchie developed C in 1973, and then Bjarne Strousoup developed C++ in

1983.

Note these dates – First real computer in 1945, first programming language in 1957, and

then C in 1973. That‟s 16 years that programming languages existed before C was made.

Most faculty at the University of Kentucky have more experience with programming

languages than that – and that‟s more experience than anyone on earth had at the time.

Only 28 years from the first computer to C as well – and we‟ve got faculty with more

experience than that, too.

Which, perhaps, explains why there are a few mistakes in the design…

53

Page 54: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

DEC PDP-11. 1970.

$110,000 in current dollars. 170,000 sold. Down to refrigerator size from classroom size.

This was one of the first machines that really started showing up in universities

everywhere and lead to the boom in computer science in the „70s.

54

Page 55: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

IBM PC.

$8,000 in current dollars as standardly equipped; 20 million or so sold. There are now

150 million or so computers sold each year. Changed everything – before the IBM PC,

everyone who used a computer was a programmer, trained user, or enthusiast. The “end

user” was a theoretical concept before the IBM PC.

55

Page 56: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Or, an alternate theory – C was, after all, a product of the ‟70s, and opening a Sears

catalog from that era…

56

Page 57: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Demonstrates pretty clearly that it was not a good decade for decision-making.

57

Page 58: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

All of which kinda puts this into perspective…

58

Page 59: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

So, anyway, back to C++.

Strings.

59

Page 60: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Just like you need to have #include <iostream> to use cout and endl, you need to have

#include <string> at the top of your program to use strings.

60

Page 61: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Here‟s an updated program that uses strings.

We assign them to string literals, and can send them to cout. They can be const, just like

scalars (simple numeric types).

Note that unlike scalars, strings do have a default value if constructed and uninitialized;

they are, explicitly, an empty string if uninitialized.

Note also that both methods used to output the string – sending the subparts over to

cout via insertion operators, or using + to concatenate the strings together – produce the

same output. There‟s not much of a reason to prefer one over the other in a vacuum.

61

Page 62: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Output of the previous program.

62

Page 63: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Adding an extra long – “again.length()” here is the way we get an integer with the length

of the string.

63

Page 64: Brief recap of the first lecture. - Network Protocols Labprotocols.netlab.uky.edu/~davidb/cs215/notes/cs215_2.pdf+ Addition - Subtraction * Multiplication / Division ... the result

Output of the program.

64