Download presentation
Presentation is loading. Please wait.
1
CS148 Introduction to Programming II
Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 16: 3/24/2003 Lecture 16: 3/24/2003 CS148 Spring 2003
2
Outline Classes and dynamic data Use of destructors Shallow copy
Deep copy using a deep copy function Deep copy using a copy-constructor Lecture 16: 3/24/2003 CS148 Spring 2003
3
Destructors and Dynamic Data 1/2
When a class instance allocates dynamic data on the free store (using new) , a destructor is used to properly deallocate the previously allocated data (using delete) Dynamically Allocates data pointed to by msgStr class Date { public: Date (int Mo, int Day, int Yr, const char* msgStr); void print(); ~Date(); //destructor invoked when a class object is destroyed private: int mo; int day; int yr; char* msg; }; Deallocates data pointed to by msgStr Lecture 16: 3/24/2003 CS148 Spring 2003
4
Destructors and Dynamic Data 2/2
When a class instance allocates dynamic data on the free store (using new in a constructor) , a destructor is used to properly deallocate the previously allocated data (using delete) 4 msg Free Store ‘X’ ‘y’ ‘Z’ ‘\0’ 15 2001 Date::Date(int Mo, int Day, int Yr, const char* msgStr) { … //allocate space for msg msg = new char[strlen(msgStr) + 1]; //copy the contents of msgStr into msg strcpy (msg,msgStr); } Date::~Date() delete [] msg; void main () //create an instance of class Date Date date1 (4,15,2001,“XyZ"); Free Store After the call to destructor Lecture 16: 3/24/2003 CS148 Spring 2003
5
Shallow copy When using the built in assignment operator with class objects a shallow copy is performed. A shallow copy copies one class object to another without copying any pointed-to data Free Store ‘X’ ‘y’ ‘Z’ ‘\0’ 4 15 2001 5 16 2002 ‘A’ ‘B’ ‘C’ ‘D’ void main () { //create an instance of class Date Date date1 (4,15,2001,“XyZ"); Date date2 (5,16,2002,”ABCD”); date1 = date2; } Free Store ‘X’ ‘y’ ‘Z’ ‘\0’ 5 16 2002 ‘A’ ‘B’ ‘C’ ‘D’ What happens when the destructor is invoked when main function is exited? Lecture 16: 3/24/2003 CS148 Spring 2003
6
Deep copy using a deep copy function 1/2
A deep copy is an operation that not only copies one class object to another but also makes copies of any pointed-to data Instead of built-in assignment operator use a deep copy function //deep copy function void Date::copyFrom (Date otherDate) { mo = otherDate.mo; day = otherDate.day; yr = otherDate.yr; delete []msg; msg = new char[strlen(otherDate.msg)+1]; strcpy(msg,otherDate.msg); } void main () //create an instance of class Date Date date1 (4,15,2001,“XyZ"); Date date2 (5,16,2002,”ABCD”); date1.copyFrom(date2); What happens when date2 is passed by value to copyFrom? What happens when copyFrom is exited? What happens when the destructor is called upon main function exit? Lecture 16: 3/24/2003 CS148 Spring 2003
7
Deep copy using a deep copy function 2/2
date1.copyFrom(date2); By default, date2 is copied to the copyFrom function using a shallow copy When copyFrom is exiting, date2 copy is destroyed prompting a call to the destructor The destructor deallocates the original pointed-to char array Free Store ‘X’ ‘y’ ‘Z’ ‘\0’ 4 15 2001 5 16 2002 ‘A’ ‘B’ ‘C’ ‘D’ main function 5 16 2002 copyFrom function Copy of date2 Created using a shallow copy Lecture 16: 3/24/2003 CS148 Spring 2003
8
Deep copy using a copy-constructor
Include a copy-constructor Date (const Date & otherDate); The copy constructor will be invoked when Passing a copy of an argument to a parameter (pass by value) Initialization in a variable declaration Date date3 = date2; Returning an object as the value of a function return someObject; //copy constructor Date::Date (const Date& otherDate) { mo = otherDate.mo; day = otherDate.day; yr = otherDate.yr; msg = new char[strlen(otherDate.msg)+1]; strcpy(msg,otherDate.msg); } void main () //create an instance of class Date Date date1 (4,15,2001,“XyZ"); Date date2 (5,16,2002,”ABCD”); date1.copyFrom(date2); The date2 copy will be created by invoking the copy-constructor on date2 Lecture 16: 3/24/2003 CS148 Spring 2003
9
Deep copy using a copy-constructor 2/2
date1.copyFrom(date2); //in the presence of a copy-constructor Free Store ‘X’ ‘y’ ‘Z’ ‘\0’ ‘A’ ‘B’ ‘C’ ‘D’ main function 4 15 2001 5 16 2002 When copyFrom is exiting, date2 copy is destroyed prompting a call to the destructor The destructor deallocates the dynamic char array created using the copy-constructor leaving the one pointed to by date2.msg intact ‘A’ ‘B’ ‘C’ ‘D’ ‘\0’ 5 16 2002 copyFrom function Copy of date2 created using the copy-constructor Lecture 16: 3/24/2003 CS148 Spring 2003
10
Summary When dealing with classes and dynamic data provide the following functions A destructor to deallocate dynamic data A deep copy function that copies pointed-to data in addition to class object members A copy-constructor to guarantee deep copying of class objects If deep copying is not provided, the default is shallow copying which does not copy pointed-to data Lecture 16: 3/24/2003 CS148 Spring 2003
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.