| | |
Passing arrays of objects to functions
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2004
Posts: 7
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Feb 2003
Posts: 129
Reputation:
Solved Threads: 1
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
#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; }
>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.
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.
New members chased away this month: 4
![]() |
Similar Threads
- Problem when passing arrays from c# to matlab (C#)
- Help: Passing arrays between functions (C)
- Passing Arrays of Objects to Member Functions (C++)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Help with dice game!
- Next Thread: Error in compiling matrix
Views: 14519 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





