Step 1: In this experiment you will investigate how an object is copied. Enter,
ID: 3684467 • Letter: S
Question
Step 1: In this experiment you will investigate how an object is copied.
Enter, save, compile and execute the following program in MSVS. Call the new project “CopyConstructorExp1” and the program “CopyConstructor1.cpp”. Answer the questions below:
#include <iostream>
using namespace std;
const int SIZE=5;
class ARRAY_CLASS
{
public:
ARRAY_CLASS();//default constructor
void Add(int); //mutator
void Print(); //accessor
int * Get_Address(); //accessor
private:
int *A;
int count;
};
ARRAY_CLASS::ARRAY_CLASS()
{
cout<<"Default Constructor has been Called! ";
A = new int[SIZE];
count = 0;
}
void ARRAY_CLASS::Add(int item)
{
if (count<SIZE)
A[count++]=item;
else
cout<<"Array Full ";
}
void ARRAY_CLASS::Print()
{
for(int i=0; i<count; i++)
cout<<"A[i] = "<<A[i]<<endl;
}
int * ARRAY_CLASS::Get_Address()
{
return A;
}
int main()
{
ARRAY_CLASS B;
B.Add(1);
B.Add(2);
B.Add(3);
B.Add(4);
B.Add(5);
B.Print();
cout<<endl<<endl;
ARRAY_CLASS A = B;
A.Print();
cout<<"A holds address location = "<<A.Get_Address()
<<" and B holds address location "<<B.Get_Address()<<endl;
return 0;
}
Question 1:Referring to the declaration/initialization statement and the output of the last cout
statement in the main function of the program of Step 1, what type of copy was implementated, deep or shallow.? Explain your answer.
Question 2:Was a copy constructor implemented in the program in Step 1? Explain you answer.
Step 2: In this experiment you will investigate how a copy constructor is implemented.
Enter, save, compile and execute the following program in MSVS. Call the new project “CopyConstructorExp2” and the program “CopyConstructor2.cpp”. Answer the questions below:
#include <iostream>
using namespace std;
const int SIZE=5;
class ARRAY_CLASS
{
public:
ARRAY_CLASS();//default constructor
ARRAY_CLASS(const ARRAY_CLASS &);//copy constructor
~ARRAY_CLASS(); //destructor
void Add(int); //mutator
void Print(); //accessor
int * Get_Address(); //accessor
private:
int *A;
int count;
};
ARRAY_CLASS::ARRAY_CLASS()
{
cout<<"The Default Constructor has been Called! ";
A = new int[SIZE];
count = 0;
}
ARRAY_CLASS::ARRAY_CLASS(const ARRAY_CLASS & Org)
{
cout<<"The Copy Constructor has been Called! ";
count = Org.count;
A = new int[SIZE];
for(int i=0; i<count; i++)
{
A[i] = Org.A[i];
}
}
ARRAY_CLASS::~ARRAY_CLASS()
{
cout<<"The Destructor has been Called! ";
delete [ ] A;
A=0;
count = 0;
}
void ARRAY_CLASS::Add(int item)
{
if (count<SIZE)
A[count++]=item;
else
cout<<"Array Full ";
}
void ARRAY_CLASS::Print()
{
for(int i=0; i<count; i++)
cout<<"A[i] = "<<A[i]<<endl;
}
int * ARRAY_CLASS::Get_Address()
{
return A;
}
int main()
{
ARRAY_CLASS B;
B.Add(1);
B.Add(2);
B.Add(3);
B.Add(4);
B.Add(5);
B.Print();
ARRAY_CLASS A = B;
A.Print();
cout<<"A holds address location = "<<A.Get_Address()
<<" and B holds address location "<<B.Get_Address()<<endl;
return 0;
}
Question 3:Referring to the declaration/initialization statement and the output of the last cout statement in the program of Step 2, what type of copy was implemented? Explain your answer.
Question 4:Why was a copy constructor implemented in the program in Step 2?
Question 5:What is the purpose of a destructor, and why was one implemented in the program in Step 2.
Step 3: In this experiment you will investigate a program that allocates and de-allocates the dynamic memory used by a class. Enter, save, compile and execute the following program in MSVS. Call the new project “CopyConstructorExp3” and the program “CopyConstructor3.cpp”. Answer the questions below:
#include <iostream>
using namespace std;
const int SIZE=5;
class ARRAY_CLASS
{
public:
ARRAY_CLASS();//default constructor
~ARRAY_CLASS(); //destructor
void Add(int); //mutator
void Print(); //accessor
int * Get_Address(); //accessor
void DeAllocate(); //mutator
private:
int *A;
int count;
};
ARRAY_CLASS::ARRAY_CLASS()
{
cout<<"Default constructor has been called ";
A = new int[SIZE];
count = 0;
}
ARRAY_CLASS::~ARRAY_CLASS()
{
cout<<"The Destructor has been Called! ";
delete [ ] A;
A=0;
count = 0;
}
void ARRAY_CLASS::Add(int item)
{
if (count<SIZE)
A[count++]=item;
else
cout<<"Array Full ";
}
void ARRAY_CLASS::Print()
{
for(int i=0; i<count; i++)
cout<<"A[i] = "<<A[i]<<endl;
}
int * ARRAY_CLASS::Get_Address()
{
return A;
}
void ARRAY_CLASS::DeAllocate()
{
delete [ ] A;
A = 0;
count = 0;
}
int main()
{
ARRAY_CLASS B;
B.Add(1);
B.Add(2);
B.Add(3);
B.Add(4);
B.Add(5);
B.Print();
ARRAY_CLASS A = B;
cout<<"A holds address location = "<<A.Get_Address()
<<" and B holds address location "<<B.Get_Address()<<endl;
B.DeAllocate();
A.Print();
return 0;
}
Question 6:What is the purpose of the DeAllocate function in the program of Step 3?
Question 7:What is the difference between the destrunctor and the DeAllocate functions?
Question 8:Does the program in Step 3 execute with errors? If so, name and explain the
type(s) of execution error(s) you observed.
Explanation / Answer
1;Copy used is shallow...because they didn't include another program
2:Copy constructor is included in step 1 because in construtor class name and object name is same...
3: Copy included is deep....
4:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.