954,180 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Passing arrays of objects to functions

Hi
I am trying to teach myself C++ coding but am having a problem with classes and objects. I need to pass my objects to a function which may use various object attributes in calculations but may also change the original values of these attributes. My code is:

#include
#include
#include
#include "fishclass.hpp"
using namespace std;
int main ()
{
int n, i = 0;
srand(static_cast(time(0)));

cout << "How many fish in population? ";
cin >> n;

Fish river[n]; //creating array of objects
for(int i=0;i

fishman
Newbie Poster
7 posts since Jan 2004
Reputation Points: 10
Solved Threads: 0
 
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;
}
Bob
Junior Poster
Team Colleague
129 posts since Feb 2003
Reputation Points: 15
Solved Threads: 2
 

Thankyou Bob, it all becomes so very clear when you have code like that to look at.
Cheers
Fishman

fishman
Newbie Poster
7 posts since Jan 2004
Reputation Points: 10
Solved Threads: 0
 

What if I did not know the size of the array before I started. Say i have
cout << "" How Many fish \n";
cin >>fishcoutn;
fish myFish[fishcoutn];

showAges(myFish);
cout << endl;
etc. etc etc.

How would it change the code?

ajpeters
Newbie Poster
1 post since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

>How would it change the code?
First, you would start a new thread instead of resurrecting a thread that's over a year old. Then you would use a std::vector object because arrays are error prone and difficult to use correctly.

Thread locked to avoid further bumping.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You