Thread: help with classes

  1. #1
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116

    help with classes

    ok suppose i have a class called members, and all the members are listed by their id (ie the first one is 1, second is 2, etc.) and i want to check each member for a certain property, how would i do that? ok that didnt make much sense, so i want to do something to this effect.
    Code:
    //this is just fragments of code, not an actual code
    //also members have a number as well as an ID, they are listed by ID and have a number that is used for something else
    
    class members {
      public:
      int getNumber();
      members(); //constructor
      private:
      int number;
    }
    
    int members::getNumber()
    {
      return number;
    }
    
    for(int n = 1; n <= m; n++) //m is a variable with the total number of members of the class
    {
      a = n.getNumber();
      if(a == 0240)
      {
        //do something here
      }
    }
    so basicly i want to quickly go through each member and check to see if one of their variables equals something. that way if i was to add another member, i could check to see if the variable number i was going to asign them is taken or not.
    thanks in advance for your help
    Last edited by Geo-Fry; 04-13-2003 at 10:20 AM.
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You could use a flag, and if a member is found with the ID you were gonna use, then set the flag, so you will know later on whether or not to try a different ID.

    Also: Why is your constructor private? It should always be public.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    You could use a flag, and if a member is found with the ID you were gonna use, then set the flag, so you will know later on whether or not to try a different ID.
    a flag? i dont know what that is
    Also: Why is your constructor private? It should always be public.
    oops, that was a typo
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Have a bool, initialized to false, and when you go through and compare the IDs, if there is a batch, set the bool to true and exit the loop.
    Code:
    bool isFound = false;
    
    for(int n = 1; n <= m; n++) //m is a variable with the total number of members of the class
    {
      a = n.getNumber();
      if(a == 0240)
      {
        isFound = true;
        break;
      }
    }
    
    if( isFound )
    {
      //try checking with another ID
    } else {
      //you have a unique ID
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Another way you could do it is to store used IDs in a std::set, which has its own find function.

    Code:
    #include <set>
    
    ...
    
    std::set<int> mySet;
    
    ...
    
    if( mySet.find( IDToLookFor ) == mySet.end( ) ) {
    
      // It was NOT found
      mySet.insert( IDToLookFor );
    
    } else {
    
      // It WAS found
    
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    alright thanks for all the help xsquared, youre my hero
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM