#include <iostream> #include <cassert> #include \"pointerDataClass.h\" using nam
ID: 3614633 • Letter: #
Question
#include <iostream>
#include <cassert>
#include "pointerDataClass.h"
using namespace std;
void pointerDataClass::print() const
{
cout<<"x = "<<x<<endl;
cout<<"p = ";
for(inti = 0; i < lenP; i++)
cout<<p[i]<<"";
cout<<endl;
}
void pointerDataClass::setData()
{
cout<<"Enter an integer for x:";
cin>>x;
cout<<endl;
cout<<"Enter"<<lenP<<" numbers:";
for(inti = 0; i < lenP; i++)
cin>>p[i];
cout<<endl;
}
void pointerDataClass::destroyP()
{
lenP = 0;
delete [] p;
p = NULL;
}
pointerDataClass::pointerDataClass(intsizeP)
{
x = 0;
if(sizeP <= 0)
{
cout<<"Array size must bepositive"<<endl;
cout<<"Creating an array of size10"<<endl;
lenP = 10;
}
else
lenP = sizeP;
p = new int[lenP];
assert(p != NULL);
}
pointerDataClass::~pointerDataClass()
{
delete [] p;
}
//copy constructor
pointerDataClass::pointerDataClass
(const pointerDataClass& otherObject)
{
x = otherObject.x;
lenP = otherObject.lenP;
p = new int[lenP];
assert(p != NULL);
for(inti = 0; i < lenP; i++)
p[i] = otherObject.p[i];
}
Explanation / Answer
#include <iostream>
#include "pointerDataClass.h"
using namespace std;
void testCopyConst(pointerDataClass temp);
int main()
{
pointerDataClass one(5); //Line1
one.setData(); //Line 2
cout<<"Line 3: ###Object one'sdata###"<<endl;//Line 3
one.print(); //Line 4
cout<<"Line5:______________________________"
<<"______________"<<endl; //Line 5
pointerDataClass two(one); //Line6
cout<<"Line 7: ^^^Object two'sdata^^^"<<endl;//Line 7
two.print(); //Line 8
cout<<"Line9:_______________________________"
<<"_____________"<<endl; //Line 9
two.destroyP(); //Line 10
cout<<"Line 11: ~~~ Object one's dataafter "
<<"destroying object two.p~~~"<<endl; //Line11
one.print(); //Line 12
cout<<"Line13:_______________________________"
<<"_____________"<<endl; //Line 13
cout<<"Line 14: Calling the functiontestCopyConst"
<<endl; //Line 14
testCopyConst(one); //Line 15
cout<<"Line16:_______________________________"
<<"_____________"<<endl; //Line 16
cout<<"Line 17: After a call to thefunction "
<<"testCopyConst, object oneis:"<<endl; //Line17
one.print(); //Line 18
return 0; //Line19
}
void testCopyConst(pointerDataClass temp)
{
cout<<"Line 20: *** Inside function"
<<"testCopyConst***"<<endl; //Line20
cout<<"Line 21: Object tempdata:"<<endl;//Line 21
temp.print(); //Line 22
temp.setData(); //Line 23
cout<<"Line 24: After changing theobject "
<<"temp, its data is:"<<endl; //Line24
temp.print(); //Line 25
cout<<"Line 26: *** Exiting function"
<<"testCopyConst***"<<endl; //Line26
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.