Here is an example of using an array to store some data:
Code:
#include <iostream>
using namespace std;
void show(int num[], int size)
{
for(int i = 0; i<size; i++)
{
cout<<num[i]<<" ";
}
cout<<endl;
}
int main()
{
const int size = 3;
int numbers[size] = {10, 20, 30};
show(numbers, size);
return 0;
}
However, with an array all the data must be of the same type, in this case int's. With a class, you can store different types of data in it:
Code:
#include <iostream>
#include <string>
using namespace std;
class Data
{
public:
int num1;
double num2;
string str;
};
void show(Data aDataObj)
{
cout<<aDataObj.num1<<" "<<aDataObj.num2<<" "<<aDataObj.str<<endl;
}
int main()
{
Data myData;
myData.num1 = 10;
myData.num2 = 3.5;
myData.str = "hello";
show(myData);
return 0;
}
So, to start, you can think of a class as an array that can store different types of values.