By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

way to address objects in for loop

P: n/a
I have defined and declared 3 Cat objects which want to display
information on each via a for loop. I have a value n, which
increments, but I am not sure how I would change the call to each Cats
methods so that the value n replaces the 1 for example:

myCatn.GetAge() >> where n gets replaced.

Ive thought about adding a Display() method for cat but then i will
still find myself calling display on each cat in the loop.

Maybe someone can sugest a way to do this easyer.

Code:

Cat myCat1; // RagDoll, 1 year old, weighs 6 lbs
Cat myCat2(Persian, 2); // Persian, 2 years old, weighs 6 lbs
Cat myCat3(RagDoll, 5, 10); // RagDoll, 5 years old, weighs 10 lbs;

for (int n = 1; n < 4; n++)
{
cout << "Cat: myCat" << n << "\n\n";
cout << "Breed: " << myCat1.GetBreed() << endl;
cout << "Age: " << myCat1.GetAge() << endl;
cout << "Weight: " << myCat1.GetWeight() << endl;
}

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Kelly Mandrake wrote:
I have defined and declared 3 Cat objects which want to display
information on each via a for loop. I have a value n, which
increments, but I am not sure how I would change the call to each Cats
methods so that the value n replaces the 1 for example:

myCatn.GetAge() >> where n gets replaced.

Ive thought about adding a Display() method for cat but then i will
still find myself calling display on each cat in the loop.

Maybe someone can sugest a way to do this easyer.

Code:

Cat myCat1; // RagDoll, 1 year old, weighs 6 lbs
Cat myCat2(Persian, 2); // Persian, 2 years old, weighs 6 lbs
Cat myCat3(RagDoll, 5, 10); // RagDoll, 5 years old, weighs 10 lbs;

for (int n = 1; n < 4; n++)
{
cout << "Cat: myCat" << n << "\n\n";
cout << "Breed: " << myCat1.GetBreed() << endl;
cout << "Age: " << myCat1.GetAge() << endl;
cout << "Weight: " << myCat1.GetWeight() << endl;
}


Place addresses of your cats in an array of pointers to Cat objects,
and then loop through the array elements. For every element of the array
use -> to access the member you need to call.

V
Jul 23 '05 #2

P: n/a
"Kelly Mandrake" <at******@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
I have defined and declared 3 Cat objects which want to display
information on each via a for loop. I have a value n, which
increments, but I am not sure how I would change the call to each Cats
methods so that the value n replaces the 1 for example:

myCatn.GetAge() >> where n gets replaced.

Ive thought about adding a Display() method for cat but then i will
still find myself calling display on each cat in the loop.

Maybe someone can sugest a way to do this easyer.

Code:

Cat myCat1; // RagDoll, 1 year old, weighs 6 lbs
Cat myCat2(Persian, 2); // Persian, 2 years old, weighs 6 lbs
Cat myCat3(RagDoll, 5, 10); // RagDoll, 5 years old, weighs 10 lbs;

for (int n = 1; n < 4; n++)
{
cout << "Cat: myCat" << n << "\n\n";
cout << "Breed: " << myCat1.GetBreed() << endl;
cout << "Age: " << myCat1.GetAge() << endl;
cout << "Weight: " << myCat1.GetWeight() << endl;
}


Using a loop to change a name, and therefore refer to a different object,
cannot be done. You have to use some sort of collection of Cats or Cat
pointers. You can replace the three variable names you have now with an
array or vector of Cats, or you can keep them and place their addresses in
an array or vector, e.g.,

#include <iostream>
#include <ostream>

using namespace std;

int main()
{
Cat myCat1;
Cat myCat2(Persian, 2);
Cat myCat3(RagDoll, 5, 10);
Cat *cats[] = {&myCat1, &myCat2, &myCat3};
for(int n = 0; n < 3; ++n)
{
cout << "Cat: " << n+1 << endl;
cout << "Breed: " << cats[n]->GetBreed() << endl;
cout << "Age: " << cats[n]->GetAge() << endl;
cout << "Weight: " << cats[n]->GetWeight() << endl;
}
}

Note that arrays begin indexing from zero, not 1.

DW
Jul 23 '05 #3

P: n/a
Thanks very much for the help everyone. The solution worked and now I
will need to go read about vectors now that you used a new word.

Jul 23 '05 #4

P: n/a
Kelly Mandrake wrote:
I have defined and declared 3 Cat objects which want to display
information on each via a for loop. I have a value n, which
increments, but I am not sure how I would change the call to each Cats methods so that the value n replaces the 1 for example:

myCatn.GetAge() >> where n gets replaced.


You could use a container. A container, although having slightly
more complicated syntax to initailize it, manages memory safely
and can be re-sized. An array has fixed size and it is easy to
run off the end of it by accident.
Having a display function is also a great idea. Here is an example:

.. void display(Cat const &cat)
.. {
.. cout << "Breed: " << cat.GetBreed() << endl;
.. cout << "Age: " << cat.GetAge() << endl;
.. cout << "Weight: " << cat.GetWeight() << endl;
.. }
..
.. std::vector<Cat> myCats;
.. myCats.push_back( Cat() );
.. myCats.push_back( Cat(Persian, 2) );
.. myCats.push_back( Cat(RagDoll, 5, 10) );
..
.. for (int n = 0; n != myCats.size(); ++n)
.. {
.. cout << "Cat: myCat" << n << "\n\n";
.. display(myCats[n]);
.. }

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.