Question: #include #include #include #include using namespace std; / / Interface for SafeArray class / / - - - - - - - - - -
#include
#include
#include
#include
using namespace std;
Interface for SafeArray class
const int SIZE ;
class SafeArray
public:
SafeArray;
SafeArrayconst SafeArray & copy;
~SafeArray;
bool GetElementconst int i float & Value const;
bool SetElementconst int i float Value;
bool ReadArrayconst string Filename;
bool WriteArrayconst string Filename const;
void Print const;
private:
float ArraySIZE;
;
Implementation for SafeArray class
SafeArray::SafeArray
cout "construct
;
for int i ; i SIZE; i
Arrayi;
SafeArray::SafeArrayconst SafeArray & copy
cout "copy
;
for int i ; i SIZE; i
Arrayi copy.Arrayi;
SafeArray::~SafeArray
cout "destruct
;
bool SafeArray::GetElementconst int i float & Value const
bool Success false;
if i && i SIZE
Success true;
Value Arrayi;
return Success;
bool SafeArray::SetElementconst int i float Value
bool Success false;
if i && i SIZE
Success true;
Arrayi Value;
return Success;
bool SafeArray::ReadArrayconst string Filename
ifstream infile;
infile.openFilenamecstr;
if infilefail
return false;
for int i ; i SIZE; i
infile Arrayi;
infile.close;
return true;
bool SafeArray::WriteArrayconst string Filename const
ofstream outfile;
outfile.openFilenamecstr;
if outfilefail
return false;
for int i ; i SIZE; i
outfile Arrayi endl;
outfile.close;
return true;
void SafeArray::Print const
for int i ; i SIZE; i
cout "Array i Arrayi endl;
Program to demonstrate SafeArray class
int main
Step creating two SafeArray objects data data;
SafeArray data;
SafeArray data;
Step using the "GetElement" and "SetElement" methods.
for int i ; i ; i Step for loop is setting values from to but our setElement is taking care
bool check dataSetElementi i ;
ifcheck
cout "set i i endl;
Step calling print function on data object
dataPrint;
Step calling getElement and printing the values
for int i ; i ; i
float value;
bool check dataGetElementi value;
ifcheck
cout "get i value endl;
Step created a file datain
Step calling ReadArray and WriteArray ie reading values from datain and output the values to dataout and output screen.
dataReadArraydatain;reading values from datain
dataPrint;printing those values to output screen
dataWriteArraydataout";printing those values to dataout
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
