passing arrays to classes

Just a general qustion... How would you go about passing a char array (for example) to a class? I'm so confused. Thanks!!
What do you mean, passing it to a class?

Usually when you "pass X to Y" it means you are passing a parameter X to a function Y.
A class is not a function: it is a collection of data and functions.

That said, you can do what is called "operator overloading" for a class.
Then instead of a function you'll use an operator (which is basically a function in disguise).

Here are some links to articles you should read now:
http://www.cplusplus.com/doc/tutorial/classes/
http://www.cplusplus.com/doc/tutorial/templates/

Going back to how to pass an array to a function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int my_int_array[10];

// the traditional way (C and C++)
void func1(int *arr, int size)
{
}

// passing by reference (C++ only)
void func2(int (&arr)[10])
{
}

int main()
{
    func1(my_int_array, 10);
    func2(my_int_array);
}
Last edited on
Well, for example, the user inputs a series of numbers and then it gets maniuplated in the class. How would I pass someArray[] to the class?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class myArray
{
    //Add the elements of the array, for example
    
   //Then how would I pass the array back to the main function??
};

int main()
{
cout << "Enter and array";
int someArray[3];
for (int i = 0; i < 3; i++)
    cin >> someArray[i]
//How would I pass someArray[] to the class to maipulate it?
}
In your example, someArray is data.
Maybe you want this data to be a part of the class myArray.

1
2
3
4
5
6
class myArray
{
private:

    int someArray[3];
};


Now, since myArray contains the data, it would make sense for it to also contain functions for reading it.
Basically we move the reading of someArray into the myArray 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
26
27
28
29
30
#include <cstddef>
#include <iostream>

class myArray
{
private:

    int someArray[3];

public:

    void readArray()
    {
        std::cout << "Reading someArray elements.\n";

        // prefer using size_t for lengths and sizes, instead of int
        for (std::size_t i=0; i < 3; ++i)
        {
            std::cout << "someArray[" << i << "]: ";
            std::cin >> someArray[i];
        }
    }
};

int main()
{
    myArray ma;

    ma.readArray();
}


Things will get more complex when you realize you need to write a copy constructor and copy assignment operator (this problem can be dealt with more elegantly by not using arrays in the first place, but instead std::vector).
http://www.cplusplus.com/doc/tutorial/classes2/
http://www.mochima.com/tutorials/vectors.html

Please read the documentation I linked you to, here it is again:
http://www.cplusplus.com/doc/tutorial/classes/
That answers my question! Appreciate it! And yes I will read those. I hear vectors are much easier, not quite there yet though, still making my way through the tutorials. Thanks!
Topic archived. No new replies allowed.