I need to pass the whole array of objects to a function where I can play with the attribute values or change them if necessary. I have been playing with pointers and references but can not get them to work with an array of objects like I have.
Just pass the array of objects to the function by passing the array name as a paramater. Just like passing any other array. You can then access the objects in the array in the same way you would any array element. For example, not using your code exactly but a simplified example:
#include <iostream>
using namespace std;
class fish
{
public:
fish();
int getAge() {return age;}
void setAge(int newage) { age = newage; }
private:
int age;
};
fish::fish()
{
age = 0;
}
void showAges(fish fishes[])
{
cout << "fish 1 age is: " << fishes[0].getAge() << endl;
cout << "fish 2 age is: " << fishes[1].getAge() << endl;
cout << "fish 3 age is: " << fishes[2].getAge() << endl;
}
int main()
{
fish myFish[3];
showAges(myFish);
cout << endl;
myFish[0].setAge(10);
myFish[1].setAge(20);
myFish[2].setAge(30);
showAges(myFish);
cin.get();
return 0;
}