22
Introduction to C++ with content from www.cplusplus.com Introduction 2 C++ widely-used general-purpose programming language ____________ procedural and object-oriented support strong support created by Bjarne Stroustrup starting in 1979 based on C also with inheritance, , default function arguments, and strong type checking many programs compile with C++ compiler major releases in 1983, 1989, 1998, 2011 (C++11) Structure of a C++ Program 3 Structure of a C++ Program 4 previous program could also be written as follows

Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Introduction to C++

with content from www.cplusplus.com

Introduction

2

C++widely-used general-purpose programming language

____________procedural and object-oriented supportstrong support

created by Bjarne Stroustrup starting in 1979based on C

also with inheritance, , default functionarguments, and strong type checkingmany programs compile with C++ compiler

major releases in 1983, 1989, 1998, 2011 (C++11)

Structure of a C++ Program

3

Structure of a C++ Program

4

previous program could also be written as follows

Page 2: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Structure of a C++ Program

5

two styles of ______________

Structure of a C++ Program

6

_______________

Identifiers

7

similar to rules for identifiers_________________________keywords

Types

8

fundamental types

Page 3: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Variables

9

must be _______________

Initializing Variables

10

different ways to initialize variables at _______________

Variables

11

automatic deductionwith initialization

without initialization

used in cases where type cannot be obtained easily for___________________

Strings

12

Page 4: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Literals

13

integers

floats

chars

Literals

14

escape sequences

Constants

15

constants

Constants

16

#define constants

Page 5: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Increment/Decrement

17

_________________________

Operators

18

if a=2, b=3, c=6

AND/OR

other work similarly to Python

Ternary Operator

19

condition ? result1 : result2

Bitwise Operators

20

Page 6: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Type Casting

21

both OK

Operator Precedence

22

Input/Output

23

Input/Output

24

Page 7: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

if Statements

25

if

Iteration

26

statement

Iteration

27

do-while statement

Iteration

28

loop

Page 8: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Iteration

29

for loop

break Statement

30

break

continue Statement

31

continue

switch Statement

32

Page 9: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Functions

33

Functions

34

Functions

35

Functions

36

return value from ______________

Page 10: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Functions

37

pass by vs. pass by reference

Functions

38

functions

Functions

39

values for parameters

Functions

40

function ___________

Page 11: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Functions

41

_________________

Scope

42

vs. local variables

general rule: DO NOT USE

Scope

43

can only represent one entity

Scope

44

scope

Page 12: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Arrays

45

memory locations

Initializing Arrays

46

elements not automatically initialized, but can be _________initialized

Initializing Arrays

47

if { } are present, values are to default values

Initializing Arrays

48

initialized arrays without are automatically sized toaccommodate values

can be initialized without =

no error if range _________________example uses of arrays

Page 13: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Arrays

49

Multidimensional Arrays

50

arrays of arrays

Multidimensional Arrays

51

can be any , but space increases ___________

allocates a char for each second in the last centuryconsumes 3GB of memory

could have been implemented as a single-dimension array

Multidimensional Arrays

52

Page 14: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Arrays as Parameters

53

Character Arrays

54

sets aside space, but not ________________

Character Arrays

55

can initialize with individual elements or literals

not valid

OK

Strings and Character Arrays

56

Page 15: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Strings and Character Arrays

57

can be transformed one to another

Pointers

58

pointer the of somethingexact memory locations unknown at time

use & to get the of a variable

Pointers

59

use * to get the at a pointer (address)

Pointers

60

& and * are _________________________

with following assignments

all of the following are true

Page 16: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Declaring Pointers

61

all are the same in memory

different

Pointers

62

Pointers

63

Pointers and Arrays

64

with no index is a pointer to the first elementarrays can always be converted to _______________

not valid to go the other way

Page 17: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Pointers and Arrays

65

v

Pointers and Arrays

66

array with is a simply a pointer with an _______can be represented with pointer

Pointer Initialization

67

pointers can be initialized at __________________

same as

not valid

OK

Pointer Arithmetic

68

pointers can be used in expressions, withunderlying taken into accountsuppose the following have addresses 1000, 2000, 3000

after the following

values are 1001, 2002, 3004same results for

Page 18: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Pointer Arithmetic

69

the following is equivalent to ____________

other examples

done before increment

same as

Pointers and const

70

if value pointed to is const, it cannot be _______________

pointers can be const

same

Pointers and const

71

pointers are not , so can be modified

Pointers to Pointers

72

Page 19: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

void Pointers

73

void pointers point to no particular ______________

Pointers

74

pointers can point to any ________________

pointers can point to __________________

or simply

pointers and void pointers are different

Dynamic Memory

75

memory can be allocated during with new

can check for success/failure

Dynamic Memory

76

memory can (and should) be during run timewith delete

can also usemix

Page 20: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Dynamic Memory

77

Data Structures

78

struct

or

access

Data Structures

79

Data Structures

80

Page 21: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Data Structures

81

Data Structures

82

pointers to _________________

different from

Data Structures

83

structs

access

Other Data Structures

84

type ________________

can be used as

with clause

Page 22: Introduction to C++tadavis/cs303/cppsm.pdf · 2018. 7. 9. · Introduction to C++ with content from Introduction 2 C++ widely-used general-purpose programming language _____ ... do-while

Other Data Structures

85

union

can be accessed as

Other Data Structures

86

union

Other Data Structures

87

anonymous union

Enumerated Types

88

can assign integer values (assigned anyway starting at )