way to address objects in for loop

K

Kelly Mandrake

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;
}
 
V

Victor Bazarov

Kelly said:
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
 
D

David White

Kelly Mandrake said:
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
 
K

Kelly Mandrake

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.
 
O

Old Wolf

Kelly said:
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]);
.. }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top