473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

way to address objects in for loop

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
4 1099
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jon Franz | last post by:
----- Original Message ----- From: "Jon Franz" <jfranz@neurokode.com> To: "Serge Orlov" <sombDELETE@pobox.ru> Sent: Wednesday, November 19, 2003 2:39 PM Subject: Re: Python Database Objects (PDO)...
8
by: CJM | last post by:
I'm trying to retrieve the users email address via ADSI in ASP. So far, I have the following code: Set oUser = GetObject("WinNT://domain/" & Request.Form("UID") & ",user") Response.Write...
10
by: gogogo_1001 | last post by:
Dear all, I don't understand why "delete" works well on destructing a object, but fails to destruct a vector of it. Any of your comment is highly appreciated! Following is the program...
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
4
by: outforblood74 | last post by:
Ok here's the deal, the for each statment using VB 6 doesn't loop all the objects. I would like it to close all the IE browsers that are open, but it doesn't work. And now I'm concerned that its...
2
by: elaine | last post by:
I'm working on a .net web application. The architect of this web application is quite different than other web applications i worked before. Since we use a set of tools to generate most of the...
14
by: NetworkElf | last post by:
Hi all, Does anyone have some code that shows an example of how to loop through a range of IP addresses? I'm using text boxes to get a start and end value for the range. I was thinking about...
12
by: BillE | last post by:
I'm trying to decide if it is better to use typed datasets or business objects, so I would appreciate any thoughts from someone with more experience. When I use a business object to populate a...
3
by: Bruno.DiStefano | last post by:
Hi All, BACKGROUND INFO I need to use a "vector" structure to store a number of objects that becomes known only at run time. The constructor, at instantiation time of a new object, increments a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.