1 / 10

CS148 Introduction to Programming II

CS148 Introduction to Programming II. Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 16: 3/24/2003. Outline. Classes and dynamic data Use of destructors Shallow copy Deep copy using a deep copy function Deep copy using a copy-constructor.

moses
Download Presentation

CS148 Introduction to Programming II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University 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 CS148 Spring 2003

  3. Dynamically Allocates data pointed to by msgStr Deallocates data pointed to by msgStr 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) 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; }; CS148 Spring 2003

  4. Free Store 4 15 2001 ‘X’ ‘y’ ‘Z’ ‘\0’ msg Free Store After the call to destructor 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) • 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"); • } CS148 Spring 2003

  5. Free Store 5 5 4 16 15 16 2002 2001 2002 ‘X’ ‘X’ ‘A’ ‘A’ ‘y’ ‘y’ ‘B’ ‘B’ ‘Z’ ‘Z’ ‘C’ ‘C’ ‘\0’ ‘\0’ ‘D’ ‘D’ ‘\0’ ‘\0’ 5 Free Store 16 2002 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 • void main () • { • //create an instance of class Date • Date date1 (4,15,2001,“XyZ"); • Date date2 (5,16,2002,”ABCD”); • date1 = date2; • } What happens when the destructor is invoked when main function is exited? 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? CS148 Spring 2003

  7. main function 5 4 15 16 Free Store 2001 2002 ‘X’ ‘A’ ‘y’ ‘B’ ‘Z’ ‘C’ ‘\0’ ‘D’ ‘\0’ copyFrom function 5 Copy of date2 Created using a shallow copy 16 2002 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 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 CS148 Spring 2003

  9. main function Free Store 4 15 2001 5 16 ‘X’ ‘A’ ‘A’ ‘y’ ‘B’ ‘B’ ‘Z’ ‘C’ ‘C’ ‘\0’ ‘D’ ‘D’ ‘\0’ ‘\0’ 2002 copyFrom function 5 Copy of date2 created using the copy-constructor 16 2002 Deep copy using a copy-constructor 2/2 • date1.copyFrom(date2); //in the presence of a copy-constructor • 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 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 CS148 Spring 2003

More Related