Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Chapter 1 0 Pointers In C + + PleaseExample Output: this program section uses 3 variables i = 7 , pi a pointer to i
Chapter Pointers
In C PleaseExample Output:
this program section uses variables
pi a pointer to i and pri a pointer to pi
dereference pi
address of pi EF
address of EF
pri
dereference of pRi EF
address of pRi, EFC
double dereference of pRi
this section instantiates a wrapper class for a dynamic array of elements
WrapArrayDeep
a b c d e
WrapArrayDeep created using the copy constructor on
a b c d e
after changing the contents of WrapArrayDeep and
a b c d e
Now doing the same thing with WrapArrayShallow
WrapArrayShallow
a b c d e
wrapArrayShallow created using the copy constructor on
a b c d e
after changing the contents of WrapArrayShallow and
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
Part
Create an int i with a value of 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
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 objects, which should have independent memory storage, accidently wind up sharing memory.
I want you to wrap a character array and array with abcde is strictly speaking not a string since it does not end in with a class this is just a class that contains an array and then properlyin 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 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;
pch ; etc
WrapArrayDeepWrapArrayDeep 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 wadwad;
for the variables holding your WrapArrayDeeps and for WrapArrayShallow:
WrapArrayShallow waswas;
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 ;
pca;
etc.
Use array notation to print your array.
forint I ; I ; I
cout pcai endl;
In WrapArrayShallow:
Use array notation to load your array with char data.
pcav;
pcaw;
etc
Use pointer arithmetic to print your array.
forint I ; I ; i
cout pca endl;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started