Actually, you need to declare your "array" as a data member of the class. You might initialise and possibly resize it in a constructor.
Depends a bit on what type of container you want to use as an "array" (indexed variable) - C-style arrays or dynamic arrays; std::array<T>, std:vector<T>, ...
I want to have setter and getter function to assign values inside constructor
You can't return a c-style array from a getter-function. For a setter-function this is similar to the constructor. For a getter, you could return a std::vector.
Have you an example of what you're trying to do with 'array'?
I want to have setter and getter function to assign values inside constructor
A class typically has private members (implementation) and public interface.
In code above:
1 2 3 4 5
int main() {
constint b[] { 1,2,3,4,5,6 };
S s { b };s.display();
}
The constructor and S::display() const are the interface, how other code can interact/use objects of type S.
The implementation of all members of S (like the constructor) has access to all members of S (like the a). They do not need to use any getters/setters, but may do so in order to avoid code duplication. If you simply read/modify value of an element of the array, then there is no need for getter/setter in that.
Yes. I used a struct for an example of a constructor for simplicity as the OP didn't mention class. Of course, for a struct everything is public by default whereas with a class everything is private by default.