Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "References and Pointers Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University."— Presentation transcript:

1

2 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

3 Sound Check

4 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

5 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

6 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

7 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

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

9 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

10 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 };

11 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()

12 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()

13 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()

14 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 };

15 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 };

16 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

17 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

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

19 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;

20 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

21 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

22 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

23 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

24 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; }

25 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

26 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

27 You Know What’s Funny If you don’t get this joke now… you will… oh you will >=) http://portermason.com/johnny/1997/12/03/you-know-what-s-funny/

28 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

29 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; }

30 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

31 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

32 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; }

33 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

34 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

35 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

36 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

37 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

38 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

39 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

40 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

41 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

42 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

43 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

44 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

45 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

46 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

47 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

48 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

49 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.

50 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

51 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

52 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

53 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

54 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

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

56 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

57 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

58 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

59 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

60 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

61 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

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

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

64 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”

65 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

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

67 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

68 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

69 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

70 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()

71 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()

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

73 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; 8000 17 x 5600 8000 ptr ???

74 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; 8000 17 x 5600 8000 ptr 42 Same code as previous slide

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

76 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

77 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

78 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

79 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”

80 // 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.

81 // 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

82 // 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 );

83 // 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;

84 // 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

85 // 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;

86 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

87 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

88 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

89 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 *

90 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

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

92 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; }

93 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; }

94 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; }

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

96 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]

97 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

98 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

99 The End Or is it?


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

Similar presentations


Ads by Google