Arrays within a class

Array is a collection of homogenous data, arranged in sequential format. Learning the concept of arrays in C is very important as it is the basic data structure. Here, in this section, we shall look into some very useful array programs to give you insight of how C programming language deals with arrays.

C Array Types

Different Types of the array in C Programming language

1.Single Dimensional Array :

  1. Single or One Dimensional array is used to represent and store data in a linear form.
  2. Array having only one subscript variable is called One-Dimensional array
  3. It is also called as Single Dimensional Array or Linear Array

Syntax :

<data-type> <array_name> [size];

Example of Single Dimensional Array :

int iarr[3]   = {2, 3, 4};

char carr[20] = "c4learn" ;

float farr[3] = {12.5,13.5,14.5} ;

2. Multi Dimensional Array :

  1. Array having more than one subscript variable is called Multi-Dimensional array.
  2. Multi Dimensional Array is also called as Matrix.

Syntax :

<data-type> <array_name> [row_subscript][column-subscript];

Example : Two Dimensional Array

int a[3][3] = { 1,2,3
                5,6,7
                8,9,0 };

Array within a Class

The array can be used as member variables in a class. The following class definition is valid.

const int size=10;

class array
{
int a[size];
public:
void setval(void);
void display(void);
};

The array variable a[] declared as private member of the class array can be used in the member function, like any other array variable. We can perform any operations on it. For instance, in the above class definition, the member function setval() sets the value of element of the array a[], and display() function displays the values. Similarly, we may use other member functions to perform any other operation on the array values.