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

Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Introduction to C++

with content from www.cplusplus.com

Page 2: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Introduction

2

−C++

−widely-used general-purpose programming language

−compiled

−procedural and object-oriented support

−strong library support

−created by Bjarne Stroustrup starting in 1979

−based on C

−first called “C with Classes”

−also with inheritance, inlining, default function

arguments, and strong type checking

−many C programs compile with C++ compiler

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

Page 3: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Structure of a C++ Program

3

Page 4: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Structure of a C++ Program

4

−previous program could also be written as follows

Page 5: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Structure of a C++ Program

5

− two styles of comments

Page 6: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Structure of a C++ Program

6

−namespace

Page 7: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Identifiers

7

−similar to rules for Python identifiers

−case-sensitive

−keywords

Page 8: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Types

8

− fundamental types

Page 9: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Variables

9

−must be declared

Page 10: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Initializing Variables

10

−different ways to initialize variables at declaration

Page 11: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Variables

11

−automatic type deduction

−with initialization

−without initialization

−used in cases where type cannot be obtained easily for

generality

Page 12: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Strings

12

Page 13: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Literals

13

− integers

− floats

−chars

Page 14: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Literals

14

−escape sequences

Page 15: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Constants

15

− typed constants

Page 16: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Constants

16

−#define constants

Page 17: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Increment/Decrement

17

−prefix/postfix

Page 18: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Operators

18

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

−AND/OR

−other operators work similarly to Python

Page 19: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Ternary Operator

19

−condition ? result1 : result2

Page 20: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Bitwise Operators

20

Page 21: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Type Casting

21

−both OK

Page 22: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Operator Precedence

22

Page 23: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Input/Output

23

Page 24: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Input/Output

24

Page 25: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

if Statements

25

−compound if

Page 26: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Iteration

26

−while statement

Page 27: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Iteration

27

−do-while statement

Page 28: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Iteration

28

− for loop

Page 29: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Iteration

29

− range-based for loop

Page 30: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

break Statement

30

−break

Page 31: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

continue Statement

31

−continue

Page 32: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

switch Statement

32

Page 33: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Functions

33

Page 34: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Functions

34

Page 35: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Functions

35

Page 36: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Functions

36

− return value from main

Page 37: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Functions

37

−pass by value vs. pass by reference

Page 38: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Functions

38

− inline functions

Page 39: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Functions

39

−default values for parameters

Page 40: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Functions

40

− function prototypes

Page 41: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Functions

41

− recursion

Page 42: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Scope

42

−global vs. local variables

Page 43: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Scope

43

−name can only represent one entity

Page 44: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Scope

44

−block scope

Page 45: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Arrays

45

−contiguous memory locations

Page 46: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Initializing Arrays

46

−elements not automatically initialized, but can be explicitly

initialized

Page 47: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Initializing Arrays

47

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

Page 48: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Initializing Arrays

48

− initialized arrays without size are automatically sized to

accommodate values

−can be initialized without =

−no error if range exceeded

−example uses of arrays

Page 49: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Arrays

49

Page 50: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Multidimensional Arrays

50

−arrays of arrays

Page 51: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Multidimensional Arrays

51

−can be any dimension, but space increases exponentially

−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++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Multidimensional Arrays

52

Page 53: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Arrays as Parameters

53

Page 54: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Character Arrays

54

−sets aside space, but not initialized

Page 55: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Character Arrays

55

−can initialize with individual elements or string literals

−not valid

−OK

Page 56: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Strings and Character Arrays

56

Page 57: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Strings and Character Arrays

57

−can be transformed one to another

Page 58: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Pointers

58

−pointer – the address of something

−exact memory locations unknown at compile time

−use & to get the address of a variable

Page 59: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Pointers

59

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

Page 60: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Pointers

60

−& and * are complementary

−with following assignments

−all of the following are true

Page 61: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Declaring Pointers

61

−all are the same size in memory

−different

Page 62: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Pointers

62

Page 63: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Pointers

63

Page 64: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Pointers and Arrays

64

−array name with no index is a pointer to the first element

−arrays can always be converted to pointers

−not valid to go the other way

Page 65: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Pointers and Arrays

65

v

Page 66: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Pointers and Arrays

66

−array with index is a simply a pointer with an offset

−can be represented with pointer

Page 67: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Pointer Initialization

67

−pointers can be initialized at declaration

−same as

−not valid

−OK

Page 68: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Pointer Arithmetic

68

−pointers can be used in arithmetic expressions, with

underlying size 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++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Pointer Arithmetic

69

− the following is equivalent to *(p++)

−other examples

−assignment done before increment

−same as

Page 70: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Pointers and const

70

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

−pointers can be const

−same

Page 71: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Pointers and const

71

−pointers are not const, so can be modified

Page 72: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Pointers to Pointers

72

Page 73: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

void Pointers

73

−void pointers point to no particular type

Page 74: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Pointers

74

−pointers can point to any address

−pointers can point to nothing

−or simply

−NULL pointers and void pointers are different

Page 75: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Dynamic Memory

75

−memory can be allocated during run time with new

−can check for success/failure

Page 76: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Dynamic Memory

76

−memory can (and should) be de-allocated during run time

with delete

−can also use malloc/free (from C), but don’t mix

Page 77: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Dynamic Memory

77

Page 78: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Data Structures

78

−struct

−or

−access

Page 79: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Data Structures

79

Page 80: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Data Structures

80

Page 81: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Data Structures

81

Page 82: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Data Structures

82

−pointers to structs

−different from

Page 83: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Data Structures

83

−nested structs

−access

Page 84: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Other Data Structures

84

− type aliases

−can be used as

−with using clause

Page 85: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Other Data Structures

85

−union

−can be accessed as

Page 86: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Other Data Structures

86

−union

Page 87: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Other Data Structures

87

−anonymous union

Page 88: Introduction to C++tadavis/cs303/f16/cpp.pdf · Introduction 2 −C++ −widely-used general-purpose programming language −compiled −procedural and object-oriented support −strong

Enumerated Types

88

−can assign integer values (assigned anyway starting at 0)