I have a vector that I want to limit access to. I want to only be able to access the vector through a class so that no part of the program can just accidentally go in and change something. Something like this
I am wondering if the vector needs to be protected or private, Or if there is a better way to do this?Code:class My_Class
{
public:
Numbers();
~Numbers();
void Display(int pos1, int pos2);
void Add(int x);
void Replace(int x, int pos);
void Remove(int pos);
void Clear();
protected:
vector<int> Numbers;
vector<int>::iterator iter1;
vector<int>::iterator iter2;
};
void Number::Display(int pos1, int pos2)
{
for (iter1 = Numbers.begin() + pos1; iter1 <= Numbers.begin() + pos2; ++iter1)
cout<<*iter1 <<endl;
return;
}
void Number::Add(int x)
{
Numbers.push_back(x);
return;
}
void Replace(int x, int pos)
{
Numbers.at(pos) = x;
return;
}
.......
Also, by doing this will the vector remain intact for the entire program or just while the class is in scope?