@ Zhigang Zhu, 2002-2014 1 CSC212 Data Structure - Section FG Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Zhigang Zhu Department.

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Introduction to Programming Lecture 39. Copy Constructor.
1 CSC211 Data Structures Lectures 4 & 5 Container Classes Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
@ Zhigang Zhu, CSC212 Data Structure - Section RS Lectures 6/7 Pointers and Dynamic Arrays Instructor: Zhigang Zhu Department of Computer Science.
Reviews for Exam 1 Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, Fall 2010.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Zhigang Zhu Department.
1 CSC212 Data Structure - Section FG Lectures 4 & 5 Container Classes Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Reviews for Exam 1 Chapter 1-4 CS 211 Data Structures MHC, 2007.
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Zhigang Zhu Department.
Pointers1 Pointers & Dynamic Arrays Allocating memory at run-time.
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.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
OOP Languages: Java vs C++
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
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.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Xiaoyan Li, CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer.
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.
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
Edgardo Molina, CSC212 Data Structure - Section AB Lectures 6/7 Pointers and Dynamic Arrays Instructor: Edgardo Molina Department of Computer Science.
Memory Management Issues, Solutions, and Examples.
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
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.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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:
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Edgardo Molina, CSC212 Data Structure - Section AB Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Edgardo Molina Department.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
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.
Exam Review 2 Chapter 5 – 9 CSC212 FG CS Dept, CCNY.
Xiaoyan Li, CSC211 Data Structures Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Prof. Xiaoyan Li Department of Computer Science.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
@ George Wolberg, CSC212 Data Structure Lecture 6 Dynamic Classes and the Law of the Big Three Instructor: George Wolberg Department of Computer.
Pointers and Dynamic Arrays
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
Chapter 1-4 CSc 212 Data Structures, Sec AB CCNY, Spring 2012
This pointer, Dynamic memory allocation, Constructors and Destructor
The Bag and Sequence Classes with Linked Lists
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Array Lists Chapter 6 Section 6.1 to 6.3
Classes with Dynamically Allocated Data
CSC212 Data Structure - Section FG
CSC212 Data Structure - Section RS
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Rule of Three Part 1 & 2.
Copy Constructors and Overloaded Assignment
Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, 2009
SPL – PS3 C++ Classes.
Presentation transcript:

@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Zhigang Zhu Department of Computer Science City College of New York

@ Zhigang Zhu, Why Dynamic Classes p Limitation of our bag class p bag::CAPACITY constant determines the capacity of every bag p wasteful and hard to reuse p Solution: p provide control over size in running time, by p pointers and dynamic memory p => dynamic arrays p => dynamic classes

@ Zhigang Zhu, Dynamic Classes New Features (Ch 4.3–4) p Pointers Member Variables p Dynamic Memory Allocation (where and how) p Value Semantics (what’s new?) p assignment operator overloading p your own copy constructor p Introducing Destructor p Conclusion: the Law of the Big Three

@ Zhigang Zhu, Pointer Member Variable  The Static bag  The Dynamic bag // From bag1.h in Section 3.1 class bag { public: static const size_t CAPACITY = 20;... private: value_type data[CAPACITY]; size_type used; }; // From bag2.h in Section 4.3 class bag { public:... private: value_type *data; size_type used; size_type capacity; };

@ Zhigang Zhu, Invariant of the Dynamic bag  the number of items is in the member variable used  The actual items are stored in a partially filled array. The array is a dynamic array, pointed to by the pointer variable data  The total size of the dynamic array is the member variable capacity p Invariant is about rules of implementation...

@ Zhigang Zhu, Allocate Dynamic Memory: Where? p In p In Old Member Functions  constructor  constructor – how big is the initial capacity?  insert  insert – if bag is full, how many more?  +/+=  +/+= operators operators – how to combine two bags? pNew Member Functions  reserve  reserve – explicitly adjust the capacity pExample pconstructor with default size

@ Zhigang Zhu, Allocate Dynamic Memory: How?  In constructor : p why initialize? p how? p default p specific size // From bag2.h in Section 4.3 class bag { public: static const size_t DEFAULT_CAPACITY = 20; bag(size_type init_cap = DEFAULT_CAPACITY);... private: value_type *data; size_type used; size_type capacity; }; // From implementation file bag2.cxx bag::bag(size_type init_cap) { data = new value_type[init_cap]; capacity = init_cap; used = 0; }

@ Zhigang Zhu, Value Semantics p Assignment operator p y = x; p Copy constructor p bag y(x); // bag y = x; Automatic assignment operator and copy constructor p copy all the member variables (data, used, capacity) from object x to object y p but our days of easy contentment are done!

@ Zhigang Zhu, Failure in auto assignment operator x bag x(4), y(5); x.insert(18); x.insert(19); y=x; x.insert(20); capacity used data y50964 ???? ????? [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] Question: What will happen after executing lines 2 – 5?

@ Zhigang Zhu, Failure in auto assignment operator x bag x(4), y(5); x.insert(18); x.insert(19); y=x; x.insert(20); capacity used data y ?? ????? [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] Question: What will happen after executing lines 2 – 5?

@ Zhigang Zhu, Failure in auto assignment operator x bag x(4), y(5); x.insert(18); x.insert(19); y=x; x.insert(20); capacity used data y ?? ????? [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] Question: What will happen after executing lines 2 – 5? lost memory

@ Zhigang Zhu, Failure in auto assignment operator x bag x(4), y(5); x.insert(18); x.insert(19); y=x; x.insert(20); capacity used data y ? ????? [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] Consequence: Change to x’ array will also change y’s array lost memory

@ Zhigang Zhu, If we want y to have its own dynamic array x bag x(4), y(5); x.insert(18); x.insert(19); y=x; x.insert(20); capacity used data y ?? ????? [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3] [4] [0] [1] [2] [3] [4]

@ Zhigang Zhu, Dynamic memory allocation is needed x bag x(4), y(5); x.insert(18); x.insert(19); y=x; x.insert(20); capacity used data y ?? ????? [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] Answer: overloading the assignment operator = memory de-allocated 1819??

@ Zhigang Zhu, Dynamic memory allocation is needed x bag x(4), y(5); x.insert(18); x.insert(19); y=x; x.insert(20); capacity used data y ? ????? [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] Answer: overloading the assignment operator = memory de-allocated 1819??

@ Zhigang Zhu, Solution: overloading assignment operator p Your own assignment operator p C++ Requires the overloaded assignment operator to be a member function bag x, y ; // OR bag x(4), y(5); // OR.... y=x; // y.operator=(x); void bag::operator=(const bag& source) // Postcondition: The bag that activated this function has the same items and capacity as source A 5-minute Quiz: write your own implementation - turn in // From bag2.h in Section 4.3 class bag { public: static const size_t DEFAULT_CAPACITY = 20; bag(size_type init_cap = DEFAULT_CAPACITY);... private: value_type *data; size_type used; size_type capacity; }; // From implementation file bag2.cxx bag::bag(size_type init_cap) { data = new value_type[init_cap]; capacity = init_cap; used = 0; }

@ Zhigang Zhu, Implementation of operator= p y = x; p y  *this p x  source void bag::operator =(const bag& source) // Library facilities used: algorithm { value_type *new_data; // Check for possible self-assignment: if (this == &source) return; // If needed, allocate an array with a different size: if (capacity != source.capacity) { new_data = new value_type[source.capacity]; delete [ ] data; // make sure all valid before delete!!! data = new_data; capacity = source.capacity; } // Copy the data from the source array: used = source.used; copy(source.data, source.data + used, data); }

@ Zhigang Zhu, The 2 nd part of the value semantics copy constructor

@ Zhigang Zhu, Break p Programming Assignment 2 Due October 1st (Wed)! p Assignment 3 is online, due Oct 15 (Wed) p Next Class: Exam review p Oct 06 Monday: First Exam 4:00 – 5:30 pm

@ Zhigang Zhu, The 2 nd part of the value semantics copy constructor

@ Zhigang Zhu, Auto Copy Constructor: shallow copy x bag x(4) bag y(x); x.insert(18); x.insert(19); capacity used data y40984 ???? [0] [1] [2] [3] [0] [1] [2] [3] The only difference with auto assignment is: y does not have its own data

@ Zhigang Zhu, Failure in auto copy constructor x bag x(4); bag y(x); x.insert(18); x.insert(19); capacity used data y ?? [0] [1] [2] [3] [0] [1] [2] [3] change to x also changes y

@ Zhigang Zhu, Deep copy: providing your own copy constructor p Questions on Implementation (homework!) p do you need to check self-copy p bag y(x); // never have bag y(y); p do you need to delete old bag? p Questions on Usage p 4 different ways that copy constructor is used bag::bag(const bag& source) // Postcondition: The bag that has been constructed has the same items and capacity as source

@ Zhigang Zhu, Four common situations p Declaration bag y(x); bag y(x); p Declaration with Alternate Syntax bag y = x ; bag y = x ; p Returning an object from a function bag union(const bag& s1, const bag& s2); bag union(const bag& s1, const bag& s2); p Value parameter is an object void temp_bag_copy(bag clone); void temp_bag_copy(bag clone);

@ Zhigang Zhu, What’s missing? allocate dynamic memory via new, take care of the value semantics,....?

@ Zhigang Zhu, De-allocation of dynamic memory p Return an object’s dynamic memory to the heap when the object is no longer in use p Where and How? – Two ways p Take care of it yourself  delete dynamic data of an object after you’re done with it p let the program do it automatically p destructor

@ Zhigang Zhu, Destructor p The primary purpose is to return an object’s dynamic memory to the heap, and to do other “cleanup” p Three unique features of the destructor p The name of the destructor is always ~ followed by the class name; p No parameters, no return values; p Activated automatically whenever an object becomes inaccessible Question: when this happens? Question: when this happens? bag::~bag() { delete [ ] data; }

@ Zhigang Zhu, Destructor p Some common situations causing automatic destructor activation p Upon function return, objects as local variables destroyed; p Upon function return, objects as value parameters destroyed; p when an object is explicitly deleted Question: shall we put destructor in how-to-use-a- bag documentation? Question: shall we put destructor in how-to-use-a- bag documentation? bag::~bag() { delete [ ] data; }

@ Zhigang Zhu, The Law of the Big Three p Using dynamic memory requires the following three things all together p a destructor p a copy constructor (and of course an ordinary one) p an overloaded assignment operator p In other words, the three functions come in a set – either you need to write all three yourself, or you can rely on the compiler-supplied automatic versions of all the three.

@ Zhigang Zhu, What will happen if not? If we only have a constructor and a destructor, but do not provide a copy constructor and an overloaded assignment operator

@ Zhigang Zhu, Importance of the Law of Big-3 bag *x, *y; x = new bag(4); y = new bag(5); x->insert(18); x->insert(19); *y = *x; delete x; y->insert(20); Question: What will happen after executing lines 1 – 8? // destructor bag::~bag() { delete [ ] data; } // constructor bag::bag(size_type init_cap) { data = new value_type[init_cap]; capacity = init_cap; used = 0; }

@ Zhigang Zhu, Importance of the Law of Big-3 *x*x*x*x bag *x, *y; x = new bag(4); y = new bag(5); x->insert(18); x->insert(19); *y = *x; delete x; y->insert(20); capacity used data *y*y*y*y50964 ???? ????? [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] allocate memory for objects (*x, *y) and their dynamic arrays // From implementation file bag2.cxx bag::bag(size_type init_cap) { data = new value_type[init_cap]; capacity = init_cap; used = 0; }

@ Zhigang Zhu, Importance of the Law of Big-3 bag *x, *y; x = new bag(4); y = new bag(5); x->insert(18); x->insert(19); *y = *x; delete x; y->insert(20); capacity used data ?? ????? [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] Insert two items in the dynamic array of object *x *x*x*x*x *y*y*y*y

@ Zhigang Zhu, Importance of the Law of Big capacity used data ?? ????? [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] automatic assignment only copies three variables (capacity, used and data) from *x to *y lost memory bag *x, *y; x = new bag(4); y = new bag(5); x->insert(18); x->insert(19); *y = *x; delete x; y->insert(20); *x*x*x*x *y*y*y*y

@ Zhigang Zhu, Importance of the Law of Big ????? [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] Deleting x will also delete the dynamic array of *x by calling the destructor dangling pointer lost memory bag *x, *y; x = new bag(4); y = new bag(5); x->insert(18); x->insert(19); *y = *x; delete x; y->insert(20); bag::~bag() { delete [ ] data; } *y*y*y*y

@ Zhigang Zhu, Importance of the Law of Big-3 *y*y*y*y ????? [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] Your program crashes: *y needs its own copy of data !!! dangling pointer lost memory bag *x, *y; x = new bag(4); y = new bag(5); x->insert(18); x->insert(19); *y = *x; delete x; y->insert(20);

@ Zhigang Zhu, Reading and Programming Assignments p Putting pieces together p bag2.h, bag2.cxx both in textbook and online online p Self-test exercises p p After-class reading (string) p Section 4.5, Self-Test (within exam scope) p Programming Assignment 2 Due Oct 01 (Wed)! p Assignment 3 is online, due Oct 15 (Wed) p Next Class: Exam review p Oct 06 Monday: First Exam 4:00 – 5:30 pm