Step 3: #include <iostream> using namespace std; const int SIZE=5; class ARRAY_C
ID: 3559181 • Letter: S
Question
Step 3:
#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()
{
Deallocate();
}
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 << endl;
cout << "A holds address location = " << A.Get_Address();
cout << " and B holds address location " << B.Get_Address() << endl;
cout << 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 destructor and theDeAllocate() 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
#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()
{
Deallocate();
}
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 << endl;
cout << "A holds address location = " << A.Get_Address();
cout << " and B holds address location " << B.Get_Address() << endl;
cout << endl;
B.DeAllocate();
A.Print();
return 0;
}
Question 6:What is the purpose of the DeAllocate() function in the program of Step 3?
memory allocated by pointer should be deallocated once objects no where in use. because of which we need deallocate call.
Question 7:What is the difference between the destructor and theDeAllocate() functions?
Both will do the same thing...because destructor should de-allocate the memory. and in turn destrcutor is calling deallocate funtion inside it.
destructor will be called automatically by compiler again we are not supposed to call deallocate inside destructor...or we should not call DeAllocate on object. because again destructor will be called by compiler which in turn call DeAllocate.
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.
first error is casesensitive of method name it should be DeAllocate not Deallocate.
second since no copy costructor is declared.
it will do shallow copy of object elements.
since we deallocated memory for one object i.e B (means A also deallocated this time).
again we are trying to deallocate memory for object A which was already de-allocated as part of B destruction...gives a error saying that
*** glibc detected *** demo: double free or corruption (fasttop)
CAUSE IS TRYING TO DEALLOCATE MEMORY THAT WAS ALREADY DEALLOCATED AS PART OF ANOTHER OBJECT DESTRUCTION.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.