88
Introduction to C++ with content from www.cplusplus.com

Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

  • Upload
    lyhanh

  • View
    221

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Introduction to C++

with content from www.cplusplus.com

Page 2: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

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

−first called “C with Classes”

−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)

Page 3: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Structure of a C++ Program

3

Page 4: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Structure of a C++ Program

4

−previous program could also be written as follows

Page 5: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Structure of a C++ Program

5

− two styles of ______________

Page 6: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Structure of a C++ Program

6

−_______________

Page 7: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Identifiers

7

−similar to rules for identifiers

−_________________________

−keywords

Page 8: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Types

8

− fundamental types

Page 9: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Variables

9

−must be _______________

Page 10: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Initializing Variables

10

−different ways to initialize variables at _______________

Page 11: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Variables

11

−automatic deduction

−with initialization

−without initialization

−used in cases where type cannot be obtained easily for

___________________

Page 12: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Strings

12

Page 13: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Literals

13

− integers

− floats

−chars

Page 14: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Literals

14

−escape sequences

Page 15: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Constants

15

− constants

Page 16: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Constants

16

−#define constants

Page 17: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Increment/Decrement

17

−_________________________

Page 18: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Operators

18

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

−AND/OR

−other work similarly to Python

Page 19: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Ternary Operator

19

−condition ? result1 : result2

Page 20: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Bitwise Operators

20

Page 21: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Type Casting

21

−both OK

Page 22: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Operator Precedence

22

Page 23: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Input/Output

23

Page 24: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Input/Output

24

Page 25: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

if Statements

25

− if

Page 26: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Iteration

26

− statement

Page 27: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Iteration

27

−do-while statement

Page 28: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Iteration

28

− loop

Page 29: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Iteration

29

− for loop

Page 30: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

break Statement

30

−break

Page 31: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

continue Statement

31

−continue

Page 32: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

switch Statement

32

Page 33: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Functions

33

Page 34: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Functions

34

Page 35: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Functions

35

Page 36: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Functions

36

− return value from ______________

Page 37: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Functions

37

−pass by vs. pass by reference

Page 38: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Functions

38

− functions

Page 39: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Functions

39

− values for parameters

Page 40: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Functions

40

− function ___________

Page 41: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Functions

41

−_________________

Page 42: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Scope

42

− vs. local variables

−general rule: DO NOT USE

Page 43: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Scope

43

− can only represent one entity

Page 44: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Scope

44

− scope

Page 45: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Arrays

45

− memory locations

Page 46: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Initializing Arrays

46

−elements not automatically initialized, but can be _________

initialized

Page 47: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Initializing Arrays

47

− if { } are present, values are to default values

Page 48: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Initializing Arrays

48

− initialized arrays without are automatically sized to

accommodate values

−can be initialized without =

−no error if range _________________

−example uses of arrays

Page 49: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Arrays

49

Page 50: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Multidimensional Arrays

50

−arrays of arrays

Page 51: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Multidimensional Arrays

51

−can be any , but space increases ___________

−allocates a char for each second in the last century

−consumes 3GB of memory

−could have been implemented as a single-dimension array

Page 52: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Multidimensional Arrays

52

Page 53: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Arrays as Parameters

53

Page 54: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Character Arrays

54

−sets aside space, but not ________________

Page 55: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Character Arrays

55

−can initialize with individual elements or literals

−not valid

−OK

Page 56: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Strings and Character Arrays

56

Page 57: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Strings and Character Arrays

57

−can be transformed one to another

Page 58: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Pointers

58

−pointer – the of something

−exact memory locations unknown at time

−use & to get the of a variable

Page 59: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Pointers

59

−use * to get the at a pointer (address)

Page 60: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Pointers

60

−& and * are _________________________

−with following assignments

−all of the following are true

Page 61: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Declaring Pointers

61

−all are the same in memory

−different

Page 62: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Pointers

62

Page 63: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Pointers

63

Page 64: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Pointers and Arrays

64

− with no index is a pointer to the first element

−arrays can always be converted to _______________

−not valid to go the other way

Page 65: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Pointers and Arrays

65

v

Page 66: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Pointers and Arrays

66

−array with is a simply a pointer with an _______

−can be represented with pointer

Page 67: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Pointer Initialization

67

−pointers can be initialized at __________________

−same as

−not valid

−OK

Page 68: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Pointer Arithmetic

68

−pointers can be used in expressions, with

underlying taken into account

−suppose the following have addresses 1000, 2000, 3000

−after the following

−values are 1001, 2002, 3004

−same results for

Page 69: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Pointer Arithmetic

69

− the following is equivalent to ____________

−other examples

− done before increment

−same as

Page 70: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Pointers and const

70

− if value pointed to is const, it cannot be _______________

−pointers can be const

−same

Page 71: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Pointers and const

71

−pointers are not , so can be modified

Page 72: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Pointers to Pointers

72

Page 73: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

void Pointers

73

−void pointers point to no particular ______________

Page 74: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Pointers

74

−pointers can point to any ________________

−pointers can point to __________________

−or simply

− pointers and void pointers are different

Page 75: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Dynamic Memory

75

−memory can be allocated during with new

−can check for success/failure

Page 76: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Dynamic Memory

76

−memory can (and should) be during run time

with delete

−can also use (from C), but don’t

mix

Page 77: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Dynamic Memory

77

Page 78: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Data Structures

78

−struct

−or

−access

Page 79: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Data Structures

79

Page 80: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Data Structures

80

Page 81: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Data Structures

81

Page 82: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Data Structures

82

−pointers to _________________

−different from

Page 83: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Data Structures

83

− structs

−access

Page 84: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Other Data Structures

84

− type ________________

−can be used as

−with clause

Page 85: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Other Data Structures

85

−union

−can be accessed as

Page 86: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Other Data Structures

86

−union

Page 87: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Other Data Structures

87

−anonymous union

Page 88: Introduction to C++ - Computer Sciencetadavis/cs303/cppsf.pdf · Introduction 2 −C++ ... −first called “C with Classes ... Other Data Structures 84

Enumerated Types

88

−can assign integer values (assigned anyway starting at )