I'm working on a project in my programming class that you're supposed to make a .h and .cpp file that you can set values of an array, get values of an array, print the array, add the array to another object, and subtract the array from another object.
We're supposed to provide some cout in the class methods to show the teacher what the method is doing. like the in the set function saying 'cout << "Setting the " << place << "to " << x << " value." << endl;'
What I'm wondering is can you access the object name from the class methods so that I can say 'cout << "adding " << firstobjectname << " array to << secondobjectname << ". " << endl;
#include "safearray.h"
#include <iostream>
usingnamespace std;
SafeArray::SafeArray()
{
size = 0;
}
SafeArray::SafeArray(SafeArray &x)
{
for (int index = 0; index < 100; index++)
{
array[index] = x.array[index];
}
size=x.size;
}
SafeArray::~SafeArray()
{
}
void SafeArray::SetArray(int place, int value)
{
if (place < 0 || place > 99)
{
cout << "SetArray error" << endl;
return;
}
array[place] = value;
if(place >= size)
{
size = place + 1;
}
cout << "Setting the " << place << " place in the array to " << value << ". " << endl;
}
int SafeArray::GetArray(int x)
{
if (x < 0 || x > size-1)
{
cout << "SetArray error" << endl;
return 0;
}
cout << "Getting the value in the " << x << " place." << endl;
return array[x];
}
void SafeArray::PrintArray()
{
for (int index = 0; index < size; index++)
{
if (index == size - 1)
{
cout << array[index] << ". ";
}
else
cout << array[index] << ", ";
}
cout << endl;
}
void SafeArray::AddArray(const SafeArray s)
{
if(size!=s.size)
{
cout << "sizes not equal. " << endl;
return;
}
for (int count = 0; count < size; count++)
{
array[count] = array[count] + s.array[count];
}
}
void SafeArray::SubArray(const SafeArray s)
{
if(size!=s.size)
{
cout << "sizes not equal. " << endl;
return;
}
for (int count = 0; count < size; count++)
{
array[count] = array[count] - s.array[count];
}
}
I'm not sure if I'm being clear enough but basically can you add code in the classes (without having to pass through the class name as a str or char in the parameters of the method) to be able to cout some sort of confirmation message about what's happening using the object names.
I think we're talking about different things. I know how to print out the array. But when you create an object in main, how you can access the name of that object in the classes.
For example, if I create SafeArray s object, how can I print out something that says
"Printing out the s array." without hard coding the 's' into the PrintArray function.
Sorry for the lack of clarity. Thanks for the reply!