Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.

Slides:



Advertisements
Similar presentations
Array-Based Lists List Length of a list
Advertisements

Data Structures Using C++ 2E
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
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.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 13: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Chapter 15: Operator Overloading
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Data Structures Using C++ 2E
Review of C++ Programming Part II Sheng-Fang Huang.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CHAPTER 15 POINTERS, CLASSES, AND VIRTUAL FUNCTIONS.
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Pointers You are not responsible for virtual functions (starting on.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
More C++ Features True object initialisation
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter 3 Pointers and Array-Based Lists Dr. Youssef Harrath
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Pointer Data Type and Pointer Variables III CS1201: Programming Language 2 By: Nouf Aljaffan Edited by : Nouf Almunyif.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Pointers and Arrays Dynamic Variables and Arrays.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Chapter 13: Overloading and Templates
Chapter 7: User-Defined Functions II
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
About the Presentations
Pointer Data Type and Pointer Variables II
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Array Lists Chapter 6 Section 6.1 to 6.3
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists

Data Structures Using C++ 2E2 Developer > Programmer >= Coder Fruit Juice Machine (Pg. 38) Complex Numbers (Pg. 103) Polynomial Operations (Pg. 187) ?

Data Structures Using C++ 2E3 The Pointer Data Type and Pointer Variables Pointer data types –Values are computer memory addresses –Domain consists of addresses (memory locations) Pointer variable –Contains an address (memory address)

Data Structures Using C++ 2E4 The Pointer Data Type and Pointer Variables (cont’d.) Declaring pointer variables –Specify data type of value stored in the memory location that pointer variable points to –General syntax dataType *identifier; –Asterisk symbol ( * ) Between data type and variable name Can appear anywhere between the two Preference: attach * to variable name –Examples: int *p; and char *ch;

The Pointer Data Type and Pointer Variables (cont’d.) int* p; int *p, q; int *p, *q; Data Structures Using C++ 2E5

6 The Pointer Data Type and Pointer Variables (cont’d.) Address of operator ( & ) –Unary operator –Returns address of its operand Dereferencing operator ( * ) –Unary operator Different from binary multiplication operator –Also known as indirection operator –Refers to object where the pointer points (operand of the * )

The Pointer Data Type and Pointer Variables (cont’d.) int num; int *p; p = # Value of &p = ? Value of p = ? Value of *p = ? Example 3_2! Data Structures Using C++ 2E7

8 The Pointer Data Type and Pointer Variables (cont’d.) Pointers and classes –Dot operator (. ) Higher precedence than dereferencing operator ( * ) –Member access operator arrow ( -> ) Simplifies access of class or struct components via a pointer Consists of two consecutive symbols: hyphen and ‘‘greater than’’ symbol –Syntax pointerVariableName -> classMemberName

The Pointer Data Type and Pointer Variables (cont’d.) string *str; str = new string; *str = “Hello!”; str.length(); *str.length(); (*str).length(); str->length(); Data Structures Using C++ 2E9

10 The Pointer Data Type and Pointer Variables (cont’d.) Initializing pointer variables –No automatic variable initialization in C++ –Pointer variables must be initialized If not initialized, they do not point to anything –Initialized using Constant value 0 (null pointer) Named constant NULL –Number 0 Only number directly assignable to a pointer variable

Data Structures Using C++ 2E11 The Pointer Data Type and Pointer Variables (cont’d.) Dynamic variables –Variables created during program execution Real power of pointers –Two operators new : creates dynamic variables delete : destroys dynamic variables Reserved words

Data Structures Using C++ 2E12 The Pointer Data Type and Pointer Variables (cont’d.) Operator new –Allocates single variable –Allocates array of variables –Syntax new dataType; new dataType[intExp]; –Allocates memory (variable) of designated type Returns pointer to the memory (allocated memory address) Allocated memory: uninitialized

Data Structures Using C++ 2E13 The Pointer Data Type and Pointer Variables (cont’d.) Operator delete –Destroys dynamic variables –Syntax delete pointerVariable; delete [ ] pointerVariable; –Memory leak Memory space that cannot be reallocated –Dangling pointers Pointer variables containing addresses of deallocated memory spaces Avoid by setting deleted pointers to NULL after delete

The Pointer Data Type and Pointer Variables (cont’d.) Memory Leak int *p; p = new int; *p = 54; p = new int; *p = 73; How to fix? Dangling Pointer int *p; p = new int; *p = 54; delete p; cout << *p; How to fix? Example 3_3! Data Structures Using C++ 2E14

Data Structures Using C++ 2E15 The Pointer Data Type and Pointer Variables (cont’d.) Operations on pointer variables –Operations allowed Assignment, relational operations; some limited arithmetic operations Can assign value of one pointer variable to another pointer variable of the same type Can compare two pointer variables for equality Can add and subtract integer values from pointer variable –Danger Accidentally accessing other variables’ memory locations and changing content without warning

Data Structures Using C++ 2E16 The Pointer Data Type and Pointer Variables (cont’d.) Dynamic arrays –Static array limitation Fixed size at compile time Not possible for same array to process different data sets of the same type –Solution Declare array large enough to process a variety of data sets Problem: potential memory waste –Dynamic array solution Prompt for array size during program execution

Data Structures Using C++ 2E17 The Pointer Data Type and Pointer Variables (cont’d.) Dynamic arrays (cont’d.) –Dynamic array An array created during program execution –Dynamic array creation Use new operator –Example p=new int[10];

The Pointer Data Type and Pointer Variables (cont’d.) int *p; p = new int[10]; *p = 25; p++; *p = 55; p[0]=25; p[1]=55; … Data Structures Using C++ 2E18

Data Structures Using C++ 2E19 The Pointer Data Type and Pointer Variables (cont’d.) Functions and pointers –Pointer variable passed as parameter to a function By value or by reference –Declaring a pointer as a value parameter in a function heading Same mechanism used to declare a variable –Making a formal parameter be a reference parameter Use & when declaring the formal parameter in the function heading

Data Structures Using C++ 2E20 The Pointer Data Type and Pointer Variables (cont’d.) If I tell you the URL, I'm passing by reference. You can use that URL to see the same web page I can see. If that page is changed, we both see the changes. If you delete the URL, all you're doing is destroying your reference to that page - you're not deleting the actual page itself. If I print out the page and give you the printout, I'm passing by value. Your page is a disconnected copy of the original. You won't see any subsequent changes, and any changes that you make (e.g. scribbling on your printout) will not show up on the original page. If you destroy the printout, you have actually destroyed your copy of the object - but the original web page remains intact.

Data Structures Using C++ 2E21 The Pointer Data Type and Pointer Variables (cont’d.) Functions and pointers (cont’d.) –Example

Data Structures Using C++ 2E22 The Pointer Data Type and Pointer Variables (cont’d.) Dynamic two-dimensional arrays –Creation

Data Structures Using C++ 2E23 The Pointer Data Type and Pointer Variables (cont’d.) Dynamic two-dimensional arrays (cont’d.) –Declare board to be a pointer to a pointer int **board; –Declare board to be an array of 10 rows and 15 columns To access board components, use array subscripting notation Example 3-5!

Data Structures Using C++ 2E24 The Pointer Data Type and Pointer Variables (cont’d.) Shallow vs. Deep copy and pointers

Data Structures Using C++ 2E25 Shallow copy –Two or more pointers of same type points to same memory and same data The Pointer Data Type and Pointer Variables (cont’d.) FIGURE 3-16 Pointer first and its array FIGURE 3-17 first and second after the statement second = first; executes FIGURE 3-18 first and second after the statement delete [] second; executes

Data Structures Using C++ 2E26 The Pointer Data Type and Pointer Variables (cont’d.) Deep copy –Two or more pointers have their own data FIGURE 3-19 first and second both pointing to their own data

Data Structures Using C++ 2E27 Classes and Pointers: Some Peculiarities Class can have pointer member variables –Peculiarities of such classes exist FIGURE 3-20 Objects objectOne and objectTwo

Data Structures Using C++ 2E 28 Classes and Pointers: Some Peculiarities (cont’d.) Destructor –Could be used to prevent an array from staying marked as allocated Even though it cannot be accessed –If a class has a destructor Destructor automatically executes whenever a class object goes out of scope Put code in destructor to deallocate memory FIGURE 3-21 Object objectOne and its data pointerDataClass::~pointerDataClass() { delete [] p; } If p is not properly initialized or if it’s garbage and the destructor executes, either the program terminates with error or deallocates an unreleated memory space

Data Structures Using C++ 2E29 Classes and Pointers: Some Peculiarities (cont’d.) Assignment operator –Built-in assignment operators for classes with pointer member variables may lead to shallow copying of data FIGURE 3-22 Objects objectOne and objectTwo FIGURE 3-23 Objects objectOne and objectTwo after the statement objectTwo = objectOne; executes

Data Structures Using C++ 2E30 Classes and Pointers: Some Peculiarities (cont’d.) Assignment operator (cont’d.) –Overloading the assignment operator Avoids shallow copying of data for classes with a pointer member variable FIGURE 3-24 Objects objectOne and objectTwo

Data Structures Using C++ 2E31 Classes and Pointers: Some Peculiarities (cont’d.) Copy constructor –When declaring the class object Can initialize a class object by using the value of an existing object of the same type –Default memberwise initialization May result from copy constructor provided by compiler May lead to shallow copying of data Correct by overriding copy constructor definition provided by compiler –Syntax to include copy constructor in the definition of a class className(const className& otherObject);

Data Structures Using C++ 2E32 Summary: Classes and Pointers For classes with pointer member variables, these three things are normally done: 1.Include the destructor in the class 2.Overload the assignment operator for the class 3.Include the copy constructor

Data Structures Using C++ 2E33 Inheritance, Pointers, and Virtual Functions C++ allows the user to pass an object of a derived class to a formal parameter of the base class type Compile-time Binding vs Run-time Binding

Data Structures Using C++ 2E34 Inheritance, Pointers, and Virtual Functions

Data Structures Using C++ 2E35 Inheritance, Pointers, and Virtual Functions

Data Structures Using C++ 2E36 Inheritance, Pointers, and Virtual Functions

Data Structures Using C++ 2E37 Inheritance, Pointers, and Virtual Functions

Data Structures Using C++ 2E38 Inheritance, Pointers, and Virtual Functions

Data Structures Using C++ 2E39 Array-Based Lists List –Collection of elements of same type Length of a list –Number of elements in the list Many operations may be performed on a list Store a list in the computer’s memory –Using an array

Data Structures Using C++ 2E40 Array-Based Lists (cont’d.) Three variables needed to maintain and process a list in an array –The array holding the list elements –A variable to store the length of the list Number of list elements currently in the array –A variable to store array size Maximum number of elements that can be stored in the array Desirable to develop generic code –Used to implement any type of list in a program –Make use of templates

Data Structures Using C++ 2E41 Array-Based Lists (cont’d.) Define class implementing list as an abstract data type (ADT) FIGURE 3-29 UML class diagram of the class arrayListType

Data Structures Using C++ 2E42 Array-Based Lists (cont’d.) Definitions of functions isEmpty, isFull, listSize and maxListSize

Data Structures Using C++ 2E43 Array-Based Lists (cont’d.) Template print (outputs the elements of the list) and template isItemAtEqual

Data Structures Using C++ 2E44 Array-Based Lists (cont’d.) Template insertAt

Data Structures Using C++ 2E45 Array-Based Lists (cont’d.) Template insertEnd and template removeAt

Data Structures Using C++ 2E46 Array-Based Lists (cont’d.) Template retrieveAt, template replaceAt and template clearList

Data Structures Using C++ 2E47 Array-Based Lists (cont’d.) Definition of the constructor and the destructor

Data Structures Using C++ 2E48 Array-Based Lists (cont’d.) Copy constructor –Called when object passed as a (value) parameter to a function –Called when object declared and initialized using the value of another object of the same type –Copies the data members of the actual object into the corresponding data members of the formal parameter and the object being created

Data Structures Using C++ 2E49 Array-Based Lists (cont’d.) Copy constructor (cont’d.) –Definition

Data Structures Using C++ 2E50 Array-Based Lists (cont’d.) Overloading the assignment operator – Definition of the function template

Data Structures Using C++ 2E51 Array-Based Lists (cont’d.) Searching for an element –Linear search example: d etermining if 27 is in the list –Definition of the function template FIGURE 3-32 List of seven elements

Data Structures Using C++ 2E52 Array-Based Lists (cont’d.) Inserting an element

Data Structures Using C++ 2E53 Array-Based Lists (cont’d.) Removing an element

Assignments Next lecture –Read Chapter 5 (Linked Lists) –Examine and CODE Programming Examples Video Store (Pg. 327) All semester –Bring your book to lecture and lab –Code the examples in book –Monitor course website, and –Share best practices –CODE! CODE! CODE! Data Structures Using C++ 2E54