14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Beginning C++ Through Game Programming, Second Edition
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
C++ for Engineers and Scientists Third Edition
Data Structures Using C++ 2E
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Object Oriented Data Structures
Pointer Data Type and Pointer Variables
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Data Structures Using C++ 2E
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
Generic Programming Using the C++ Standard Template Library.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Pointers OVERVIEW.
18-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Object-Oriented Programming in C++
2-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
10-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Starting Out with C++, 3 rd Edition Introduction to the STL vector The Standard Template Library (or STL) is a collection of data types and algorithms.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
CS212: Object Oriented Analysis and Design
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Pointers and Linked Lists
Computing Fundamentals with C++
Pointers and Linked Lists
Chapter 4 Linked Lists.
Dynamic Memory CSCE 121 J. Michael Moore.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 12 Pointers and Memory Management
Chapter 4 Implementing Free Functions
Computing Fundamentals with C++
COP 3330 Object-oriented Programming in C++
Dynamic Memory CSCE 121.
Presentation transcript:

14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN Presentation Copyright 1999, Franklin, Beedle & Associates Students who purchase and instructors who adopt Computing Fundamentals with C++, Object-Oriented Programming and Design by Rick Mercer are welcome to use this presentation as long as this copyright notice remains intact.

14-2 Chapter 14 A Little Indirection Pointers, Containers, and Iterators  Chapter Objectives — understand that pointer objects store addresses of other objects — use several methods for initializing pointers they can store null or an address they can store null or an address — use the standard list class from the standard template library (STL) from the standard template library (STL) — use the STL iterator class to iterate over a list object

Memory Considerations  In addition to name, state, and operations, every object has a fourth attribute––its address  With the following initialization, we see that the name (charlie), state (100), and operations (the set of int operations) are known: int charlie = 100; // But where is it stored? int charlie = 100; // But where is it stored?

14-4 Addresses  An object's address is the actual memory location where the first byte of the object is stored  The actual memory location is something we have not needed to know about until now.

14-5 Static and Dynamic Memory Allocation  Some objects take a fixed amount of memory at compiletime: char int double char int double  Other objects require varying amounts of memory, which is allocated and deallocated dynamically, that is, at runtime: string (string objects change size during =, >>, and +)  We usually use pointer objects to allow for these dynamic objects

Pointer Objects  Pointer objects store addresses of other objects and are declared with * as follows: class-name * identifier ; class-name * identifier ; int anInt = 123; // The int object is initialized int anInt = 123; // The int object is initialized int* intPtr; // intPtr stores an address int* intPtr; // intPtr stores an address  anInt stores an integer, however, intPtr stores a pointer to an integer  So pointer objects may store the address of other objects

14-7 The State of a Pointer Object  At this point, the value of intPtr may have or become one of these values: — Undefined (as intPtr exists above) — The special null value 0, after intPtr = 0; — The address of an int object, after intPtr=&anInt;

14-8  Currently, we may depict the undefined value of intPtr as follows:  The & symbol is called the address-of operator when it precedes an object.  This assignment returns the address of anInt and stores that address into intPtr : intPtr = &anInt; intPtr = &anInt; intPtr ??? The Value of intPtr

14-9 Defining Pointer Objects  The affect of this assignment intPtr = &anInt; intPtr = &anInt; is represented graphically like this:  Now intPtr is defined, but anInt is still undefined intPtranInt 123

14-10  We can define anInt with the usual assignment (anInt = 0;).  Or we can initialize anInt indirectly with the dereference operator * anInt = 97; intPtr* = 97; // The same as anInt = 97  Now both objects are defined: intPtranInt (or intPtr*) 97 Dereferencing

14-11 The dereference Operator  The following code displays 97 and 96 cout << (intPtr*) << (intPtr*-1) << endl; cout << (intPtr*) << (intPtr*-1) << endl;  and this code changes 97 to 98 intPtr* = intPtr* + 1; intPtr* = intPtr* + 1;  Demonstrate pointer manipulation to indirectly swap two variables Demo pointers.cpp Demo pointers.cpp intPtranInt 97

14-12 Address-of and Dereference  Write the output generated by this program #include #include using namespace std; using namespace std; int main() int main() { int *p1, *p2; int *p1, *p2; int n1, n2; int n1, n2; p1 = &n1; p1 = &n1; *p1 = 5; *p1 = 5; n2 = 10; n2 = 10; p2 = &n2; p2 = &n2; cout << n1 << " " << *p1 << endl; cout << n1 << " " << *p1 << endl; cout << n2 << " " << *p2 << endl; cout << n2 << " " << *p2 << endl; return 0; return 0; }

The Standard list Class (from the STL)  A list object — stores a collection of objects no need to determine maximum capacity at first no need to determine maximum capacity at first — allows access to individual elements either find one or iterate over all sequentially either find one or iterate over all sequentially  Here are some member functions empty Returns true if there are zero elements in the list push_back Adds one elements at end of the list remove Removes an object from the list if it is found size Returns the current number of objects in the container

14-14 Sample Program // Demonstrate list, one of the standard container classes #include #include #include // for the standard (STL) list class using namespace std; int main() { list intList; // intList stores collection of ints list intList; // intList stores collection of ints cout << "intList.size() == " << intList.size() << endl; cout << "intList.size() == " << intList.size() << endl; // "Push" 5 new int objects onto the "back" of the list. // "Push" 5 new int objects onto the "back" of the list. intList.push_back(111); intList.push_back(111); intList.push_back(222); intList.push_back(222); intList.push_back(333); intList.push_back(333); intList.push_back(444); intList.push_back(444); intList.push_back(555); intList.push_back(555); Output: intList.size() == 0

14-15 More sample messages // "Push" 5 new int objects onto the "back" of the list // "Push" 5 new int objects onto the "back" of the list intList.push_back(111); intList.push_back(111); intList.push_back(222); intList.push_back(222); intList.push_back(333); intList.push_back(333); intList.push_back(444); intList.push_back(444); intList.push_back(555); intList.push_back(555); // Remove an item in the list and // Remove an item in the list and // attempt to remove am object that is not // attempt to remove am object that is not intList.remove(333); // remove 333 intList.remove(333); // remove 333 intList.remove(999); // not found intList.remove(999); // not found // assert: There are four elements in the container intList. // assert: There are four elements in the container intList. // assert: The first is 111 and the last is 555. // assert: The first is 111 and the last is 555. // Show the number of objects in the container: // Show the number of objects in the container: cout << "intList.size() == " << intList.size() << endl; cout << "intList.size() == " << intList.size() << endl; return 0; return 0;} Output: intList.size() == 4

Traversing a Standard vector Object  Individual vector elements can be referenced — using subscript notation [ ] — using an iterator object lists have iterator objects traverse the collection lists have iterator objects traverse the collection  Another way to visit all vector elements — assume 5 vector elements are initialized like this: vector x(5); vector x(5); x[0] = 11; x[0] = 11; x[1] = 22; x[1] = 22; x[2] = 33; x[2] = 33; x[3] = 44; x[3] = 44; x[4] = 55; x[4] = 55;

14-17 Using an iterator object same idea as iterator pattern of Chapter 11 // Construct an iterator object named i. vector ::iterator i; // i may indirectly reference any vector element cout << "*i x[j]" << endl; int j = 0; // Now you don't need a variable 'n' to store the number // of meaningful elements. Use end() member function instead for(i = x.begin(); i != x.end(); i++) { cout << (*i) << " " << x[j] << endl; cout << (*i) << " " << x[j] << endl; // i stores an address, *i refers to the element // i stores an address, *i refers to the element // *i in this context is equivalent to x[j] // *i in this context is equivalent to x[j] j++; j++;} *i x[j] Output

The difference between i and *i  General form for constructing an iterator object container-class :: iterator object-name ; container-class :: iterator object-name ; — The container-class is any of the Standard Template Library (STL) Classes: list stack queue vector — The class-of-elements is any class that has a default constructor and defines relational operators such as < most standard classes can be contained most standard classes can be contained you have to overload operators for your own new classes you have to overload operators for your own new classes –see chapter 18: Operator Overloading

14-19 Some Characteristics of the C++ list class  A list object  manages a collection of objects  is sequenced—there is a first, second,...,last  can add objects at the beginning or at the end of the list with push_front and push_back  always knows how big it is  is generic because it may be declared to store collections of any class  may grow as big as computer memory allows  has many available algorithms available such as find, remove, sort

14-20 Search and sort are built in  The standard STL containers have algorithms defined for them. For example — sort: A member function to sort any collection  This code builds a list of five string object list presidents; list presidents; presidents.push_back("George Washington"); presidents.push_back("George Washington"); presidents.push_back("John Adams"); presidents.push_back("John Adams"); presidents.push_back("Thomas Jefferson"); presidents.push_back("Thomas Jefferson"); presidents.push_back("James Madison"); presidents.push_back("James Madison"); presidents.push_back("James Monroe"); presidents.push_back("James Monroe");

14-21 Sort and Search the Easy way  Then the list is sorted with the sort message presidents.sort(); presidents.sort(); // assert: The list is in ascending order // assert: The list is in ascending order  The list is searched from beginning to end for a specific object like this assuming == is defined list ::iterator presPtr; list ::iterator presPtr; presPtr = find(presidents.begin(), presidents.end(), presPtr = find(presidents.begin(), presidents.end(), "Abraham Lincoln"); "Abraham Lincoln"); // prePtr is always == end if it wasn't found // prePtr is always == end if it wasn't found if(presPtr != presidents.end()) if(presPtr != presidents.end()) cout << "Found " << (*presPtr) << endl; cout << "Found " << (*presPtr) << endl; else else cout << "Did not find Abe" << endl; cout << "Did not find Abe" << endl;

14-22 #include #include  sort is a member function  find is a free function not defined vis #include #include  To use the find algorithm #include #include  Question, write code that counts the number of times an element is found in a list