Using Variables for Intelligent Access Functions?

This is a discussion on Using Variables for Intelligent Access Functions? within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I知 an intermediate-level C++ programmer grappling with a fairly rudimentary problem. I知 creating a C++ object called FruitCart; ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    New Jersey
    Posts
    14

    Using Variables for Intelligent Access Functions?

    Hello everyone,

    I知 an intermediate-level C++ programmer grappling with a fairly rudimentary problem. I知 creating a C++ object called FruitCart; FruitCart contains a number of integers categorizing the numbers of individual fruit in the cart. I also need FruitCart to contain accessor functions so other functions can set, retrieve, and print the fruit values. Here痴 what I have so far:

    --------------------------------------------------------------------------------------------------------
    Code:
    class FruitCart {
      public:
        // Constructors
        FruitCart();
        ~FruitCart();
    
        // Accessors
        void SetApples(int a)  {Apples=a;}
        int GetApples()        {return Apples;}
        void PrintApples()     {cout<<Apples;}
    
        // Need accessor functions like the above for all kinds of fruit
    
      protected:
        int Apples;   // Number of Apples in the cart
        int Oranges;  // Number of Oranges in the cart
        int Bananas;  // Number of Bananas in the cart
        int Plums;    // Number of Plums in the cart
    
        // May have nearly 50 kinds of fruit!
    
    };
    --------------------------------------------------------------------------------------------------------

    The problem I have is that I may have over fifty kinds of fruit in the cart! It would be a big pain to manually write out accessor functions for each kind of fruit.

    I知 searching for a more intelligent solution. What I壇 like to have is a general accessor function where I pass in the name of the fruit as a string (specifically, as a char * pointer) and leave everything else the same. Put in quasi-psuedocode:

    --------------------------------------------------------------------------------------------------------
    Code:
    class FruitCart {
      public:
        // Constructors
        FruitCart();
        ~FruitCart();
    
        // Accessors
        void SetFruit(int a, char * FruitType)
        int GetFruit(char * FruitType)
        void PrintFruit(char * FruitType)
    
      protected:
        int Apples;   // Number of Apples in the cart
        int Oranges;  // Number of Oranges in the cart
        int Bananas;  // Number of Bananas in the cart
        int Plums;    // Number of Plums in the cart
    
    };
    
    void FruitCart::SetFruit(int a, char * FruitType)
      {  ${FruitType} = a;  }
    int FruitCart::GetFruit(char * FruitType)
      {  return ${FruitType};  }
    void FruitCart::PrintFruit(char * FruitType)
      {  cout << ${FruitType};  }
    --------------------------------------------------------------------------------------------------------

    Hopefully this makes sense. I know this would be simple to do in UNIX, so I知 hoping there痴 a (relatively) simple way to do this in C++.

    Many thanks!
    -Pete

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Sounds like the problem map<string, int> was born to solve.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Location
    New Jersey
    Posts
    14

    Smile Fantastic!

    This is great advice, thanks a million! I can't tell you how great it is to get expert help like this.

    Many thanks!
    -Pete

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help coming up with variables, functions
    By Dokkan in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2010, 03:41 PM
  2. C functions to access USB port.
    By nethunter in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-10-2006, 02:25 PM
  3. Static variables in functions
    By tretton in forum C Programming
    Replies: 1
    Last Post: 04-08-2006, 06:49 AM
  4. Parse a program for functions, variables
    By Enu in forum C Programming
    Replies: 2
    Last Post: 02-15-2006, 10:08 AM
  5. Class member variables only readable by member functions?
    By _Elixia_ in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2003, 03:52 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21