140 likes | 264 Views
ECE 264 Object-Oriented Software Development. Instructor: Dr. Honggang Wang Fall 2012 Lecture 28: Destructors and Copy Constructors. Lecture outline. Announcements / reminders Design due 11/19 Submit either through group folder or via e-mail
E N D
ECE 264Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 28: Destructors and Copy Constructors
Lecture outline • Announcements / reminders • Design due 11/19 • Submit either through group folder or via e-mail • Lab session time (2:00 -3:15 pm, Wednesday, 11/07) • Project groups to work on the design, and meet with Dr. Wang in his office; Attendance are required. The meeting schedule is as follows • 2:00-2:15 pm meeting with group 1 • 2:15-2:30 pm meeting with group 2 • 2:30-2:45 pm meeting with group 3 • 2:45-3:00 pm meeting with group 4 • Exam 2 at 9:00-9:50 on 11/14 (Wednesday) • Review on Friday (11/09) • Today • Brief review of dynamic memory allocation • Destructors • Copy constructors ECE 264: Lecture 26
Destructors • Destructors: function called when object is destroyed and used for “object cleanup” • When are these functions called? • End of function • When dynamically allocated object is freed • When are destructors really necessary? • When object contains dynamically allocated data • General syntax: similar to constructor <class name>::~<class name>() {} ECE 264: Lecture 26
Destructor example class dynamicIntArray { private: int n_elem; // size of array int *arr; public: dynamicIntArray(); dynamicIntArray(int n); ~dynamicIntArray(); … } // Assume array initially has 0 elements dynamicIntArray::dynamicIntArray() : n_elem(0) { arr = NULL; n_elem = 0; } dynamicIntArray::dynamicIntArray(int n) : n_elem(n) { arr = new int[n_elem]; } dynamicIntArray::~dynamicIntArray() { delete [] arr; } ECE 264: Lecture 26
Composition and destructors • If a class has a data object as a member, the member destructor is automatically called • DO NOT explicitly call destructors! tenElementArr::tenElementArr : dia(10) { } • tenElementArr ::~tenElementArr() • { /* empty destructor—arr’s destructor called automatically */ } class tenElementArr{ private: dynamicIntArray dia; public: tenElementArr(); ~tenElementArr(); … } ECE 264: Lecture 26
Copy constructors: Constructor example • Given following code, on what lines are constructors called? • Assume functions f1 and f2 have following prototype: • void f(Point p); • void f2(Point &p); • Point p1, p2; • Point p3(3,7); • Point p4 = p3; • p2 = p3; • f(p4); • f2(p3); • Answer: all lines except lines 4 & 6 • Clearly declaring new objects in lines 1-3 • Default in line 1 • Parameterized in line 2 • No new object in line 4 • Pass by value—create new object and copy data members from argument (line 5) • Pass by reference—copy pointer (line 6) ECE 264: Lecture 26
Copy constructors • We’ve seen two forms of constructors • Default • Parameterized • Third type of constructor: copy constructor • Used to initialize a newly declared variable from an existing variable • Not called for assignments • Example: Point p1(2,3), p3; Point p2 = p1; // calls copy constructor p3 = p2; // uses assignment • Often generated by default ECE 264: Lecture 26
Basic copy constructor: Point Point::Point(const Point &p) { xCoord = p.xCoord; yCoord = p.yCoord; } • Argument p • Passed by reference • Specified as const • Function cannot change value of p • Copies all data members from p to current object ECE 264: Lecture 26
Default copy constructors • By default, copy constructor performs a shallow copy • Directly copies data members from one object to the other • When is a shallow copy a problem? • Pointer-based data • Arrays • Dynamically allocated data (scalars and arrays) • In these cases, prefer deep copy ECE 264: Lecture 26
Example: deep copy Say we have the following class: class tenInts { private: int arr[10]; ... } • What would copy constructor look like? ECE 264: Lecture 26
Example: deep copy (cont.) tenInts::tenInts(const tenInts &t){ for (int i=0; i < 10; i++) arr[i] = t.arr[i]; } • Copy array values element by element • Note: If class contained scalar values, would have to copy them as well • Copy constructor must account for all variables in class—even those that would have been handled correctly by the default shallow copy ECE 264: Lecture 26
Example: revisit dynamicIntArray • Say we want to add a copy constructor to the dynamicIntArray class shown earlier: class dynamicIntArray { private: int n_elem; // size of array int *arr; public: dynamicIntArray(); dynamicIntArray(int n); ~dynamicIntArray(); … } • What change(s) would we need to make to the .h file? • How would we write the code for this function in the .cpp file? ECE 264: Lecture 26
Solution • Add the following to the .h file: dynamicIntArray(const dynamicIntArray &); • Write the function as follows in the .cpp file: dynamicIntArray::dynamicIntArray(const dynamicIntArray &d) { n_elem = d.n_elem; for (int i = 0; i < n_elem; i++) arr[i] = d.arr[i]; } ECE 264: Lecture 26
Final notes • Next time • Exam 2 review and Operator overloading • Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: • Deitel & Deitel, C++ How to Program, 8th ed. • Etter & Ingber, Engineering Problem Solving with C++, 2nd ed. ECE 264: Lecture 26