Passing arrays of objects to functions

Please support our C++ advertiser: Intel Parallel Studio Home
Closed Thread

Join Date: Jan 2004
Posts: 7
Reputation: fishman is an unknown quantity at this point 
Solved Threads: 0
fishman fishman is offline Offline
Newbie Poster

Passing arrays of objects to functions

 
0
  #1
Jan 27th, 2004
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 <cstdlib>
#include <iostream>
#include <ctime>
#include "fishclass.hpp"
using namespace std;
int main ()
{
int n, i = 0;
srand(static_cast<unsigned>(time(0)));

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

Fish river[n]; //creating array of objects
for(int i=0;i<n;i++)
{
river[i].setWeight();
river[i].setAge();
river[i].setEggs();
}

for(int i=0;i<n;i++) //outputting info
{
river[i].getWeight();
river[i].getAge();
river[i].getEggs();
cout << "Fish " << i << " Weight " << river[i].weight;
cout << " Age " << river[i].age << " Eggs " << river[i].eggs << endl;
}
system ( "PAUSE" );
return 0;
}


So I have a class called fish, which have objects called river (ie these are fish in a river). the river objects have attributes called age, weight and eggs. 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.

Pls help a newby!
John G.
Quick reply to this message  
Join Date: Feb 2003
Posts: 129
Reputation: Bob is an unknown quantity at this point 
Solved Threads: 1
Team Colleague
Bob Bob is offline Offline
Team Member

Re: Passing arrays of objects to functions

 
0
  #2
Jan 31st, 2004
Originally Posted by fishman
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:

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class fish
  6. {
  7. public:
  8. fish();
  9. int getAge() {return age;}
  10. void setAge(int newage) { age = newage; }
  11. private:
  12. int age;
  13. };
  14.  
  15. fish::fish()
  16. {
  17. age = 0;
  18. }
  19.  
  20. void showAges(fish fishes[])
  21. {
  22. cout << "fish 1 age is: " << fishes[0].getAge() << endl;
  23. cout << "fish 2 age is: " << fishes[1].getAge() << endl;
  24. cout << "fish 3 age is: " << fishes[2].getAge() << endl;
  25. }
  26.  
  27. int main()
  28. {
  29. fish myFish[3];
  30.  
  31. showAges(myFish);
  32. cout << endl;
  33.  
  34. myFish[0].setAge(10);
  35. myFish[1].setAge(20);
  36. myFish[2].setAge(30);
  37.  
  38. showAges(myFish);
  39.  
  40. cin.get();
  41. return 0;
  42. }
Quick reply to this message  
Join Date: Jan 2004
Posts: 7
Reputation: fishman is an unknown quantity at this point 
Solved Threads: 0
fishman fishman is offline Offline
Newbie Poster

Re: Passing arrays of objects to functions

 
0
  #3
Feb 13th, 2004
Thankyou Bob, it all becomes so very clear when you have code like that to look at.
Cheers
Fishman
Quick reply to this message  
Join Date: Aug 2005
Posts: 1
Reputation: ajpeters is an unknown quantity at this point 
Solved Threads: 0
ajpeters ajpeters is offline Offline
Newbie Poster

Re: Passing arrays of objects to functions

 
0
  #4
Aug 1st, 2005
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?
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Passing arrays of objects to functions

 
0
  #5
Aug 1st, 2005
>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.
I'm here to prove you wrong.
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC