Step 2: . #include <iostream> using namespace std; const int SIZE=5; class ARRAY
ID: 3559180 • Letter: S
Question
Step 2: .
#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 << endl;
cout << "A holds address location = " << A.Get_Address();
cout << " and B holds address location " << B.Get_Address() << endl;
cout << endl;
return 0;
}
Question 3: Referring to the declaration/initialization statement and the output of the last cout statement in the main() function of the program in Step 2, what type of copy was implemented? Shallow or deep? 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.
Explanation / Answer
#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 << endl;
cout << "A holds address location = " << A.Get_Address();
cout << " and B holds address location " << B.Get_Address() << endl;
cout << endl;
return 0;
}
Question 3: Referring to the declaration/initialization statement and the output of the last cout statement in the main()
function of the program in Step 2, what type of copy was implemented? Shallow or deep? Explain your answer.
since explicit copy constructor was implemented ..it will do deep copy..so two objects will have different set of elements.
/* OUTPUT
*/
Two OBJECTS HAVE DIFFERENT ADDRESSES
Question 4:Why was a copy constructor implemented in the program in Step 2?
since class has pointer variable, with out copy constructor it will do shall copy instead of deep copy.
so it is necessary to implement copy constructor.
Question 5: What is the purpose of a destructor, and why was one implemented in the program in Step 2
since class has pointer variable, memory allocated by pointer should be de allocated once program completes.
for which destructor is requried.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.