6

I want to use a class in C++ that has an integer array like this:

class A{
private:
        int arr[50];

}

I will read something from text file like this:

sum i1 i2

That means: Sum of arrays index1 and index2 and store in index1.

How can I do it, with using getters and setters like:

seti2(geti1()+geti2())

or something like that, (because it's not very useful, I don't want to write getter and setter for every index geti1() geti2() ... geti50())

Do you have any idea?

By the way, my second question is that, is getter should not have any parameters and is setter should have only one parameter?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 3
    You might want to read [Are getters and setters poor design?](http://stackoverflow.com/questions/565095/are-getters-and-setters-poor-design-contradictory-advice-seen) – Bo Persson Oct 28 '16 at 12:32
  • 1
    'Doing this is inconvenient' - so don't do it. Why do you think you need to do it? Why is it private? Either **(A)** it should be encapsulated, if you have some reason to check/manipulate input before getting/setting it - or you want to operate at a higher level, with the class doing the work and you just calling methods like `sortArray()`, `processArray()`, etc - _xor_ **(B)** it shouldn't, so the get/setter are pointless. If all you want is unabstracted public access, accessors are pointless bloat - just use a plain array, and stop pretending there's encapsulation as a token gesture. – underscore_d Oct 29 '16 at 12:27

4 Answers4

6

One idea might be to use actual indexes. So you have a single get function which takes an index as an argument, and a single set function which takes an index and the value as arguments.

Another solution is to overload the operator[] function, to provide nice array-like indexing.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • thanks I know it but my problem is that how can I understand which get's index should be call? I mean if file is : add i1 i2 , I should do set(get(i1)+get(i2),i1) but thats bot general solution its private, if file is add i48 i3 how can I do this: set(get(48)+ get(i3), i48) ? – Jonathan Cedric Oct 28 '16 at 10:38
  • 1
    @FurkanYıldız That is really a different question. If that format is all you need to support then it's relatively easy using simple string functions. Still another question. Please first try it yourself first (input streams and the input operator `>>` is basically all you need here), and if you don't manage it try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and post a new question. – Some programmer dude Oct 28 '16 at 11:01
0

To encapsulate using setter/getter, you can use, for example:

class A{
  private:
    int arr[50];
  public:
    int get(int index);
    void set(int index, int value);
}
...
int A::get(int index) {
   return arr[index];
}
void A::set(int index, int value) {
   arr[index] = value;
}
..    
instanceOfA->set(1, instanceOfA->get(1) + instanceOfA->get(2));

However, parsing command read from text file will require more work.

Zamrony P. Juhara
  • 5,222
  • 2
  • 24
  • 40
0

If you still want to take advantage of the names of your fields, you could have a single getter/setter and use an enumeration to make your code a bit more meaningful:

class A{
public:
    enum Index
    {
        INDEX_SUM,
        INDEX_I1,
        INDEX_I2,
        INDEX_I3,
        ...
        INDEX_I50,
    };

    int geti(const Index index);

    void seti(const Index index, const int value);

private:
    int arr[50];

};

int A::geti(const Index index)
{
    return arr[static_cast<int>(index)];
}

void A::seti(const Index index, const int value)
{
    // Maybe don't allow "sum" to be set manually?
    if (INDEX_SUM == index)
        throw std::runtime_error("Cannot set sum manually");

    arr[static_cast<int>(index)] = value;

    // Maybe update sum?
    arr[INDEX_SUM] = std::accumulate(arr, arr + 50, 0);
}

If you don't want to manually create the enum, and have access to the Boost libraries, you can use BOOST_PP_ENUM_PARAMS. Alternatively you can use a simple shell script to generate the enum. See this stackoverflow question for more information on that.

Community
  • 1
  • 1
Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65
0

May I suggest:

class A{
private:
        const int ARR_SIZE = 50;
        int arr[ARR_SIZE];
public:
    int get(int _iIndex) 
    {
        return arr[_iIndex];
    }

    void set(int _iIndex, int _iValue)
    {
         if (_iIndex < ARR_SIZE)
             arr[_iIndex] = _iValue;
    }
}

So you can;

get(i);

set(i, get(x) + get(y));
Sampath
  • 1,144
  • 1
  • 21
  • 38