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 =20;
class SafeArray
{
public:
SafeArray();
SafeArray(const SafeArray & copy);
~SafeArray();
bool GetElement(const int i, float & Value) const;
bool SetElement(const int i, float Value);
bool ReadArray(const string Filename);
bool WriteArray(const string Filename) const;
void Print() const;
private:
float Array[SIZE];
};
// Implementation for SafeArray class
//-----------------------------------
SafeArray::SafeArray()
{
cout << "construct
";
for (int i =0; i < SIZE; i++)
Array[i]=42;
}
SafeArray::SafeArray(const SafeArray & copy)
{
cout << "copy
";
for (int i =0; i < SIZE; i++)
Array[i]= copy.Array[i];
}
SafeArray::~SafeArray()
{
cout << "destruct
";
}
bool SafeArray::GetElement(const int i, float & Value) const
{
bool Success = false;
if ((i >=0) && (i < SIZE))
{
Success = true;
Value = Array[i];
}
return Success;
}
bool SafeArray::SetElement(const int i, float Value)
{
bool Success = false;
if ((i >=0) && (i < SIZE))
{
Success = true;
Array[i]= Value;
}
return Success;
}
bool SafeArray::ReadArray(const string Filename)
{
ifstream infile;
infile.open(Filename.c_str());
if (infile.fail())
return false;
for (int i =0; i < SIZE; i++)
infile >> Array[i];
infile.close();
return true;
}
bool SafeArray::WriteArray(const string Filename) const
{
ofstream outfile;
outfile.open(Filename.c_str());
if (outfile.fail())
return false;
for (int i =0; i < SIZE; i++)
outfile << Array[i]<< endl;
outfile.close();
return true;
}
void SafeArray::Print() const
{
for (int i =0; i < SIZE; i++)
cout << "Array["<< i <<"]="<< Array[i]<< endl;
}
// Program to demonstrate SafeArray class
//---------------------------------------
int main()
{
// Step2- creating two SafeArray objects data1, data2;
SafeArray data1;
SafeArray data2;
// Step 4- using the "GetElement" and "SetElement" methods.
for (int i =-5; i <25; i++)// Step5- for loop is setting values from -5 to 24 but our setElement is taking care
{
bool check = data1.SetElement(i, i *0.1);
if(check)
cout << "set "<< i <<""<< i *0.1<< endl;
}
//Step3- calling print() function on data1 object
data1.Print();
//Step7- calling getElement and printing the values
for (int i =-5; i <25; i++)//
{
float value=0;
bool check = data1.GetElement(i, value);
if(check)
cout << "get "<< i <<""<< value << endl;
}
// Step8- created a file data2.in
//Step 9-calling ReadArray and WriteArray i.e reading values from data2.in and output the values to data2.out and output screen.
data2.ReadArray("data2.in");//reading values from data2.in
data2.Print();//printing those values to output screen
data2.WriteArray("data2.out");//printing those values to data2.out
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!