[RESOLVED] Using Variables for Intelligent Accessor Functions?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] Using Variables for Intelligent Accessor Functions?

  1. #1
    Join Date
    Apr 2010
    Posts
    19

    [RESOLVED] Using Variables for Intelligent Accessor Functions?

    Hello everyone,

    Iím an intermediate-level C++ programmer grappling with a fairly rudimentary problem. Iím 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ís what I have so far:

    --------------------------------------------------------------------------------------------------------
    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ím searching for a more intelligent solution. What Iíd 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:

    --------------------------------------------------------------------------------------------------------
    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ím hoping thereís a (relatively) simple way to do this in C++.

    Many thanks!
    -Pete

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,292

    Re: Using Variables for Intelligent Accessor Functions?

    Quote Originally Posted by phummon View Post
    Hello everyone,

    I’m an intermediate-level C++ programmer grappling with a fairly rudimentary problem. I’m 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’s what I have so far:

    --------------------------------------------------------------------------------------------------------
    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’m searching for a more intelligent solution. What I’d 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:

    --------------------------------------------------------------------------------------------------------
    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’m hoping there’s a (relatively) simple way to do this in C++.

    Many thanks!
    -Pete
    C++ does not do so well with converting strings to variable names. However, it works quite well with enums:

    Code:
    enum fruit
    {
        apples,
        banana,
        pizza, //I love that fruit
        FRUIT_LAST_VALUE
    };
    
    In this case, apples will implicitly be equal to 0, banana to 1, pizza to 2, and finally, FRUIT_LAST_VALUE will have the convenience of being equal to 3, which is the size of your enum.
    then in code, you can do this:
    Code:
    class FruitCart {
      public:
        // Constructors
        FruitCart();
        ~FruitCart();
    
        // Accessors
        void SetFruit(fruit iFruit, int i)  {_Fruits[iFruit] = i;}
        int GetFruit(fruit iFruit)         {return _Fruits[iFruit];}
        void PrintFruit(fruit iFruit)     {std::cout << _Fruits[iFruit];}
    
      protected:
        fruit _Fruits[FRUIT_LAST_VALUE]; //Array holding as many values as there are fruits. The cool part is that this should grow all by itself.
    
    };
    There, I hope that helps.

    PS: A map will be able to do the string to fruit conversion you need, but I would highly advise against this type of implementation. C++ was not built for this kind of thing, and it will turn into a development nightmare. Perl was built for this, but don't force a language's paradigm into another one. When in Rome (C++) do as the Romans (C++ method of coding).
    Last edited by monarch_dodra; April 26th, 2010 at 02:57 AM.

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,013

    Re: Using Variables for Intelligent Accessor Functions?

    Quote Originally Posted by monarch_dodra View Post
    PS: A map will be able to do the string to fruit conversion you need, but I would highly advise against this type of implementation. C++ was not built for this kind of thing, and it will turn into a development nightmare. Perl was built for this, but don't force a language's paradigm into another one. When in Rome (C++) do as the Romans (C++ method of coding).
    The difference is that with an enum your types of fruit are hard coded, whereas with a map they can be dynamic - e.g., they could be read from file. You (the OP) should pick the appropriate method according to your requirements.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Apr 2010
    Posts
    19

    Talking Re: Using Variables for Intelligent Accessor Functions?

    Wow! Great advice! This is exactly what I was hoping to learn, many many thanks!!!

    Yours,
    -Pete

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center