Question: My code doesn't have shallow copy. I need it to be fixed. Chapter 10 Pointers Part 1 Create an int i with a value of
My code doesn't have shallow copy. I need it to be fixed.
Chapter 10 Pointers Part 1 Create an int i with a value of 7. Create a pointer to integer pi. Point your pointer pi to your int variable i. Print out your pointer, the address of your pointer and a dereference of your pointer. Create a pointer to your integer pointer ppi. Point it to your pointer to int pi. Print out ppi, the address of ppi, a dereference to ppi and a double dereference to ppi. Part 2 Understanding deep vs. shallow copy is essential for a programmer. You will get into severe problems trying to code if you do not understand it. The essence of the problem is that 2 objects, which should have independent memory storage, accidently wind up sharing memory. I want you to wrap a character array (and array with 'a','b','c','d','e' is strictly speaking not a string since it does not end in '\0') with a class (this is just a class that contains an array) and then properly(in Deep) and improperly (in Shallow) assign memory. Make a class WrapArrayDeep that has a private pointer to char. Your default constructor should allocate an array of size 5. You should have a copy constructor that does a deep copy. (allocates a new array) Your WrapArrayDeep class should start like: class WrapArrayDeep{ char *pch; WrapArrayDeep(){ pch = new char[5]; *pch = 97; //etc. } WrapArrayDeep(WrapArrayDeep wad){ // correct copy constructor. } } Make a similar class, WrapArrayShallow, that has an improper copy constructor that causes your copy to point to the array in the source object. (instead of making a new array, have pch point to the original array) Demonstrate the difference between the classes use WrapArrayDeep wad1, *wad2; for the variables holding your WrapArrayDeeps and for WrapArrayShallow: WrapArrayShallow was1, *was2; Be sure to include a destructor in each class note it must be an ARRAY destructor put a cout in the destructor showing it was called.. In WrapArrayDeep: Use pointer arithmetic to load your array with ASCII values for letters. *pca = 97; *(pca+1) = 98; etc. Use array notation to print your array. for(int I = 0; I < 5; I++) cout << pca[i] << endl; In WrapArrayShallow: Use array notation to load your array with char data. pca[0]='v'; pca[1]='w'; etc Use pointer arithmetic to print your array. for(int I = 0; I < 5; i++) cout << *(pca + 1) << endl; Example Output: this program section uses 3 variables i = 7, pi a pointer to i and ppi a pointer to pi pi = 002EF738 dereference pi 7 address of pi 002EF744 address of i 002EF738 ppi = 002EF744 dereference of ppi 002EF738 address of ppi 002EF72C double dereference of ppi 7 this section instantiates a wrapper class for a dynamic array of 5 elements WrapArrayDeep 1 a b c d e WrapArrayDeep 2 created using the copy constructor on 1 a b c d e after changing the contents of WrapArrayDeep 1, 1 and 3 = { | } ~ a b c d e Now doing the same thing with WrapArrayShallow wrapArrayShallow 1 a b c d e wrapArrayShallow 2 created using the copy constructor on 1 a b c d e after changing the contents of WrapArrayShallow 1, 1 and 3 = { | } ~ { | } ~ calling destructor for WrapArrayShallow calling destructor for WrapArrayShallow ***** this may or may not work depending on your compiler calling destructor for WrapArrayDeep calling destructor for WrapArrayDeep Press any key to continue . . . ***** If this crashes your program simply remove it.
My code:
#include
class WrapArrayDeep { public: //default and copy constructors, and destructor for WrapArrayDeep WrapArrayDeep(); WrapArrayDeep(const WrapArrayDeep& wad); ~WrapArrayDeep(); //functions to print and alter the array void printArray(); void alterArray(); //return the capacity of the array int getCapacity() const { return capacity; } //overloaded = operator WrapArrayDeep& operator =(const WrapArrayDeep& wad); private: //class variables for the dynamic character array and capacity of the array char* pch; int capacity; };
class WrapArrayShallow { public: //default constructor and destructor for WrapArrayShallow WrapArrayShallow(); ~WrapArrayShallow(); //functions to print and alter the array void printArray(); void alterArray(); //return the capacity of the array int getCapacity() { return capacity; } private: //class variables for the dynamic character array and capacity of the array char* pca; int capacity; };
void partOne(); void partTwo();
//function main int main() { //part 1 partOne();
//part 2 partTwo();
cout << endl; system("pause"); return 0; } // ends main
//part one of Chapter 10 assignment - pointers/dereference with simple data type void partOne() { //part 1 //int i and pointer to int pi int i = 7; int* pi; pi = &i;
//output for i and pi cout << "this program section uses 3 variables" << endl << "i = " << i << ", pi a pointer to i and ppi a pointer to pi" << endl << " pi = " << pi << endl << "dereference pi = " << *pi << endl << "address of pi = " << &pi << endl << "address of i = " << &i << endl;
//create pointer to pointer ppi int** ppi; ppi = π
//output including ppi cout << " ppi = " << ppi << endl << "deference of ppi = " << *ppi << endl << "address of ppi = " << &ppi << endl << "double dereference of ppi " << endl << **ppi << endl << endl; } //ends partOne
//part two of Chapter 10 assignment - array wrapping/copy constructor/destructors void partTwo() { //part two header cout << "This section instantiates a wrapper class for a dynamic array of 5" << " elements" << endl;
//declare and create wraparraydeep1 using default constructor and print WrapArrayDeep wad1, * wad2; cout << "WrapArrayDeep 1" << endl; wad1.printArray();
//create wraparraydeep2 using copy constructor and print wad2 = new WrapArrayDeep(wad1); cout << "WrapArrayDeep 2 using the copy constructor on 1" << endl; wad2->printArray();
//alter wad1 and print wad1.alterArray(); cout << "after changing the contents of WrapArrayDeep 1, 1 and 3 = " << endl; wad1.printArray(); wad2->printArray();
//declare and create WrapArrayShallow1 using default constructor and print WrapArrayShallow was1, * was2; cout << endl << "Now doing the same thing with WrapArrayShallow" << endl << endl << "WrapArrayShallow 1" << endl; was1.printArray();
//create was2 using the unwritten copy constructor for WrapArrayShallow and print was2 = new WrapArrayShallow(was1); cout << "WrapArrayShallow 2 created using the copy constructor on 1" << endl; was2->printArray();
//alter the array of was1 and print both arrays was1.alterArray(); cout << "after changing the contents of WrapArrayShallowc 1, 1 and 3 = " << endl; was1.printArray(); was2->printArray(); cout << endl;
//for some reason the destructor for the pointer versions of the classes is not being //called at the end of the partTwo function. So I am manually deleting the pointers //for wad2 and was2. wad1 and wad2 will call destructor automatically once //the partTwo function finishes delete wad2; //delete was2; - the delete call to was2 sometimes causes the program to crash } //ends partTwo
//member functions for WrapArray Deep
WrapArrayDeep::WrapArrayDeep() { //create char array capacity = 5; pch = new char[capacity];
//initialize char array using pointer arithmetic for (int i = 0; i < capacity; i++) *(pch + i) = (97 + i); } //ends WrapArrayDeep constructor
WrapArrayDeep::WrapArrayDeep(const WrapArrayDeep& wad) :capacity(wad.getCapacity()) { //allocate memory for the character array pch = new char[capacity];
//copy char array using pointer arithmetic for (int i = 0; i < capacity; i++) *(pch + i) = wad.pch[i]; } //ends WrapArrayDeep copy constructor
WrapArrayDeep::~WrapArrayDeep() { cout << "calling destructor for WrapArrayDeep "; //return memory used by the char array delete[] pch; } //ends WrapArrayDeep destructor
void WrapArrayDeep::printArray() { //print contents of array using array notation for (int i = 0; i < capacity; i++) cout << pch[i] << " ";
cout << endl; } //ends printArray
void WrapArrayDeep::alterArray() { //alter array contents for (int i = 0; i < capacity; i++) *(pch + i) = (123 + i); } //ends alterArray
WrapArrayDeep& WrapArrayDeep::operator =(const WrapArrayDeep& wad) { if (capacity != wad.capacity) { delete[] pch; pch = new char[wad.capacity]; } //ends if
capacity = wad.capacity; for (int i = 0; i < capacity; i++) pch[i] = wad.pch[i];
return *this; } //ends operator =
//member functions for WrapArrayShallow
WrapArrayShallow::WrapArrayShallow() { //create char array capacity = 5; pca = new char[capacity];
//initialize char array using array notation for (int i = 0; i < capacity; i++) pca[i] = (97 + i); } //ends WrapArrayShallow constructor
WrapArrayShallow::~WrapArrayShallow() { cout << "calling destructor for WrapArrayShallow "; //return memory used by the char array delete[] pca; } //ends WrapArrayShallow destructor
void WrapArrayShallow::printArray() { //print contents of array using array notation for (int i = 0; i < capacity; i++) cout << *(pca + i) << " ";
cout << endl; } //ends printArray
void WrapArrayShallow::alterArray() { //alter array contents for (int i = 0; i < capacity; i++) pca[i] = (123 + i); } //ends alterArray
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
