I don't understand how it works. What are lines 19-33 doing exactly? Why did they do "arraySize = obj.arraySize;" ?
#include <iostream>
#include <iomanip>
using namespace std;
class NumberArray
{
private:
double *aPtr;
int arraySize;
public:
NumberArray(NumberArray &);
NumberArray(int size, double value);
~NumberArray() { if (arraySize > 0) delete [] aPtr; }
void print();
void setValue(double value);
};
NumberArray::NumberArray(NumberArray &obj)
{
arraySize = obj.arraySize;
aPtr = new double[arraySize];
for(int index = 0; index < arraySize; index++)
aPtr[index] = obj.aPtr[index];
}
NumberArray::NumberArray(int size, double value)
{
arraySize = size;
aPtr = new double[arraySize];
setValue(value);
}
void NumberArray::setValue(double value)
{
for(int index = 0; index < arraySize; index++)
aPtr[index] = value;
}
void NumberArray::print()
{
for(int index = 0; index < arraySize; index++)
cout << aPtr[index] << " ";
}
int main()
{
NumberArray first(3, 10.5);
NumberArray second = first;
cout << setprecision(2) << fixed << showpoint;
cout << "Value stored in first object is ";
first.print();
cout << "\nValue stored in second object is ";
second.print();
cout << "\nOnly the value in second object will "
<< "be changed.\n";
second.setValue(21.5);
cout << "Value stored in first object is ";
first.print();
cout << endl << "Value stored in second object is ";
second.print();
return 0;
}