Step 1: #include <iostream> using namespace std; const int SIZE=5; class ARRAY_C
ID: 3559179 • Letter: S
Question
Step 1:
#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 << endl;
cout << "A holds address location = " << A.Get_Address();
cout << " and B holds address location " << B.Get_Address() << endl;
cout << 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 in Step 1, what type of copy was implemented: deep or shallow.? Explain your answer.
Question 2:Was a copy constructor implemented in the program in Step 1? Explain your answer.
Explanation / Answer
#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 << endl;
cout << "A holds address location = " << A.Get_Address();
cout << " and B holds address location " << B.Get_Address() << endl;
cout << 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 in Step 1, what type of copy was implemented: deep or shallow.? Explain your answer.
since class has pointer and no copy constructor is implemented ..it will do shallow copy menas two objects refer to same set of elements.
/* OUTPUT
Address of A and B are SAME !!!
*/
Question 2:Was a copy constructor implemented in the program in Step 1? Explain your answer.
No Copy constructor was not implemented and compiler using default copy constructor to copy the data.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.