References and Pointers Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.

Slides:



Advertisements
Similar presentations
Chapter 9 Technicalities: Classes, etc. Bjarne Stroustrup
Advertisements

Lectures 10 & 11.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Chapter 14: Overloading and Templates
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Flashback Technicalities: Classes, etc. Lecture 13 Hartmut Kaiser
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
General Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Pointers OVERVIEW.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Review and Prepare for Test 1 Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
POINTERS.
Review 1 List Data Structure List operations List Implementation Array Linked List.
References and Pointers CS 244 Connect Speakers for this Presentation Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics,
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Advanced Classes and Dynamic Memory Allocation Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Reading from a file, Sorting, and a little Searching Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics,
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
Fundamental Programming Fundamental Programming Introduction to Functions.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Object-Oriented Programming (OOP) and C++
A First Book of ANSI C Fourth Edition
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
User-Written Functions
Chapter 7: User-Defined Functions II
C++ Templates.
void Pointers Lesson xx
DATA HANDLING.
One-Dimensional Array Introduction Lesson xx
Popping Items Off a Stack Lesson xx
Pointers, Dynamic Data, and Reference Types
Arrays Kingdom of Saudi Arabia
QUIZ.
COP 3330 Object-oriented Programming in C++
Arrays Arrays A few types Structures of related data items
CISC181 Introduction to Computer Science Dr
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

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: and some from C++ Through Game Programming (Dawson) Connect Speakers for this Presentation

Sound Check

Things to Note Homework 3 is Due Soon (tonight) Homework 4 is Posted on D2L – Do NOT delay in starting it Do not forget to look at the Meta-Info files

From Last Time ADTs – Describe the what --- the interface but not the how --- the implementation UML Diagrams – Standard way to present an ADT plus some C++ classes have the what and how – Interface: header.h files correspond to the what – Implementation:.cpp files correspond to the how So all 3 are related – ADT  UML  C++ Classes Object Oriented Programming – C++ Classes have data and methods and allow for inheritance – Variables that are types of C++ Classes can be viewed as Objects

For Today Review some C++ Class stuff – Relate to other user-defined types structs enum types – Glance at: operator overloading References and Pointers – These are the main topic for today

Marker Slide Any General Questions ? Next up – Review C++ Classes Enumerated Types Operator Overloading – References in C++ – Pointers in C++ Pointers Program: in-class activity Pointers as parameters to functions (Swap) Returning Ptrs from function (on your own - InventoryPtr) Arrays as pointers Pointers to Classes Tic-Tac-Toe via pointers

C++ Classes – Revisited Classes – have 2 Parts Interface and Implementation – and are made up of Constructors Member Functions Member Data

Classes – The Idea (per Stroustrup) A class represents a concept in a program – If you can think of “it” as a separate entity Then it is plausible “it” could be a class or an object of a class – Examples: vector, matrix, input stream, string, valve controller, robot arm, device driver, dialog box, graph, window, clock A class is a (user-defined) type that specifies how objects of its type can be created and used A class is the key building block for large programs – And pretty useful for small ones too The concept originated in Simula67

Members and Member Access Generically: 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 Generically: 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 aVar.m = 10; // access aVar’s data member m int x = aVar.mf(12); // call aVar’s member function mf()

Members and Member Access Generically: 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 aVar.m = 10; // access aVar’s data member m int x = aVar.mf(12); // call aVar’s member function mf()

Members and Member Access Generically: 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 aVar.m = 10; // access aVar’s data member m int x = aVar.mf(12); // call aVar’s member function mf()

Classes Summary again for compare to next slide 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 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 Class A struct is a class where members are public by default So Means struct Y { int m; //... }; class Y { public: int m; //... }; As opposed to classes where all member data defaults to private

Marker Slide Any questions on: – Review C++ classes Next up – Review Enumerated Types Operator Overloading – References in C++ – Pointers in C++ Pointers Program: in-class activity Pointers as parameters to functions (Swap) Returning Ptrs from function (on your own - InventoryPtr) Arrays as pointers Pointers to Classes Tic-Tac-Toe via pointers

Another Type - Enumeration Classes are user-defined types Structs are user-defined types An enum (enumeration) is another user defined type specifying a set of values (its enumerators) Example: enum Month { jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec }; Month m = oct; m = 3; // error: cannot assign int to Month int n = m; // ok: can get the numeric value of a Month Month mm = Month(7); // auto-convert int to Month (unchecked)

Inside the Enumeration What names you use matter Enums are a list of constants – enum { red, blue }; // enum { } does not define a scope – int a = red; // so the constant red is available here – enum { red, green, black }; // error: red defined twice Cannot mix enum types – Month m1 = jan; // is ok – Month m2 = red; // error red is not a Month – Month m3 = 5; // error 5 is not a Month Can assign enums to integer (short, int, long) types – int i = m1;

Enum Values By default – the first enumerator in the list has value ZERO – the next enumerator has 1+value of enumerator before it – enum { duck, frog, cat }; duck ==0, frog == 1, cat == 2 Numbering can be controlled – enum { jan=1, feb, mar, /* and so on */ }; jan == 1, feb == 2, mar = 3, … – enum status { good=1, fail=2, bad=4, zerg=10 }; aside: int flags = good + zerg; // flags == 11

Marker Slide Any questions on: – Review C++ classes Enumerated Types Next up – Review Operator Overloading – References in C++ – Pointers in C++ Pointers Program: in-class activity Pointers as parameters to functions (Swap) Returning Ptrs from function (on your own - InventoryPtr) Arrays as pointers Pointers to Classes Tic-Tac-Toe via pointers

Operator Overloading You can define almost all C++ operators for class, struct, or enumeration operands – i.e. operator overloading Example using enumerated types enum Month { jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec }; Month operator++(Month& m)// prefix increment operator { m = (m==dec) ? jan : Month(m+1);// “wrap around” return m; } Month m = nov; ++m; // m becomes dec ++m; // m becomes jan

Operator Overloading You can define almost all C++ operators for class, struct, or enumeration operands – i.e. operator overloading Example using enumerated types enum Month { jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec }; Month operator++(Month& m)// prefix increment operator { m = (m==dec) ? jan : Month(m+1);// “wrap around” return 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 – Class Example This example overloads the operator << – also making it (the global operator<<) a friend of the class class SimplePerson { public: SimplePerson(const string& nameStr, const string& idStr); // "Output" operator so std::cout can output a SimplePerson object friend ostream& operator<<(ostream& ostr, const SimplePerson& sp); private: string m_nameStr; // the prefix "m_" stands for member variable string m_idStr; // it is just a naming convention }; ostream& operator<<(ostream& ostr, const SimplePerson& sp) { ostr << sp.m_nameStr << " " << sp.m_idStr; return ostr; }

Operator Overloading – Restrictions You can only overload existing operators – +, -, *, /, %, [], (), ^, !, &,, >=, ++, -- You cannot change the number of operands an operator normally takes – <= requires two operands – ! requires one operand An overloaded operator must have at least one user defined type – int operator+(int, int) // won’t work General rules – Don’t change the meaning of an operator + should mean something like plus – Don’t overload unless you really need to

Marker Slide Any questions on: – Review C++ Classes Enumerated Types Operator Overloading Next up – References in C++ – Pointers in C++ Pointers Program: in-class activity Pointers as parameters to functions (Swap) Returning Ptrs from function (on your own - InventoryPtr) Arrays as pointers Pointers to Classes Tic-Tac-Toe via pointers

You Know What’s Funny If you don’t get this joke now… you will… oh you will >=)

C++ References A reference provides another name for a variable – Whatever you do to a reference is done to the variable to which it refers

Class Example – Referencing Enter and run the following program: // Referencing // Demonstrates using references #include using namespace std; int main() { int myScore = 1000; int& mikesScore = myScore; // create a reference cout << "myScore is: " << myScore << "\n"; cout << "mikesScore is: " << mikesScore << "\n\n"; cout << "Adding 500 to myScore\n"; myScore += 500; cout << "myScore is: " << myScore << "\n"; cout << "mikesScore is: " << mikesScore << "\n\n"; cout << "Adding 500 to mikesScore\n"; mikesScore += 500; cout << "myScore is: " << myScore << "\n"; cout << "mikesScore is: " << mikesScore << "\n\n"; return 0; }

Class Activity – Discussion Enter and run the following program: // Referencing // Demonstrates using references #include using namespace std; int main() { int myScore = 1000; int& mikesScore = myScore; // create a reference cout << "myScore is: " << myScore << "\n"; cout << "mikesScore is: " << mikesScore << "\n\n"; cout << "Adding 500 to myScore\n"; myScore += 500; cout << "myScore is: " << myScore << "\n"; cout << "mikesScore is: " << mikesScore << "\n\n"; cout << "Adding 500 to mikesScore\n"; mikesScore += 500; cout << "myScore is: " << myScore << "\n"; cout << "mikesScore is: " << mikesScore << "\n\n"; return 0; } The reference to myScore allows you to manipulate the value of myScore using the myScore variable or the reference variable mikesScore And all the rest should work as expected

Why Use References One of the main uses of references is to pass a variable to a function “by reference” This means the variable’s value can be changed by the function

Class Example – Swap Enter and run the following program: // Swap // Demonstrates passing references // to alter argument variables #include using namespace std; void badSwap(int x, int y); void goodSwap(int& x, int& y); int main() { int myScore = 150; int yourScore = 1000; cout << "Original values\n"; cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling badSwap()\n"; badSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling goodSwap()\n"; goodSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n"; return 0; } void badSwap(int x, int y) { int temp = x; x = y; y = temp; } void goodSwap(int& x, int& y) { int temp = x; x = y; y = temp; }

Examination of – Swap // Swap // Demonstrates passing references // to alter argument variables #include using namespace std; void badSwap(int x, int y); void goodSwap(int& x, int& y); int main() { int myScore = 150; int yourScore = 1000; cout << "Original values\n"; cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling badSwap()\n"; badSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling goodSwap()\n"; goodSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n"; return 0; } void badSwap(int x, int y) { int temp = x; x = y; y = temp; } void goodSwap(int& x, int& y) { int temp = x; x = y; y = temp; } Global Function declaration prototypes Note that goodSwap is using References

Examination of – Swap // Swap // Demonstrates passing references // to alter argument variables #include using namespace std; void badSwap(int x, int y); void goodSwap(int& x, int& y); int main() { int myScore = 150; int yourScore = 1000; cout << "Original values\n"; cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling badSwap()\n"; badSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling goodSwap()\n"; goodSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n"; return 0; } void badSwap(int x, int y) { int temp = x; x = y; y = temp; } void goodSwap(int& x, int& y) { int temp = x; x = y; y = temp; } Function definitions

Examination of – Swap // Swap // Demonstrates passing references // to alter argument variables #include using namespace std; void badSwap(int x, int y); void goodSwap(int& x, int& y); int main() { int myScore = 150; int yourScore = 1000; cout << "Original values\n"; cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling badSwap()\n"; badSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling goodSwap()\n"; goodSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n"; return 0; } void badSwap(int x, int y) { int temp = x; x = y; y = temp; } void goodSwap(int& x, int& y) { int temp = x; x = y; y = temp; } badSwap is “pass-by-VALUE” so upon return from the function nothing changes

Examination of – Swap // Swap // Demonstrates passing references // to alter argument variables #include using namespace std; void badSwap(int x, int y); void goodSwap(int& x, int& y); int main() { int myScore = 150; int yourScore = 1000; cout << "Original values\n"; cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling badSwap()\n"; badSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling goodSwap()\n"; goodSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n"; return 0; } void badSwap(int x, int y) { int temp = x; x = y; y = temp; } void goodSwap(int& x, int& y) { int temp = x; x = y; y = temp; } goodSwap is “pass-by-REFERENCE” so upon return from the function values ARE CHANGED

Additional Examples Available EX033_InventDisplay.cpp – passing vectors of strings by reference EX035_TicTacToe – game using various Reference concepts – as well as more interesting C++ stuff Left for students to investigate on their own Investigate on your own

Marker Slide Any questions on: – Review C++ Classes Enumerated Types Operator Overloading – References in C++ Next up – Pointers in C++ Pointers Program: in-class activity Pointers as parameters to functions (Swap) Returning Ptrs from function (on your own - InventoryPtr) Arrays as pointers Pointers to Classes Tic-Tac-Toe via pointers

Pointers A pointer is – a variable that contains a memory address Pointers allow us – to work directly – and efficiently with computer memory Like iterators from STL they are often used to access contents of other variables

Pointers – What are they? A pointer variable is – a variable whose value is the address of a memory location Examples – char* chPtr; // pointer to a single char – int* aryOfPtrs[12]; // array of pointers to integers

Pointers – What are they? A pointer variable is – a variable whose value is the address of a memory location Examples – char* chPtr; // pointer to a single char – int* aryOfPtrs[12]; // array of pointers to integers

Pointing Program – Example The example that follows this slide – demonstrates the mechanics of pointers – creates a variable for a score – creates a pointer to store the address of that variable – Program then shows when changing the variable directly, the pointer will reflect the change you can change the value of the variable using the pointer you can make the pointer point to a new/different variable – Also shows pointers work with built in types and user defined types

Graded In-Class Activity – Pointing Take 10 to 20 minutes Enter, Compile, and Run the following program. Submit your code to D2L before the end of class today // Pointing // Demonstrates using pointers #include using namespace std; int main() { int* pAPointer; //declare a pointer int* pScore = 0; //declare and initialize a pointer int score = 1000; pScore = &score; //assign pointer pScore address of variable score cout << "Assigning &score to pScore\n"; cout << "&score is: " << &score << "\n"; // address of score // variable cout << "pScore is: " << pScore << "\n"; //address stored // in pointer cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; // value pointed // to by pointer cout << "Adding 500 to score\n"; score += 500; cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Adding 500 to *pScore\n"; *pScore += 500; cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Assigning &newScore to pScore\n"; int newScore = 5000; pScore = &newScore; cout << "&newScore is: " << &newScore << "\n"; cout << "pScore is: " << pScore << "\n"; cout << "newScore is: " << newScore << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Assigning &str to pStr\n"; string str = "score"; string* pStr = &str; //pointer to string object cout << "str is: " << str << "\n"; cout << "*pStr is: " << *pStr << "\n"; cout << "(*pStr).size() is: " << (*pStr).size() << "\n"; cout size() is: " size() << "\n"; return 0; } In class “code walk through” follows this slide, to explain the code

Pointing – Looking at Code // Pointing // Demonstrates using pointers #include using namespace std; int main() { int* pAPointer; //declare a pointer int* pScore = 0; //declare and initialize a pointer int score = 1000; pScore = &score; //assign pointer pScore address of a variable score include iostream for std::cout, std::cin, std::endl, etc

Pointing – Looking at Code // Pointing // Demonstrates using pointers #include using namespace std; int main() { int* pAPointer; //declare a pointer int* pScore = 0; //declare and initialize a pointer int score = 1000; pScore = &score; //assign pointer pScore address of a variable score include string for std::string

Pointing – Looking at Code // Pointing // Demonstrates using pointers #include using namespace std; int main() { int* pAPointer; //declare a pointer int* pScore = 0; //declare and initialize a pointer int score = 1000; pScore = &score; //assign pointer pScore address of a variable score Use the standard name space to avoid typing “std::” everywhere

Pointing – Looking at Code // Pointing // Demonstrates using pointers #include using namespace std; int main() { int* pAPointer; //declare a pointer int* pScore = 0; //declare and initialize a pointer int score = 1000; pScore = &score; //assign pointer pScore address of a variable score main function – entry point to begin running stuff for all C++ programs

Pointing – Looking at Code // Pointing // Demonstrates using pointers #include using namespace std; int main() { int* pAPointer; //declare a pointer int* pScore = 0; //declare and initialize a pointer int score = 1000; pScore = &score; //assign pointer pScore address of a variable score int* pAPointer; A pointer to an integer – NOT initialized which is BAD --- ALWAYS initialize your variables! The prefix “p” on the variable name is a naming convention to indicate it is a pointer Aside: You will see “mp_” in classes to indicate member pointers

Pointing – Looking at Code // Pointing // Demonstrates using pointers #include using namespace std; int main() { int* pAPointer; //declare a pointer int* pScore = 0; //declare and initialize a pointer int score = 1000; pScore = &score; //assign pointer pScore address of a variable score int* pAPointer; Where you put the asterisk is up to you int * pApointer and int *pApointer are both equally valid. Just be consistent.

Pointing – Looking at Code // Pointing // Demonstrates using pointers #include using namespace std; int main() { int* pAPointer; //declare a pointer int* pScore = 0; //declare and initialize a pointer int score = 1000; pScore = &score; //assign pointer pScore address of a variable score int* pScore = 0; pointer to an integer initialized to point at address zero or rather point at nothing by convention and good practice this SHOULD BE WRITTEN as int* pScore = NULL; NULL is a defined constant in iostream where NULL == 0

Pointing – Looking at Code // Pointing // Demonstrates using pointers #include using namespace std; int main() { int* pAPointer; //declare a pointer int* pScore = 0; //declare and initialize a pointer int score = 1000; pScore = &score; //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 – Looking at Code // Pointing // Demonstrates using pointers #include using namespace std; int main() { int* pAPointer; //declare a pointer int* pScore = 0; //declare and initialize a pointer int score = 1000; pScore = &score; //assign pointer pScore address of a variable score The & operator returns the address of the variable immediately after it So: &score returns the memory address of the variable score

Pointing – Looking at Code // Pointing // Demonstrates using pointers #include using namespace std; int main() { int* pAPointer; //declare a pointer int* pScore = 0; //declare and initialize a pointer int score = 1000; pScore = &score; //assign pointer pScore address of a variable score pScore = &score; Assigns the memory address of the variable named score in the variable named pScore This is said to make “pScore point to score” This allows fun things to be done using pScore

Pointing – Using Pointers int main() { int* pAPointer; //declare a pointer int* pScore = 0; //declare and initialize a pointer int score = 1000; pScore = &score; //assign pointer pScore address of a variable score cout << "Assigning &score to pScore\n"; cout << "&score is: " << &score << "\n"; //address of score variable cout << "pScore is: " << pScore << "\n"; //address stored in pointer cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; //value pointed to by pointer To “prove” pScore contains the address of score, both are printed to the screen And it will say something like &score is: 0x234a5c pScore is: 0x234a5c As long as the numbers are the same all is good

Pointing – Using Pointers int main() { int* pAPointer; //declare a pointer int* pScore = 0; //declare and initialize a pointer int score = 1000; pScore = &score; //assign pointer pScore address of a variable score cout << "Assigning &score to pScore\n"; cout << "&score is: " << &score << "\n"; //address of score variable cout << "pScore is: " << pScore << "\n"; //address stored in pointer cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; //value pointed to by pointer In English if you “point to something” you are “referencing” it So if we want to see what a pointer is pointing to we must DEREFERENCE it To do this we put an asterisk in front of the pointer variable and that dereferences the pointer (it undoes what the declaration did) Basically the same thing that we did for iterators (sticky notes)

Pointing – Using Pointers int main() { int* pAPointer; //declare a pointer int* pScore = 0; //declare and initialize a pointer int score = 1000; pScore = &score; //assign pointer pScore address of a variable score cout << "Assigning &score to pScore\n"; cout << "&score is: " << &score << "\n"; //address of score variable cout << "pScore is: " << pScore << "\n"; //address stored in pointer cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; //value pointed to by pointer To complete the “proof” that score and pScore are related in a useful way, we print out the value of score

Pointing – Using Pointers int main() { int* pAPointer; //declare a pointer int* pScore = 0; //declare and initialize a pointer int score = 1000; pScore = &score; //assign pointer pScore address of a variable score cout << "Assigning &score to pScore\n"; cout << "&score is: " << &score << "\n"; //address of score variable cout << "pScore is: " << pScore << "\n"; //address stored in pointer cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; //value pointed to by pointer To complete the “proof” that score and pScore are related in a useful way, we print out the value of score and *pScore (via dereferencing) cout << *pScore; will display the value of what pScore is pointing at

Pointing – Using Pointers int main() { int* pAPointer; //declare a pointer int* pScore = 0; //declare and initialize a pointer int score = 1000; pScore = &score; //assign pointer pScore address of a variable score cout << "Assigning &score to pScore\n"; cout << "&score is: " << &score << "\n"; //address of score variable cout << "pScore is: " << pScore << "\n"; //address stored in pointer cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; //value pointed to by pointer To complete the “proof” 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 be: score is 1000 *pScore is 1000

Pointing – Using Pointers int score = 1000; pScore = &score; //assign pointer pScore address of a variable score cout << "Assigning &score to pScore\n"; cout << "&score is: " << &score << "\n"; //address of score variable cout << "pScore is: " << pScore << "\n"; //address stored in pointer cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; //value pointed to by pointer cout << "Adding 500 to score\n"; score += 500; cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; Here we add 500 to score and show both the value of score and *pScore change correctly

Pointing – Using Pointers cout << "Assigning &score to pScore\n"; cout << "&score is: " << &score << "\n"; //address of score variable cout << "pScore is: " << pScore << "\n"; //address stored in pointer cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; //value pointed to by pointer cout << "Adding 500 to score\n"; score += 500; cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Adding 500 to *pScore\n"; *pScore += 500; cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; In similar fashion we add 500 to *pScore Note it is dereferenced in the line: *pScore += 500; and show both the value of score and *pScore change correctly

Pointing – Using Pointers score += 500; cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Adding 500 to *pScore\n"; *pScore += 500; cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Assigning &newScore to pScore\n"; int newScore = 5000; pScore = &newScore; cout << "&newScore is: " << &newScore << "\n"; cout << "pScore is: " << pScore << "\n"; cout << "newScore is: " << newScore << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; You can change what a pointer is referencing Here we create a new variable named: newScore Which is at a different memory address than score

Pointing – Using Pointers score += 500; cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Adding 500 to *pScore\n"; *pScore += 500; cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Assigning &newScore to pScore\n"; int newScore = 5000; pScore = &newScore; cout << "&newScore is: " << &newScore << "\n"; cout << "pScore is: " << pScore << "\n"; cout << "newScore is: " << newScore << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; You can change what a pointer is referencing Here 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 = &newScore)

Pointing – Using Pointers score += 500; cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Adding 500 to *pScore\n"; *pScore += 500; cout << "score is: " << score << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Assigning &newScore to pScore\n"; int newScore = 5000; pScore = &newScore; cout << "&newScore is: " << &newScore << "\n"; cout << "pScore is: " << pScore << "\n"; cout << "newScore is: " << newScore << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; You can change what a pointer is referencing Here 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 = &newScore) And we print out addresses and values Showing pScore now references newScore (and not score)

Pointing – Using Pointers cout << "Assigning &newScore to pScore\n"; int newScore = 5000; pScore = &newScore; cout << "&newScore is: " << &newScore << "\n"; cout << "pScore is: " << pScore << "\n"; cout << "newScore is: " << newScore << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Assigning &str to pStr\n"; string str = "score"; string* pStr = &str; //pointer to string object cout << "str is: " << str << "\n"; cout << "*pStr is: " << *pStr << "\n"; cout << "(*pStr).size() is: " << (*pStr).size() << "\n"; cout size() is: " size() << "\n"; return 0; } Pointers can be used with class objects too. Here we create a variable of class std::string initialized to the word “score”

Pointing – Using Pointers cout << "Assigning &newScore to pScore\n"; int newScore = 5000; pScore = &newScore; cout << "&newScore is: " << &newScore << "\n"; cout << "pScore is: " << pScore << "\n"; cout << "newScore is: " << newScore << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Assigning &str to pStr\n"; string str = "score"; string* pStr = &str; //pointer to string object cout << "str is: " << str << "\n"; cout << "*pStr is: " << *pStr << "\n"; cout << "(*pStr).size() is: " << (*pStr).size() << "\n"; cout size() is: " size() << "\n"; return 0; } Pointers can be used with class objects too. We also must create a new pointer variable named pStr This is a variable that can point to type string

Pointing – Using Pointers cout << "Assigning &newScore to pScore\n"; int newScore = 5000; pScore = &newScore; cout << "&newScore is: " << &newScore << "\n"; cout << "pScore is: " << pScore << "\n"; cout << "newScore is: " << newScore << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Assigning &str to pStr\n"; string str = "score"; string* pStr = &str; //pointer to string object cout << "str is: " << str << "\n"; cout << "*pStr is: " << *pStr << "\n"; cout << "(*pStr).size() is: " << (*pStr).size() << "\n"; cout size() is: " size() << "\n"; return 0; } Pointers can be used with class objects too. We also must create a new pointer variable named pStr This is a variable that can point to type string and we initialize it to point to the variable named str (recall &str returns the memory address of str)

Pointing – Using Pointers cout << "Assigning &newScore to pScore\n"; int newScore = 5000; pScore = &newScore; cout << "&newScore is: " << &newScore << "\n"; cout << "pScore is: " << pScore << "\n"; cout << "newScore is: " << newScore << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Assigning &str to pStr\n"; string str = "score"; string* pStr = &str; //pointer to string object cout << "str is: " << str << "\n"; cout << "*pStr is: " << *pStr << "\n"; cout << "(*pStr).size() is: " << (*pStr).size() << "\n"; cout size() is: " size() << "\n"; return 0; } We print out the value of variable str

Pointing – Using Pointers cout << "Assigning &newScore to pScore\n"; int newScore = 5000; pScore = &newScore; cout << "&newScore is: " << &newScore << "\n"; cout << "pScore is: " << pScore << "\n"; cout << "newScore is: " << newScore << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Assigning &str to pStr\n"; string str = "score"; string* pStr = &str; //pointer to string object cout << "str is: " << str << "\n"; cout << "*pStr is: " << *pStr << "\n"; cout << "(*pStr).size() is: " << (*pStr).size() << "\n"; cout size() is: " size() << "\n"; return 0; } We print out the value of variable str and the value of the thing referenced by pStr

Pointing – Using Pointers cout << "Assigning &newScore to pScore\n"; int newScore = 5000; pScore = &newScore; cout << "&newScore is: " << &newScore << "\n"; cout << "pScore is: " << pScore << "\n"; cout << "newScore is: " << newScore << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Assigning &str to pStr\n"; string str = "score"; string* pStr = &str; //pointer to string object cout << "str is: " << str << "\n"; cout << "*pStr is: " << *pStr << "\n"; cout << "(*pStr).size() is: " << (*pStr).size() << "\n"; cout size() is: " size() << "\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 – Using Pointers cout << "Assigning &newScore to pScore\n"; int newScore = 5000; pScore = &newScore; cout << "&newScore is: " << &newScore << "\n"; cout << "pScore is: " << pScore << "\n"; cout << "newScore is: " << newScore << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Assigning &str to pStr\n"; string str = "score"; string* pStr = &str; //pointer to string object cout << "str is: " << str << "\n"; cout << "*pStr is: " << *pStr << "\n"; cout << "(*pStr).size() is: " << (*pStr).size() << "\n"; cout size() is: " size() << "\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 – Using Pointers cout << "Assigning &newScore to pScore\n"; int newScore = 5000; pScore = &newScore; cout << "&newScore is: " << &newScore << "\n"; cout << "pScore is: " << pScore << "\n"; cout << "newScore is: " << newScore << "\n"; cout << "*pScore is: " << *pScore << "\n\n"; cout << "Assigning &str to pStr\n"; string str = "score"; string* pStr = &str; //pointer to string object cout << "str is: " << str << "\n"; cout << "*pStr is: " << *pStr << "\n"; cout << "(*pStr).size() is: " << (*pStr).size() << "\n"; cout size() is: " size() << "\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 the arrow, ->, (minus sign followed by greater than) operator to call member functions of the referenced object: pStr->size()

Quick Review Follows Pointers reference or point to other variables This is accomplished because pointers hold the address of the other variables

Getting the Address via the Address Operator & Note the value pointed to by ptr is denoted *ptr So the above cout will output what to the screen? // make ptr “point” at x int x; x = 17; int* ptr; ptr = &x; cout << *ptr; x ptr ???

Getting the value of the referenced object via Dereferencing using * Change the value at the address ptr points to, to be 42 int x; x = 17; int* ptr; ptr = &x; cout << *ptr; *ptr = 42; x ptr 42 Same code as previous slide

Dereferencing using * Change the value at the address ptr points to, to be 42 int x; x = 12; int* ptr; ptr = &x; *ptr = 5; x ptr 42

Marker Slide Any questions on: – Review C++ Classes Enumerated Types Operator Overloading – References in C++ – Pointers in C++ Pointers Program: in-class activity Next up – Pointers in C++ Pointers as parameters to functions (Swap) Returning Ptrs from function (on your own - InventoryPtr) Arrays as pointers Pointers to Classes Tic-Tac-Toe via pointers

Pointers As Parameters to Function Pointers can be used as parameters to functions. This provides an alternative way to achieve sort of a pass by reference effect – Officially called: pass by pointer

Class Group Activity – SwapPointer Find someone to talk to – Take 5 to 10 minutes to download the SwapPointer Code from D2L File named similar to: EX038_SwapPtrVersion – Compare that program to the Swap program that used only references – Illustrates: Pointers VERSUS References

Swap – Pointer Version // Swap Pointer // Demos passing constant ptrs to alter argument variables #include 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 << "Original values\n"; cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling badSwap()\n"; badSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling goodSwap()\n"; goodSwap(&myScore, &yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n"; return 0; } 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; } badSwap is “pass-by-VALUE”

// Swap Pointer // Demos passing constant ptrs to alter argument variables #include 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 << "Original values\n"; cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling badSwap()\n"; badSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling goodSwap()\n"; goodSwap(&myScore, &yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n"; return 0; } 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 – Pointer Version goodSwap is “pass-by-pointer” The use of const is a little deceptive here The parameters are constant pointers because, although we plan to change the values they point to, We do NOT plan to change the pointers themselves. Remember, this is how references work too.

// Swap Pointer // Demos passing constant ptrs to alter argument variables #include 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 << "Original values\n"; cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling badSwap()\n"; badSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling goodSwap()\n"; goodSwap(&myScore, &yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n"; return 0; } 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 – Pointer Version Setup things as in previous Swap version Call badSwap also as before

// Swap Pointer // Demos passing constant ptrs to alter argument variables #include 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 << "Original values\n"; cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling badSwap()\n"; badSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling goodSwap()\n"; goodSwap(&myScore, &yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n"; return 0; } 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 – Pointer Version goodSwap expects pointers as parameters Pointers hold memory addresses So send the address of myScore and yourScore using the operator& to goodSwap( &myScore &yourScore );

// Swap Pointer // Demos passing constant ptrs to alter argument variables #include 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 << "Original values\n"; cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling badSwap()\n"; badSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling goodSwap()\n"; goodSwap(&myScore, &yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n"; return 0; } 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 – Pointer Version dereference the pointer pX using the asterisk to assigned its value to temp int temp = *pX;

// Swap Pointer // Demos passing constant ptrs to alter argument variables #include 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 << "Original values\n"; cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling badSwap()\n"; badSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling goodSwap()\n"; goodSwap(&myScore, &yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n"; return 0; } 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 – Pointer Version dereference the pointer pX and pY using the asterisk to set the value of what pX references to be the value of what pY references *pX = *pY

// Swap Pointer // Demos passing constant ptrs to alter argument variables #include 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 << "Original values\n"; cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling badSwap()\n"; badSwap(myScore, yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n\n"; cout << "Calling goodSwap()\n"; goodSwap(&myScore, &yourScore); cout << "myScore: " << myScore << "\n"; cout << "yourScore: " << yourScore << "\n"; return 0; } 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 – Pointer Version dereference the pointer pY using the asterisk to set the value of what pY references to be the value of temp *pY = temp;

Marker Slide Any questions on: – Review C++ Classes Enumerated Types Operator Overloading – References in C++ – Pointers in C++ Pointers Program: in-class activity Pointers as parameters to functions (Swap) Next up – Pointers in C++ Returning Ptrs from function (on your own - InventoryPtr) Arrays as pointers Pointers to Classes Tic-Tac-Toe via pointers

On Your Own – Inventory Program On D2L there is an Inventory program you can look at to see how returning a pointer can be done – EX040_InventoryPtr.cpp Investigate on your own

Marker Slide Any questions on: – Review C++ Classes Enumerated Types Operator Overloading – References in C++ – Pointers in C++ Pointers Program: in-class activity Pointers as parameters to functions (Swap) Returning Ptrs from function (on your own - InventoryPtr) Next up – Pointers in C++ Arrays as pointers Pointers to Classes Tic-Tac-Toe via pointers

Array Names Array names are constant pointers – and can be used as such Given a function such as: 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 – Example Available On D2L there should be an example demonstrating the relationship between pointers and arrays – Named something like EX045_ArrayPasser //Array Passer //Demonstrates relationship between pointers and arrays #include using namespace std; void increase(int* const arrayName, const int arraySize); void display(const int* const arrayName, const int arraySize); int main() { cout << "Creating a scoreArray of high scores.\n\n"; const int NUM_SCORES = 3; int highScores[NUM_SCORES] = {5000, 3500, 2700}; cout << "Displaying scores using scoreArray name as a constant pointer.\n"; cout << *highScores << endl; cout << *(highScores + 1) << endl; cout << *(highScores + 2) << "\n\n"; cout << "Increasing scores by passing scoreArray as a constant pointer.\n\n"; increase(highScores, NUM_SCORES); cout << "Displaying scores by passing scoreArray as a constant pointer to a constant.\n"; display(highScores, NUM_SCORES); return 0; } void increase(int* const arrayName, const int arraySize) { for (int i = 0; i < arraySize; ++i) { arrayName[i] += 500; } void display(const int* const arrayName, const int arraySize) { for (int i = 0; i < arraySize; ++i) { cout << arrayName[i] << endl; } Investigate on your own

Marker Slide Any questions on: – Review C++ Classes Enumerated Types Operator Overloading – References in C++ – Pointers in C++ Pointers Program: in-class activity Pointers as parameters to functions (Swap) Returning Ptrs from function (on your own - InventoryPtr) Arrays as pointers Next up – Pointers in C++ Pointers to Classes Tic-Tac-Toe via pointers (Last Class Activity)

Pointers to Classes // pointer to classes example #include using namespace std; class CRectangle { int width, height; public: void set_values (int, int); int area (void) {return (width * height);} }; void CRectangle::set_values (int a, int b) { width = a; height = b; } int main () { CRectangle a, *b, *c; CRectangle * d = new CRectangle[2]; b= new CRectangle; c= &a; a.set_values (1,2); b->set_values (3,4); d->set_values (5,6); d[1].set_values (7,8); a area: 2 *b area: 12 *c area: 2 d[0] area: 30 d[1] area: 56 Main points: [go to next slide] cout << "a area: " << a.area() << endl; cout area() << endl; cout << "d[0] area: " << d[0].area() << endl; cout << "d[1] area: " << d[1].area() << endl; delete[] d; delete b; return 0; }

Pointers to Classes // pointer to classes example #include using namespace std; class CRectangle { int width, height; public: void set_values (int, int); int area (void) {return (width * height);} }; void CRectangle::set_values (int a, int b) { width = a; height = b; } int main () { CRectangle a, *b, *c; CRectangle * d = new CRectangle[2]; b= new CRectangle; c= &a; a.set_values (1,2); b->set_values (3,4); d->set_values (5,6); d[1].set_values (7,8); a area: 2 *b area: 12 *c area: 2 d[0] area: 30 d[1] area: 56 Main points: You can have pointers to classes too cout << "a area: " << a.area() << endl; cout area() << endl; cout << "d[0] area: " << d[0].area() << endl; cout << "d[1] area: " << d[1].area() << endl; delete[] d; delete b; return 0; }

Pointers to Classes // pointer to classes example #include using namespace std; class CRectangle { int width, height; public: void set_values (int, int); int area (void) {return (width * height);} }; void CRectangle::set_values (int a, int b) { width = a; height = b; } int main () { CRectangle a, *b, *c; CRectangle * d = new CRectangle[2]; b= new CRectangle; c= &a; a.set_values (1,2); b->set_values (3,4); d->set_values (5,6); d[1].set_values (7,8); a area: 2 *b area: 12 *c area: 2 d[0] area: 30 d[1] area: 56 Main points: You can have pointers to classes too The use of new, delete, and delete [] will be discussed in the future (next class) cout << "a area: " << a.area() << endl; cout area() << endl; cout << "d[0] area: " << d[0].area() << endl; cout << "d[1] area: " << d[1].area() << endl; delete[] d; delete b; return 0; }

Marker Slide Any questions on: – Review C++ Classes Enumerated Types Operator Overloading – References in C++ – Pointers in C++ Pointers Program: in-class activity Pointers as parameters to functions (Swap) Returning Ptrs from function (on your own - InventoryPtr) Arrays as pointers Pointers to Classes Next up – Pointers in C++ Tic-Tac-Toe via pointers (Last Class Activity)

In-Class Activity: TicTacToe Ptr Version Posted on D2L is a pointer version of TicTacToe – Filename something like EX047_TicTacToePtrVersion Download, compile, and run this version Group together and discuss the differences between the pointer version and the reference version And that should be the last activity for today… – [next slide]

Marker Slide Any questions on: – Review C++ Classes Enumerated Types Operator Overloading – References in C++ – Pointers in C++ Pointers Program: in-class activity Pointers as parameters to functions (Swap) Returning Ptrs from function (on your own - InventoryPtr) Arrays as pointers Tic-Tac-Toe via pointers Any General Questions? Next Up – Free Play

Free Play – Things to Work On Graded In-Class Activity: Pointers Homework 3 Homework 4 Various In-Class Activities to revisit Several On-Your Own Activities

The End Or is it?