Chapter 15-3 Pointers, Dynamic Data, and Reference Types

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Introduction to Programming Lecture 39. Copy Constructor.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 13: Pointers, Classes, Lists, and Virtual Functions.
Dynamically Allocated Arrays May 2, Quiz 5 Today.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Pointer Data Type and Pointer Variables
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
1 Data Structures and Algorithms Pointers and Dynamic Data.
Copy Constructors Fall 2008 Dr. David A. Gaitros
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Pointers You are not responsible for virtual functions (starting on.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Chapter 14 Dynamic Data and Linked Lists
Constructors and Destructors
Pointers and Dynamic Arrays
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CISC181 Introduction to Computer Science Dr
C++ Plus Data Structures
C++ Object-Oriented Programming
Constructor & Destructor
Class Operations Pointer and References with class types
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
group work #hifiTeam
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Automatics, Copy Constructor, and Assignment Operator
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Foundational Data Structures
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Constructors and Destructors
Indirection.
CS148 Introduction to Programming II
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
9-10 Classes: A Deeper Look.
COP 3330 Object-oriented Programming in C++
A Deeper Look at Classes
Class: Special Topics 2 For classes using memory allocation
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
9-10 Classes: A Deeper Look.
Copy Constructors and Overloaded Assignment
SPL – PS3 C++ Classes.
Presentation transcript:

Chapter 15-3 Pointers, Dynamic Data, and Reference Types Dale/Weems

What happens . . . When a function is called that passes a DynArray object by value, what happens? 2000 DynArray ? 75 Private: size 5 arr 2000 ~DynArray DynArray ValueAt Store CopyFrom

Passing a Class Object by Value // Function code void SomeFunc(DynArray someArr) // Uses pass by value { . } 3

By default, Pass-by-value makes a shallow copy DynArray beta(5); // Client code . SomeFunc(beta); // Function call beta someArr DynArray . 2000 DynArray . ? 75 Private: size 5 arr 2000 Private: size 5 arr 2000 shallow copy

Suppose SomeFunc calls Store void SomeFunc(DynArray someArr) // Uses pass by value { someArr.Store(290, 2); . } What happens in the shallow copy scenario? 5

beta.arr[2] has changed DynArray beta(5); // Client code SomeFunc(beta); beta someArr DynArray . 2000 DynArray . ? 290 Private: size 5 arr 2000 Private: size 5 arr 2000 shallow copy

beta.arr[2] has changed Although beta is passed by value, its dynamic data has changed! beta someArr DynArray . 2000 DynArray . ? 290 Private: size 5 arr 2000 Private: size 5 arr 2000 shallow copy

Shallow Copy vs. Deep Copy A shallow copy copies only the class data members, and does not make a copy of any pointed-to data A deep copy copies not only the class data members, but also makes a separate stored copy of any pointed-to data

What’s the difference? A shallow copy shares the pointed to dynamic data with the original class object A deep copy makes its own copy of the pointed to dynamic data at different locations than the original class object

Making a (Separate) Deep Copy beta someArr DynArray . 2000 ? 75 Private: size 5 arr 2000 DynArray . 4000 ? 75 Private: size 5 arr 4000 deep copy

Initialization of Class Objects C++ defines initialization to mean initialization in a variable declaration passing an object argument by value returning an object as the return value of a function By default, C++ uses shallow copies for these initializations

As a result . . . When a class has a data member that points to dynamically allocated data, you must write what is called a copy constructor The copy constructor is implicitly called in initialization situations and makes a deep copy of the dynamic data in a different memory location

More about Copy Constructors When you provide (write) a copy constructor for a class, the copy constructor is used to make copies for pass by value You do not explicitly call the copy constructor Like other constructors, it has no return type Because the copy constructor properly defines pass by value for your class, it must use pass by reference in its definition

Copy Constructor Copy constructor is a special member function of a class that is implicitly called in these 3 situations: Passing object parameters by value Initializing an object variable in its declaration Returning an object as the return value of a function

SomeFunc(beta); // copy-constructor // beta passed by value beta someArr 2000 4000 DynArray DynArray ? 75 ? 75 Private: size 5 arr 2000 Private: size 5 arr 4000 ~DynArray ~DynArray DynArray DynArray ValueAt ValueAt Store Store CopyFrom CopyFrom deep copy

Classes with Data Member Pointers Need CONSTRUCTOR COPY CONSTRUCTOR DESTRUCTOR

DynArray::DynArray(const DynArray& otherArr) // Copy constructor // Implicitly called for deep copy in initializations // POST: If room on free store THEN // new array of size otherArr.size is created // on free store && arr == its base address // && size == otherArr.size // && arr[0..size-1] == otherArr.arr[0..size-1] // ELSE error occurs { int i; size = otherArr.size; arr = new int[size]; // Allocate memory for copy for (i = 0; i< size; i++) arr[i] = otherArr.arr[i]; // Copies array } 17

What about the assignment operator? The default method used for assignment of class objects makes a shallow copy If your class has a data member that points to dynamic data, you should write a member function to create a deep copy of the dynamic data

gamma.CopyFrom(beta); gamma beta 3000 2000 DynArray DynArray ? 75 ? 75 Private: size 5 arr 3000 Private: size 5 arr 2000 ~DynArray ~DynArray DynArray DynArray ValueAt ValueAt Store Store CopyFrom CopyFrom deep copy

void DynArray::CopyFrom (/* in */ DynArray otherArr) // Creates a deep copy of otherArr // POST: Array pointed to by arr@entry deallocated // && IF room on free store // THEN new array is created on free store // && arr == its base address // && size == otherArr.size // && arr[0..size-1] == otherArr[0..size-] // ELSE halts with error message { int i; delete[ ] arr; // Delete current array size = otherArr.size; arr = new int [size]; // Allocate new array for (i = 0; i< size; i++) // Deep copy array arr[i] = otherArr.arr[i]; } 20

Appointment Calendar Day Insert art on page 824

The End of Chapter 15 Part 3