Using Private Arrays??

Hi everyone,

I'm trying to use a private function with arrays as if it were public. I'm not programming savy, but I gather I'm supposed to use a pubilc access specifier. I've never really used classes before but I also gather that not doing so is messing stuff up for me. Can anyone help me with this? It'd be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

	class{
public:
	>How do get move the data to and from the private function?<
private:
    int Fix( int One[], int Two[], int Three[], int Four[]) { ...... return 0;}
        };

int main() {

	int Array1[3] = {9,4,7};
	int Array2[3] = {5,2,2};
	int Array3[3] = {5,0,3};
	int Array4[3] = {7,4,6};

	Fix( Array1, Array2, Array3, Array4);
	cout << Array1[0] << Array1[1] << Array1[2];
	cout << Array2[0] << Array2[1] << Array2[2];
	cout << Array3[0] << Array3[1] << Array3[2];
	cout << Array4[0] << Array4[1] << Array4[2];
return 0;}


Many Thanks.
First you need to give your class a name - like class ArrayFixer
Second: You can call only call public functions in a class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class ArrayFixer
{
public:
  int Fix(int One [], int Two [], int Three [], int Four []) 
  { 
    return 0; 
  }
};

int main() 
{
  ArrayFixer af;

  int Array1[3] = { 9,4,7 };
  int Array2[3] = { 5,2,2 };
  int Array3[3] = { 5,0,3 };
  int Array4[3] = { 7,4,6 };

  af.Fix(Array1, Array2, Array3, Array4);
  cout << Array1[0] << Array1[1] << Array1[2];
  cout << Array2[0] << Array2[1] << Array2[2];
  cout << Array3[0] << Array3[1] << Array3[2];
  cout << Array4[0] << Array4[1] << Array4[2];
  return 0;
}
You can have a private function, but you need to call it from a public one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Example {

public:
    int my_public_method()
    {
          return my_private_method();
     }

private:
     int my_private_method()
     {
         // do something
     }
};
Hantago, I mean like the code below but for a funtion that uses interger arrays.

Thomas1965, I'm aware of class names but I forgot to type it here. The code below makes me think otherwise. Can it be modfied for multiple int arrays? Thanks for your relpy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

class Iforgot{
public:
	void setName(string x){
		name = x;
	}
	string getName(){
		return name;
	};

private:
	string name;
};

int main() {

	Iforgot stuff;
	stuff.setName("I need to use int arrays, not strings.");
	cout << stuff.getName();
return 0;}
Nuderobmonkey , Thanks so much. I'll try it!
@ Hitsuin You could define your own Function thats beahves lik an array, but have added some functions (which are named methods if they belong to a class). Therefore you could overload the []-operator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template <typename T>
class MyArray
{
private:
     T  * m_array;
     int m_size;

public:
     MyArray(int size = 0 )
     : m_size{size}, m_array{nullptr}
     {}

      int size() { return m_size; }

      T& operator[] (int index)
      {
            if (index < 0 && index >= m_size){
                 throw out_of_range;
            }
            return m_array[index];
      }
      
      int Fix(....) { // implement your code }
};


Instead, you also could derive a class of a build-in container:
1
2
3
4
class MyArray : public std::array
{
     int Fix(.....) { // your code }
};


And so you use it:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {
    // So you could make a two-dimensional Array:
    MyArray<MyArray<int> > array(3);
    for (int i = 0; i < array.size(); i++) {
        array[i] = MyArray<int> (3);
    }

    // you can access this like an array:
   array[0][1] = 25;
   
   // you can call your Fix-method:
   array.Fix(....);
}
Topic archived. No new replies allowed.