Click to See Complete Forum and Search --> : Class member function using calling it's own array?
sjaguar13
October 18th, 2002, 11:06 PM
Is it possible to have a class, ArraySet; a member fucntion, AddNumbers; and private data int Set[101], and passing in two ArraySets into a third ArraySet.AddNumbers. It should be able to use the Set array, but how would I do it, ArraySet.Set[X]?
JamesSchumacher
October 19th, 2002, 01:30 AM
You either want to set the index 'X' of the third object's array variable with the sum of the 1st and 2nd objects at index 'X' of their respective arrays, or the sum of those and the current value of 'X' in the third objects array.
You *could* use a member function that sets the variable at index 'X' of the third array - but you don't have to.
Even though the member is private - objects of the same class can directly access eachother's members.
sjaguar13
October 19th, 2002, 01:01 PM
Yeah, I want to add the first two arrays and put the sum in the third, but I don't know how to call the first two arrays. If I pass them into a function as A and B, how would I get A and B's private data?
galathaea
October 19th, 2002, 06:02 PM
There should be a class member that allows you to obtain elements of the data, something like operator[] or GetMember(). Also, if you want to be able to write some thing like set1 = set2 + set3, I might suggest adding your own operator+ function as well. It's easy and useful.
JamesSchumacher
October 19th, 2002, 09:52 PM
Originally posted by sjaguar13
Yeah, I want to add the first two arrays and put the sum in the third, but I don't know how to call the first two arrays. If I pass them into a function as A and B, how would I get A and B's private data?
The other poster gave a good way to do things, with overloading operators. As for answering your questions about accessing the private data - if you pass A and B to a member function of the class (object A, B, and C are all the same type right?) then the C object can access A and B's member variables directly. Here is a simple example of that.
class Example
{
public:
Example() throw()
{
// zero array data
for (unsigned long x = 0; x < sizeof(m_pArray) / sizeof(unsigned long); ++x)
{
m_pArray[x] = 0;
}
}
Example(const Example & ex) throw()
{
// initialize our array with the data of ex.m_pArray
for (unsigned long x = 0; x < sizeof(m_pArray) / sizeof(unsigned long); ++x)
{
m_pArray[x] = ex.m_pArray[x]; // directly accessing a brother or sister object's members
}
}
~Example() throw()
{
}
const Example & operator = (const Example & ex) throw()
{
for (unsigned long x = 0; x < sizeof(m_pArray) / sizeof(unsigned long); ++x)
{
m_pArray[x] = ex.m_pArray[x]; // access directly
}
return *this;
}
Example & operator += (const Example & ex) throw()
{
for (unsigned long x = 0; x < sizeof(m_pArray) / sizeof(unsigned long); ++x)
{
m_pArray[x] += ex.m_pArray[x]; // direct access
}
return *this;
}
Example operator + (const Example & ex) const throw()
{
Example temp(*this);
temp += ex;
return temp;
}
private:
unsigned long m_pArray[20];
};
The 'private' declaration, means that only functions within this class can access those members directly. (Even a static member function can access those members of an object if given a pointer or reference to one.)
sjaguar13
October 19th, 2002, 11:22 PM
I'm doing something wrong. I get 21 errors and 4 warnings.
It says contructor not allowed a return type
IntegerSet::IntegerSet()
{
for(int x=0; x<101; x++)
{
m_Set[x] = 0;
}
}
I get an overload error with the other consructor:
IntegerSet::IntegerSet(int x1, int x2, int x3, int x4, int x5)
{
if(x1 != -1)
{
Set[x1] = 1;
}
if(x2 != -1)
{
Set[x2] = 1;
}
if(x3 != -1)
{
Set[x3] = 1;
}
if(x4 != -1)
{
Set[x4] = 1;
}
if(x5 != -1)
{
Set[x5] = 1;
}
}
Here's a member funtion that uses to other variables of the same class:
void unionOfIntegerSets(IntegerSet A, IntegerSet B)
{
for(int x=0; x<101; x++)
{
if(A.m_Set[x] == 1 || B.m_Set[x] == 1)
{
m_Set[x] = 1;
}
else
{
m_Set[x] = 0;
}
}
}
I get something about a pointer with that function.
The other errors are pretty much the same thing with the other functions. Should I call in Integer * A, and Integer * B?
JamesSchumacher
October 20th, 2002, 11:49 AM
You could use a pointer, but you could also use a reference. You are passing the objects by value. (I'm guessing you don't have a copy constructor defined, and maybe that's why you get an error.) And since you are not changing A or B, should probably be declared const.
void unionOfIntegerSets(const IntegerSet & A,const IntegerSet & B)
{
for(int x=0; x<101; x++)
{
if(A.m_Set[x] == 1 || B.m_Set[x] == 1)
{
m_Set[x] = 1;
}
else
{
m_Set[x] = 0;
}
}
}
sjaguar13
October 20th, 2002, 02:50 PM
I tried the & thing, it didn't fix any errors, and it make any new ones.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.