Home / Expert Answers / Computer Science / lab-4-copy-constructors-assignment-operators-deep-vs-shallow-copy-templates-and-iterators-at-pa518

(Solved): Lab 4: Copy constructors, assignment operators, deep vs. shallow copy; templates and iterators at ...



Lab 4: Copy constructors, assignment operators, deep vs. shallow copy; templates and iterators at
Due Wednesday by 11:59pm PoExercise 2: Template functions and classes. Be sure to create all necessary driver file, hh file and .cpp files. (60 minutes

Lab 4: Copy constructors, assignment operators, deep vs. shallow copy; templates and iterators at Due Wednesday by 11:59pm Points Submitting a file upload Available Jan 23 at - Feb 1 at 11:59pm Attention: All files used in this lab can be downloaded by following this link, open Lab4, select and download the Lab4AllFiles folder (you will receive a .zip file containing all the files in that folder). Exercise 1: This exercise is designed to present a class that has both a copy constructor and an assignment operator and to illustrate the difference between deep and shallow copies. Please download and compile the code in Lab4/Exercise1 (MyClass.h, MyClass.cpp, and CopyAssignTest.cpp). Then do the following (30 minutes coding): 1. Question: Look at the code; do you think that MyClass::operator=() is doing a deep or shallow copy? Take Away: Know the difference between deep and shallow copy. 2. Question \& Coding: Do you notice any warning posted by the compiler? Is there a memory leak issue? How can you fix it? Take Away: How to eye-ball memory leak (constructor and destructor) 3. Coding: Is MyClass::setlp() implemented correctly? What exactly does it do? Take Away: What to watch out for when assigning value to a pointer type. 4. Coding: Derive a new subclass of MyClass, name it "MySubClass" and define a private "int" type data member named "subClassData" in this new class. What do you need to do is to ensure that the correct copy constructor and assignment operators are called. Verify that your new class works correctly. Take Away: When to and how to call super class constructor/methods Exercise 2: Template functions and classes. Be sure to create all necessary driver file, hh file and .cpp files. (60 minutes coding) 1. Coding: Write a template function for bubble sort. Take Away: Bubble sort is not supposed to be challenging, practice template syntax 2. Coding: Dynamically allocate an array of Square objects, using the Square Class provided in Lab4/Exercise2 (Square.h, Square.cpp) (with overloaded "<<" and "<" operators implemented). Take Away: Preparation for using your own template 3. Coding: Use the bubble sort function you created in step 1 to sort the array of Square objects and print out the sorted result. Take Away: [for next 3 steps]: Your template is useful! You can sort more than just numbers, you can sort anything under the Sun as long as they know how to compare themselves. 4. Interactive Coding: Update your Square class to be a template class 5. Coding: Use the template bubble sort function you created in step 1 to sort the array of Square objects of a specific type of your choice. Exercise 3: The next set of files have to do with templates and iterators. Download and compile the code in Lab4/Exercise3 (SquareContainer2.h, SquareContainer2.cpp, and IteratorTest.cpp) and consider the following activities ( 30 minutes coding): 1. Question: Look at the code in IteratorTest.cpp. What do you think it does? Compile the code (do you need to do anything "unusual" to get it to compile?) and run it. Does it do what you think it would? Take Away: Reminder of how to template class compilation, understanding of code. 2. Question: Why do the iterator increment operators return an iterator? How do the two increment operators differ? Is the iterator they return allocated dynamically, auto, or something else? Take Away: What does it mean to return a reference to self (in this case, and, in general, remember, overloaded operators typically return self).


We have an Answer from Expert

View Expert Answer

Expert Answer


We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe