| | |
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; }
![]() |
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
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





