Quick class question

Hi,

Sorry if this a stupid question but I'm having trouble figuring something out, I've had a search around but couldn't find anything.

I've created a class with various members and want to check if any of the members information is the same as user input. i.e. I have

MemberOne.nAge = 30
MemberTwo.nAge = 28 etc...

How can I have an if statement that checks all ages, in other words accomplish the same thing as;

1
2
3
4
5
6
7
8
if (MemberOne.nAge==50)
{
//Do this
}
else if (MemberTwo.nAge=50)
{
//Do this
} etc...


Thanks
You can have an array of classes. They're treated just like any other variable.
So in your case you could do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Foo
{
public:
int var;
Foo():var(0){};
}

int main()
{
Foo MyFoos[10];
for(int i = 0; i < 10; ++i)
{
if(MyFoos[i].var == x)
{
//Do something
}
}
return 0;
}
I see, that makes a lot of sense. However is there no function to do as I have described? (It doesn't really matter as the way have shown is simple, I'm just curious really)

Thanks BlackSheep
closed account (jw6XoG1T)
you could do something similar to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

class Ages
{

     public:

     int ages;
};


int main()
{
     Ages Member[10];

     Member[0].ages = 28;
     Member[1].ages = 35;
     Member[2].ages = 42;
     //etc.......

     for (int i = 0; i < 10; i++)
     {
           if (Member[i].ages == 50)
           {
                 //do something
           }
     }

     return 0;
}
Topic archived. No new replies allowed.