introduction to c++tadavis/cs303/f16/cpp.pdf · introduction 2 −c++ −widely-used...
TRANSCRIPT
![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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/26.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/27.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/28.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/29.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/30.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/31.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/32.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/33.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/34.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/35.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/36.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/37.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/38.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/39.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/40.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/41.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/42.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/43.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/44.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/45.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/46.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/47.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/48.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/49.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/50.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/51.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/52.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/53.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/54.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/55.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/56.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/57.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/58.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/59.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/60.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/61.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/62.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/63.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/64.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/65.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/66.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/67.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/68.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/69.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/70.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/71.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/72.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/73.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/74.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/75.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/76.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/77.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/78.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/79.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/80.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/81.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/82.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/83.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/84.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/85.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/86.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/87.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0f54bd7e708231d443a11c/html5/thumbnails/88.jpg)
Enumerated Types
88
−can assign integer values (assigned anyway starting at 0)