References and Pointers Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University
98
References and Pointers Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Based on the book: Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount) Some content derived/taken from: http://www.stroustrup.com/Programming/ and some from C++ Through Game Programming (Dawson) Connect Speakers for this Presentation
Department of Mathematics Statistics and Computer Science
University of Wisconsin ndash Stout
Based on the book Data Structures and Algorithms in C++ (Goodrich Tamassia Mount)Some content derivedtaken from httpwwwstroustrupcomProgramming and some from C++ Through Game Programming (Dawson)
Connect Speakersfor this Presentation
Sound Check
>
Things to Note
bull Homework 3 is Due Soon (tonight)
bull Homework 4 is Posted on D2Lndash Do NOT delay in starting it
bull Do not forget to look at the Meta-Info files
From Last Timebull ADTs
ndash Describe the what --- the interfacebull but not the how --- the implementation
bull UML Diagramsndash Standard way to present an ADT
bull plus some
bull C++ classes have the what and howndash Interface header h files correspond to the whatndash Implementation cpp files correspond to the how
bull So all 3 are relatedndash ADT UML C++ Classes
bull Object Oriented Programmingndash C++ Classes have data and methods and allow for inheritancendash Variables that are types of C++ Classes can be viewed as Objects
For Today
bull Review some C++ Class stuffndash Relate to other user-defined types
bull structsbull enum types
ndash Glance at operator overloading
bull References and Pointersndash These are the main topic for today
Marker Slidebull Any General Questions
bull Next upndash Review
bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
C++ Classes ndash Revisited
bull Classesndash have 2 Parts
bull Interface and Implementation
ndash and are made up ofbull Constructorsbull Member Functionsbull Member Data
Classes ndash The Idea (per Stroustrup)bull A class represents a concept in a program
ndash If you can think of ldquoitrdquo as a separate entityThen it is plausible ldquoitrdquo could be a class or an object of a class
bull A class is a (user-defined) type that specifies how objects of its type can be created and used
bull A class is the key building block for large programsndash And pretty useful for small ones too
bull The concept originated in Simula67
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Struct and Classbull A struct is a class where members are public by default
bull So
bull Means
struct Y int m
class Y public int m
As opposed to classes where all member data defaults to private
Marker Slidebull Any questions on
ndash Review C++ classes
bull Next upndash Review
bull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Sound Check
>
Things to Note
bull Homework 3 is Due Soon (tonight)
bull Homework 4 is Posted on D2Lndash Do NOT delay in starting it
bull Do not forget to look at the Meta-Info files
From Last Timebull ADTs
ndash Describe the what --- the interfacebull but not the how --- the implementation
bull UML Diagramsndash Standard way to present an ADT
bull plus some
bull C++ classes have the what and howndash Interface header h files correspond to the whatndash Implementation cpp files correspond to the how
bull So all 3 are relatedndash ADT UML C++ Classes
bull Object Oriented Programmingndash C++ Classes have data and methods and allow for inheritancendash Variables that are types of C++ Classes can be viewed as Objects
For Today
bull Review some C++ Class stuffndash Relate to other user-defined types
bull structsbull enum types
ndash Glance at operator overloading
bull References and Pointersndash These are the main topic for today
Marker Slidebull Any General Questions
bull Next upndash Review
bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
C++ Classes ndash Revisited
bull Classesndash have 2 Parts
bull Interface and Implementation
ndash and are made up ofbull Constructorsbull Member Functionsbull Member Data
Classes ndash The Idea (per Stroustrup)bull A class represents a concept in a program
ndash If you can think of ldquoitrdquo as a separate entityThen it is plausible ldquoitrdquo could be a class or an object of a class
bull A class is a (user-defined) type that specifies how objects of its type can be created and used
bull A class is the key building block for large programsndash And pretty useful for small ones too
bull The concept originated in Simula67
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Struct and Classbull A struct is a class where members are public by default
bull So
bull Means
struct Y int m
class Y public int m
As opposed to classes where all member data defaults to private
Marker Slidebull Any questions on
ndash Review C++ classes
bull Next upndash Review
bull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Things to Note
bull Homework 3 is Due Soon (tonight)
bull Homework 4 is Posted on D2Lndash Do NOT delay in starting it
bull Do not forget to look at the Meta-Info files
From Last Timebull ADTs
ndash Describe the what --- the interfacebull but not the how --- the implementation
bull UML Diagramsndash Standard way to present an ADT
bull plus some
bull C++ classes have the what and howndash Interface header h files correspond to the whatndash Implementation cpp files correspond to the how
bull So all 3 are relatedndash ADT UML C++ Classes
bull Object Oriented Programmingndash C++ Classes have data and methods and allow for inheritancendash Variables that are types of C++ Classes can be viewed as Objects
For Today
bull Review some C++ Class stuffndash Relate to other user-defined types
bull structsbull enum types
ndash Glance at operator overloading
bull References and Pointersndash These are the main topic for today
Marker Slidebull Any General Questions
bull Next upndash Review
bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
C++ Classes ndash Revisited
bull Classesndash have 2 Parts
bull Interface and Implementation
ndash and are made up ofbull Constructorsbull Member Functionsbull Member Data
Classes ndash The Idea (per Stroustrup)bull A class represents a concept in a program
ndash If you can think of ldquoitrdquo as a separate entityThen it is plausible ldquoitrdquo could be a class or an object of a class
bull A class is a (user-defined) type that specifies how objects of its type can be created and used
bull A class is the key building block for large programsndash And pretty useful for small ones too
bull The concept originated in Simula67
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Struct and Classbull A struct is a class where members are public by default
bull So
bull Means
struct Y int m
class Y public int m
As opposed to classes where all member data defaults to private
Marker Slidebull Any questions on
ndash Review C++ classes
bull Next upndash Review
bull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
From Last Timebull ADTs
ndash Describe the what --- the interfacebull but not the how --- the implementation
bull UML Diagramsndash Standard way to present an ADT
bull plus some
bull C++ classes have the what and howndash Interface header h files correspond to the whatndash Implementation cpp files correspond to the how
bull So all 3 are relatedndash ADT UML C++ Classes
bull Object Oriented Programmingndash C++ Classes have data and methods and allow for inheritancendash Variables that are types of C++ Classes can be viewed as Objects
For Today
bull Review some C++ Class stuffndash Relate to other user-defined types
bull structsbull enum types
ndash Glance at operator overloading
bull References and Pointersndash These are the main topic for today
Marker Slidebull Any General Questions
bull Next upndash Review
bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
C++ Classes ndash Revisited
bull Classesndash have 2 Parts
bull Interface and Implementation
ndash and are made up ofbull Constructorsbull Member Functionsbull Member Data
Classes ndash The Idea (per Stroustrup)bull A class represents a concept in a program
ndash If you can think of ldquoitrdquo as a separate entityThen it is plausible ldquoitrdquo could be a class or an object of a class
bull A class is a (user-defined) type that specifies how objects of its type can be created and used
bull A class is the key building block for large programsndash And pretty useful for small ones too
bull The concept originated in Simula67
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Struct and Classbull A struct is a class where members are public by default
bull So
bull Means
struct Y int m
class Y public int m
As opposed to classes where all member data defaults to private
Marker Slidebull Any questions on
ndash Review C++ classes
bull Next upndash Review
bull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
For Today
bull Review some C++ Class stuffndash Relate to other user-defined types
bull structsbull enum types
ndash Glance at operator overloading
bull References and Pointersndash These are the main topic for today
Marker Slidebull Any General Questions
bull Next upndash Review
bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
C++ Classes ndash Revisited
bull Classesndash have 2 Parts
bull Interface and Implementation
ndash and are made up ofbull Constructorsbull Member Functionsbull Member Data
Classes ndash The Idea (per Stroustrup)bull A class represents a concept in a program
ndash If you can think of ldquoitrdquo as a separate entityThen it is plausible ldquoitrdquo could be a class or an object of a class
bull A class is a (user-defined) type that specifies how objects of its type can be created and used
bull A class is the key building block for large programsndash And pretty useful for small ones too
bull The concept originated in Simula67
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Struct and Classbull A struct is a class where members are public by default
bull So
bull Means
struct Y int m
class Y public int m
As opposed to classes where all member data defaults to private
Marker Slidebull Any questions on
ndash Review C++ classes
bull Next upndash Review
bull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Marker Slidebull Any General Questions
bull Next upndash Review
bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
C++ Classes ndash Revisited
bull Classesndash have 2 Parts
bull Interface and Implementation
ndash and are made up ofbull Constructorsbull Member Functionsbull Member Data
Classes ndash The Idea (per Stroustrup)bull A class represents a concept in a program
ndash If you can think of ldquoitrdquo as a separate entityThen it is plausible ldquoitrdquo could be a class or an object of a class
bull A class is a (user-defined) type that specifies how objects of its type can be created and used
bull A class is the key building block for large programsndash And pretty useful for small ones too
bull The concept originated in Simula67
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Struct and Classbull A struct is a class where members are public by default
bull So
bull Means
struct Y int m
class Y public int m
As opposed to classes where all member data defaults to private
Marker Slidebull Any questions on
ndash Review C++ classes
bull Next upndash Review
bull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
bull A class is a (user-defined) type that specifies how objects of its type can be created and used
bull A class is the key building block for large programsndash And pretty useful for small ones too
bull The concept originated in Simula67
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Struct and Classbull A struct is a class where members are public by default
bull So
bull Means
struct Y int m
class Y public int m
As opposed to classes where all member data defaults to private
Marker Slidebull Any questions on
ndash Review C++ classes
bull Next upndash Review
bull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
bull A class is a (user-defined) type that specifies how objects of its type can be created and used
bull A class is the key building block for large programsndash And pretty useful for small ones too
bull The concept originated in Simula67
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Struct and Classbull A struct is a class where members are public by default
bull So
bull Means
struct Y int m
class Y public int m
As opposed to classes where all member data defaults to private
Marker Slidebull Any questions on
ndash Review C++ classes
bull Next upndash Review
bull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Struct and Classbull A struct is a class where members are public by default
bull So
bull Means
struct Y int m
class Y public int m
As opposed to classes where all member data defaults to private
Marker Slidebull Any questions on
ndash Review C++ classes
bull Next upndash Review
bull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Struct and Classbull A struct is a class where members are public by default
bull So
bull Means
struct Y int m
class Y public int m
As opposed to classes where all member data defaults to private
Marker Slidebull Any questions on
ndash Review C++ classes
bull Next upndash Review
bull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Struct and Classbull A struct is a class where members are public by default
bull So
bull Means
struct Y int m
class Y public int m
As opposed to classes where all member data defaults to private
Marker Slidebull Any questions on
ndash Review C++ classes
bull Next upndash Review
bull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Members and Member Access
bull Generically
bull Example
class X the class name is X It has data members to store information It has function members to do things using the information
class X public int m data member int mf(int v) int old=m m=v return old function member
X aVar aVar is a variable if type X
aVarm = 10 access aVarrsquos data member m
int x = aVarmf(12) call aVarrsquos member function mf()
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Struct and Classbull A struct is a class where members are public by default
bull So
bull Means
struct Y int m
class Y public int m
As opposed to classes where all member data defaults to private
Marker Slidebull Any questions on
ndash Review C++ classes
bull Next upndash Review
bull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Struct and Classbull A struct is a class where members are public by default
bull So
bull Means
struct Y int m
class Y public int m
As opposed to classes where all member data defaults to private
Marker Slidebull Any questions on
ndash Review C++ classes
bull Next upndash Review
bull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Classes Summary again for compare to next slide
bull A class is a user-defined type
class X the class name is X public public members the user interface accessible by all public functions public types public data (though most data is kept private)
private private members implementation details accessible by members of this class only private functions private types private data
Struct and Classbull A struct is a class where members are public by default
bull So
bull Means
struct Y int m
class Y public int m
As opposed to classes where all member data defaults to private
Marker Slidebull Any questions on
ndash Review C++ classes
bull Next upndash Review
bull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Struct and Classbull A struct is a class where members are public by default
bull So
bull Means
struct Y int m
class Y public int m
As opposed to classes where all member data defaults to private
Marker Slidebull Any questions on
ndash Review C++ classes
bull Next upndash Review
bull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Marker Slidebull Any questions on
ndash Review C++ classes
bull Next upndash Review
bull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Another Type - Enumerationbull Classes are user-defined typesbull Structs are user-defined typesbull An enum (enumeration) is another user defined type
specifying a set of values (its enumerators)
bull Example
enum Month jan=1 feb mar apr may jun jul aug sep oct nov dec
Month m = octm = 3 error cannot assign int to Monthint n = m ok can get the numeric value of a MonthMonth mm = Month(7) auto-convert int to Month (unchecked)
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Inside the Enumerationbull What names you use matter
bull Enums are a list of constantsndash enum red blue enum does not define a scope
ndash int a = red so the constant red is available here
ndash enum red green black error red defined twice
bull Cannot mix enum typesndash Month m1 = jan is ok
ndash Month m2 = red error red is not a Month
ndash Month m3 = 5 error 5 is not a Month
bull Can assign enums to integer (short int long) typesndash int i = m1
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Enum Valuesbull By default
ndash the first enumerator in the list has value ZEROndash the next enumerator has 1+value of enumerator before itndash enum duck frog cat
bull duck ==0 frog == 1 cat == 2
bull Numbering can be controlledndash enum jan=1 feb mar and so on
bull jan == 1 feb == 2 mar = 3 hellip
ndash enum status good=1 fail=2 bad=4 zerg=10 bull aside
int flags = good + zerg flags == 11
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Marker Slidebull Any questions on
ndash Review bull C++ classesbull Enumerated Types
bull Next upndash Review
bull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
Operator Overloadingbull You can define almost all C++ operators
for class struct or enumeration operandsndash ie operator overloading
bull Example using enumerated types
enum Month jan=1 feb mar apr may jun jul aug sep oct
nov dec
Month operator++(Monthamp m) prefix increment operator
m = (m==dec) jan Month(m+1) ldquowrap aroundrdquoreturn m
Month m = nov++m m becomes dec++m m becomes jan
This is a fun C language trick It is really an if-then-else
Operator Overloading ndash Class Examplebull This example overloads the operator ltlt
ndash also making it (the global operatorltlt) a friend of the class
class SimplePerson public SimplePerson(const stringamp nameStr const stringamp idStr)
Output operator so stdcout can output a SimplePerson object friend ostreamamp operatorltlt(ostreamamp ostr const SimplePersonamp sp)
private string m_nameStr the prefix m_ stands for member variable string m_idStr it is just a naming convention
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Operator Overloading ndash Restrictions
bull You can only overload existing operatorsndash + - [] () ^ amp lt lt= gt gt= ++ --
bull You cannot change the number of operands an operator normally takesndash lt= requires two operandsndash requires one operand
bull An overloaded operator must have at least one user defined typendash int operator+(int int) wonrsquot work
bull General rulesndash Donrsquot change the meaning of an operator
bull + should mean something like plus
ndash Donrsquot overload unless you really need to
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
bull Next upndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
You Know Whatrsquos Funny
bull If you donrsquot get this joke nowhellip you willhellip oh you will gt=)
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
so upon return from the function values ARE CHANGED
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Additional Examples Available
bull EX033_InventDisplaycppndash passing vectors of strings by reference
bull EX035_TicTacToendash game using various Reference conceptsndash as well as more interesting C++ stuff
bull Left for students to investigate on their own
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++
bull Next upndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointers
bull A pointer is ndash a variable that contains a memory address
bull Pointers allow us ndash to work directly ndash and efficiently with computer memory
bull Like iterators from STL they are often used to access contents of other variables
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
>
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointers ndash What are they
bull A pointer variable isndash a variable whose value is the address of a memory
location
bull Examplesndash char chPtr pointer to a single charndash int aryOfPtrs[12] array of pointers to integers
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing Program ndash Examplebull The example that follows this slide
ndash demonstrates the mechanics of pointersndash creates a variable for a scorendash creates a pointer to store the address of that variable
ndash Program then showsbull when changing the variable directly
the pointer will reflect the changebull you can change the value of the variable using the pointerbull you can make the pointer point to a newdifferent variable
ndash Also showsbull pointers work with built in types and user defined types
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Graded In-Class Activity ndash Pointingbull Take 10 to 20 minutes
bull Enter Compile and Run the following programbull Submit your code to D2L before the end of class
today Pointing Demonstrates using pointersinclude ltiostreamgtinclude ltstringgtusing namespace stdint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of variable score cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
In class ldquocode walk throughrdquo follows this slide to explain the code
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include iostream for stdcout stdcin stdendl etc
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
include string for stdstring
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
Use the standard name space to avoid typing ldquostdrdquo everywhere
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
main function ndash entry point to begin running stuff for all C++ programs
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
A pointer to an integer ndash NOT initialized which is BAD --- ALWAYS initialize your variables
The prefix ldquoprdquo on the variable name is a naming convention to indicate it is a pointer Aside You will see ldquomp_rdquo in classes to indicate member pointers
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pAPointer
Where you put the asterisk is up to youint pApointer
andint pApointer
are both equally valid Just be consistent
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int pScore = 0
pointer to an integer initialized to point at address zeroor rather point at nothing
by convention and good practice this SHOULD BE WRITTEN asint pScore = NULL
NULL is a defined constant in iostream where NULL == 0
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
int score = 1000
Declare and initialize an integer variable to have the value 1000
Understand that by doing this the variable score has been assigned and is using a memory location somewhere in the computer
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
The amp operator returns the address of the variable immediately after it
So ampscore returns the memory address of the variable score
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Looking at Code Pointing Demonstrates using pointers
include ltiostreamgtinclude ltstringgt
using namespace std
int main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
pScore = ampscore
Assigns the memory address of the variable named score in the variable named pScore
This is said to make ldquopScore point to scorerdquo
This allows fun things to be done using pScore
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To ldquoproverdquo pScore contains the address of score both are printed to the screen
And it will say something likeampscore is 0x234a5cpScore is 0x234a5c
As long as the numbers are the same all is good
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
In English if you ldquopoint to somethingrdquo you are ldquoreferencingrdquo it
So if we want to see what a pointer is pointing towe must DEREFERENCE it
To do this we put an asterisk in front of the pointer variableand that dereferences the pointer (it undoes what the declaration did)
Basically the same thing that we did for iterators (sticky notes)
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score
and pScore (via dereferencing)
cout ltlt pScore will display the value of what pScore is pointing at
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointersint main() int pAPointer declare a pointer int pScore = 0 declare and initialize a pointer int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer
cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer
To complete the ldquoproofrdquo that score and pScore are related in a useful way we print out the value of score and pScore
and pScore (via dereferencing)
Output should bescore is 1000pScore is 1000
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointers int score = 1000 pScore = ampscore assign pointer pScore address of a variable score
cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
Here we add 500 to score
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointers cout ltlt Assigning ampscore to pScoren cout ltlt ampscore is ltlt ampscore ltlt n address of score variable cout ltlt pScore is ltlt pScore ltlt n address stored in pointer cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn value pointed to by pointer cout ltlt Adding 500 to scoren score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
In similar fashion we add 500 to pScoreNote it is dereferenced in the line pScore += 500
and show both the value of score and pScore change correctly
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointers score += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Adding 500 to pScoren pScore += 500 cout ltlt score is ltlt score ltlt n cout ltlt pScore is ltlt pScore ltlt nn
cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn
You can change what a pointer is referencingHere we create a new variable named newScore
Which is at a different memory address than score
We assign the address of newScore into pScore(notice NO dereferencing in the line pScore = ampnewScore)
And we print out addresses and valuesShowing pScore now references newScore (and not score)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects tooHere we create a variable of class stdstring
initialized to the word ldquoscorerdquo
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
Pointers can be used with class objects too
We also must create a new pointer variable named pStrThis is a variable that can point to type string
and we initialize it to point to the variable named str(recall ampstr returns the memory address of str)
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
We print out the value of variable str
and the value of the thing referenced by pStr via dereferencing pStr pStr
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointing ndash Using Pointers cout ltlt Assigning ampnewScore to pScoren int newScore = 5000 pScore = ampnewScore cout ltlt ampnewScore is ltlt ampnewScore ltlt n cout ltlt pScore is ltlt pScore ltlt n cout ltlt newScore is ltlt newScore ltlt n cout ltlt pScore is ltlt pScore ltlt nn cout ltlt Assigning ampstr to pStrn string str = score string pStr = ampstr pointer to string object cout ltlt str is ltlt str ltlt n cout ltlt pStr is ltlt pStr ltlt n cout ltlt (pStr)size() is ltlt (pStr)size() ltlt n cout ltlt pStr-gtsize() is ltlt pStr-gtsize() ltlt n return 0
As with iterators we can likewise dereference a pointer (pStr)
and call a member function of the dereferenced object (pStr)size()
This remains not very pretty and difficult to type so we can also use thearrow -gt (minus sign followed by greater than) operator to call member functions of the referenced object
pStr-gtsize()
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Quick Review Follows
bull Pointers reference or point to other variables
bull This is accomplished because pointers hold the address of the other variables
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Getting the Address via the Address Operator amp
bull Note the value pointed to by ptr is denoted ptrbull So the above cout will output what to the screen
make ptr ldquopointrdquo at x
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
8000
17
x
5600
8000
ptr
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Getting the value of the referenced object via Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 17
int ptr ptr = ampx
cout ltlt ptr
ptr = 42
8000
17
x
5600
8000
ptr
42
Same code as previous slide
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Dereferencing using
bull Change the value at the address ptr points toto be 42
int x x = 12
int ptr ptr = ampx
ptr = 5
8000
17
x
5600
8000
ptr
42
>
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activity
bull Next upndash Pointers in C++
bull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Pointers As Parameters to Function
bull Pointers can be used as parameters to functions
bull This provides an alternative way to achieve sort ofa pass by reference effectndash Officially called pass by pointer
Class Group Activity ndash SwapPointer
bull Find someone to talk tondash Take 5 to 10 minutes to download the SwapPointer
Code from D2Lbull File named similar to EX038_SwapPtrVersion
ndash Compare that program to the Swap program that used only references
ndash Illustrates Pointers VERSUS References
Swap ndash Pointer Version Swap Pointer Demos passing constant ptrs to alter argument variables
include ltiostreamgt
using namespace std
void badSwap(int x int y)void goodSwap(int const pX int const pY)
int main() int myScore = 150 int yourScore = 1000 cout ltlt Original valuesn cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn cout ltlt Calling badSwap()n badSwap(myScore yourScore) cout ltlt myScore ltlt myScore ltlt n cout ltlt yourScore ltlt yourScore ltlt nn
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
void badSwap(int x int y) int temp = x x = y y = temp
void goodSwap(int const pX int const pY) store value pointed to by pX in temp
int temp = pX store value pointed to by pY in address pointed to by pX
pX = pY store value originally pointed to by pX in address pointed to by pY
pY = temp
Swap ndash Pointer Version
dereference the pointer pYusing the asterisk to set the value of what pY referencesto be the value of temp
pY = temp
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)
bull Next upndash Pointers in C++
bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
On Your Own ndash Inventory Program
bull On D2L there is an Inventory program you can look at to see how returning a pointer can be donendash EX040_InventoryPtrcpp
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)
bull Next upndash Pointers in C++
bull Arrays as pointersbull Pointers to Classesbull Tic-Tac-Toe via pointers
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Array Namesbull Array names are constant pointers
ndash and can be used as such
bull Given a function such as
bull In main() you can do something like
void display(const int inventArray const int arraySize)
int main() int Inventory[450] display(Inventory 450) the array name Inventory works as a const int
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
On Your Own ndash Example Availablebull On D2L there should be an example demonstrating the relationship
between pointers and arraysndash Named something like
bull EX045_ArrayPasserArray PasserDemonstrates relationship between pointers and arraysinclude ltiostreamgtusing namespace stdvoid increase(int const arrayName const int arraySize)void display(const int const arrayName const int arraySize)int main() cout ltlt Creating a scoreArray of high scoresnn const int NUM_SCORES = 3 int highScores[NUM_SCORES] = 5000 3500 2700 cout ltlt Displaying scores using scoreArray name as a constant pointern cout ltlt highScores ltlt endl cout ltlt (highScores + 1) ltlt endl cout ltlt (highScores + 2) ltlt nn cout ltlt Increasing scores by passing scoreArray as a constant pointernn increase(highScores NUM_SCORES) cout ltlt Displaying scores by passing scoreArray as a constant pointer to a constantn display(highScores NUM_SCORES) return 0
void increase(int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) arrayName[i] += 500
void display(const int const arrayName const int arraySize) for (int i = 0 i lt arraySize ++i) cout ltlt arrayName[i] ltlt endl
Investigateon your own
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointers
bull Next upndash Pointers in C++
bull Pointers to Classesbull Tic-Tac-Toe via pointers (Last Class Activity)
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main points [go to next slide]
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Pointers to Classes pointer to classes exampleinclude ltiostreamgtusing namespace std
class CRectangle int width height public void set_values (int int) int area (void) return (width height)void CRectangleset_values (int a int b) width = a height = b
int main () CRectangle a b c CRectangle d = new CRectangle[2] b= new CRectangle c= ampa aset_values (12) b-gtset_values (34) d-gtset_values (56) d[1]set_values (78)
a area 2b area 12c area 2d[0] area 30d[1] area 56
Main pointsYou can have pointers to classes too
The use of new delete and delete []will be discussed in the future (next class)
cout ltlt a area ltlt aarea() ltlt endl cout ltlt b area ltlt b-gtarea() ltlt endl cout ltlt c area ltlt c-gtarea() ltlt endl cout ltlt d[0] area ltlt d[0]area() ltlt endl cout ltlt d[1] area ltlt d[1]area() ltlt endl delete[] d delete b return 0
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Pointers to Classes
bull Next upndash Pointers in C++
bull Tic-Tac-Toe via pointers (Last Class Activity)
In-Class Activity TicTacToe Ptr Versionbull Posted on D2L is a pointer version of TicTacToe
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
bull Group together and discuss the differences between the pointer version and the reference version
bull And that should be the last activity for todayhellipndash [next slide]
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers
Classes Summary again for compare to next slide (2)
Struct and Class
Marker Slide (2)
Another Type - Enumeration
Inside the Enumeration
Enum Values
Marker Slide (3)
Operator Overloading
Operator Overloading (2)
Operator Overloading ndash Class Example
Operator Overloading ndash Restrictions
Marker Slide (4)
You Know Whatrsquos Funny
C++ References
Class Example ndash Referencing
Class Activity ndash Discussion
Why Use References
Class Example ndash Swap
Examination of ndash Swap
Examination of ndash Swap (2)
Examination of ndash Swap (3)
Examination of ndash Swap (4)
Additional Examples Available
Marker Slide (5)
Pointers
Pointers ndash What are they
Pointers ndash What are they (2)
Pointing Program ndash Example
Graded In-Class Activity ndash Pointing
Pointing ndash Looking at Code
Pointing ndash Looking at Code (2)
Pointing ndash Looking at Code (3)
Pointing ndash Looking at Code (4)
Pointing ndash Looking at Code (5)
Pointing ndash Looking at Code (6)
Pointing ndash Looking at Code (7)
Pointing ndash Looking at Code (8)
Pointing ndash Looking at Code (9)
Pointing ndash Looking at Code (10)
Pointing ndash Using Pointers
Pointing ndash Using Pointers (2)
Pointing ndash Using Pointers (3)
Pointing ndash Using Pointers (4)
Pointing ndash Using Pointers (5)
Pointing ndash Using Pointers (6)
Pointing ndash Using Pointers (7)
Pointing ndash Using Pointers (8)
Pointing ndash Using Pointers (9)
Pointing ndash Using Pointers (10)
Pointing ndash Using Pointers (11)
Pointing ndash Using Pointers (12)
Pointing ndash Using Pointers (13)
Pointing ndash Using Pointers (14)
Pointing ndash Using Pointers (15)
Pointing ndash Using Pointers (16)
Pointing ndash Using Pointers (17)
Pointing ndash Using Pointers (18)
Quick Review Follows
Getting the Address via the Address Operator amp
Getting the value of the referenced object via Dereferencing us
Dereferencing using
Marker Slide (6)
Pointers As Parameters to Function
Class Group Activity ndash SwapPointer
Swap ndash Pointer Version
Swap ndash Pointer Version (2)
Swap ndash Pointer Version (3)
Swap ndash Pointer Version (4)
Swap ndash Pointer Version (5)
Swap ndash Pointer Version (6)
Swap ndash Pointer Version (7)
Marker Slide (7)
On Your Own ndash Inventory Program
Marker Slide (8)
Array Names
On Your Own ndash Example Available
Marker Slide (9)
Pointers to Classes
Pointers to Classes (2)
Pointers to Classes (3)
Marker Slide (10)
In-Class Activity TicTacToe Ptr Version
Marker Slide (11)
Free Play ndash Things to Work On
The End
Marker Slidebull Any questions on
ndash Review bull C++ Classesbull Enumerated Typesbull Operator Overloading
ndash References in C++ndash Pointers in C++
bull Pointers Program in-class activitybull Pointers as parameters to functions (Swap)bull Returning Ptrs from function (on your own - InventoryPtr)bull Arrays as pointersbull Tic-Tac-Toe via pointers