943,771 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 19040
  • C++ RSS
Jan 27th, 2004
0

Passing arrays of objects to functions

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fishman is offline Offline
7 posts
since Jan 2004
Jan 31st, 2004
0

Re: Passing arrays of objects to functions

Quote 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:

C++ Syntax (Toggle Plain Text)
  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. }
Bob
Team Colleague
Reputation Points: 15
Solved Threads: 2
Junior Poster
Bob is offline Offline
129 posts
since Feb 2003
Feb 13th, 2004
0

Re: Passing arrays of objects to functions

Thankyou Bob, it all becomes so very clear when you have code like that to look at.
Cheers
Fishman
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fishman is offline Offline
7 posts
since Jan 2004
Aug 1st, 2005
0

Re: Passing arrays of objects to functions

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ajpeters is offline Offline
1 posts
since Aug 2005
Aug 1st, 2005
0

Re: Passing arrays of objects to functions

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: Help with dice game!
Next Thread in C++ Forum Timeline: Error in compiling matrix





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC