Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.

Slides:



Advertisements
Similar presentations
Data Structures Using C++ 2E
Advertisements

Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
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.
Chapter 13: Overloading.
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
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
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.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
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.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
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.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
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
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
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.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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:
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.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Pointer Data Type and Pointer Variables III CS1201: Programming Language 2 By: Nouf Aljaffan Edited by : Nouf Almunyif.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
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.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Data Structures Using C++ 2E
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
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
Pointer Data Type and Pointer Variables III
Presentation transcript:

Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout 2014 Some content based on Book: Data Structures Using C++ 2 nd Ed. by D.S. Malik Review-ish of stuff from previous lecture Different perspective

Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore how to declare and manipulate pointer variables Learn about the address of operator and dereferencing operator Discover dynamic variables Examine how to use the new and delete operators to manipulate dynamic variables Learn about pointer arithmetic

Data Structures Using C++ 2E3 Objectives (cont’d.) Discover dynamic arrays Become aware of the shallow and deep copies of data Discover the peculiarities of classes with pointer data members Explore how dynamic arrays are used to process lists Learn about virtual functions Become aware of abstract classes

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

Data Structures Using C++ 2E5 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;

Data Structures Using C++ 2E6 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 * )

Data Structures Using C++ 2E7 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

Data Structures Using C++ 2E8 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++ 2E9 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++ 2E10 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++ 2E11 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

Data Structures Using C++ 2E12 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++ 2E13 The Pointer Data Type and Pointer Variables (cont’d.) Dynamic arrays –Static array limitation Fixed size 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++ 2E14 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];

Data Structures Using C++ 2E15 The Pointer Data Type and Pointer Variables (cont’d.) Array name: a constant pointer –Array name value: constant –Increment, decrement operations cannot be applied FIGURE 3-14 list and array list FIGURE 3-15 Array list after the execution of the statements list[0] = 25; and list[2] = 78;

Data Structures Using C++ 2E16 The Pointer Data Type and Pointer Variables (cont’d.) Shallow vs. deep copy and pointers –Pointer arithmetic may create unsuspected or erroneous results –Shallow copy Two or more pointers of same type Points to same memory Points to same data

Data Structures Using C++ 2E17 Shallow copy 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++ 2E18 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++ 2E19 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++ 2E20 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

Data Structures Using C++ 2E21 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++ 2E22 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++ 2E23 Abstract Classes and Pure Virtual Functions Virtual functions enforce run-time binding of functions Inheritance –Allows deriving of new classes without designing them from scratch –Derived classes Inherit existing members of base class Can add their own members Can redefine or override public and protected base class member functions –Base class can contain functions each derived class can implement

Data Structures Using C++ 2E24 Abstract Classes and Pure Virtual Functions (cont’d.) Virtual functions enforce run-time binding of functions (cont’d.) –Pure virtual functions –Abstract class Class contains one or more pure virtual functions Not a complete class: cannot create objects of that class. Can contain instance variables, constructors, functions not pure virtual

Marker Slide Questions on –Pointers? Next Up –Array Lists Data Structures Using C++ 2E25

Data Structures Using C++ 2E26 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++ 2E27 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++ 2E28 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++ 2E29 Array-Based Lists (cont’d.) TABLE 3-1 Time complexity of list operations

Data Structures Using C++ 2E30 Summary Pointers contain memory addresses –All pointers must be initialized –Static and dynamic variables –Several operators allowed Static and dynamic arrays Virtual functions –Enforce run-time binding of functions Array-based lists –Several operations allowed –Use generic code

The End Or is it? –Go to next presentation