Representing Arrays as a class in cpp

Program to implement an array.(Source Code)
// Program Ch02pr01
// Program to implement an array.

#include    
  
using namespace std;  
  
const int MAX \= 100 ;  
  
class array  
{  
    private :  
  
        int arr\[MAX\] ;  
        int N;  
        int pos;  
  
    public :  
        void create(int i)  
        {  
             N\=i;  
             int arr\[N\];  
             pos\=0;  
               
        }  
        void add(int n);  
        void insert ( int pos, int num) ;  
        void del ( int pos ) ;  
        void reverse( ) ;  
        void display( ) ;  
        void search ( int num ) ;  
} ;  
void array::add(int n)  
{  
     arr\[pos++\]\=n;  
}  
// inserts an element num at given position pos  
void array :: insert ( int pos, int num )  
{  
    // shift elements to right  
    int i;  
    for ( i \= N \- 1 ; i >\= pos ; i\-\- )  
    {  
        arr\[i\] \= arr\[i \- 1\] ;  
    }  
    arr\[i\] \= num ;  
  
}   
  
// deletes an element from the given position pos  
void array :: del ( int pos )  
{  
    // skip to the desired position  
    int i;  
    for (  i \= pos ; i < N ; i++ )      
        arr\[i \- 1\] \= arr\[i\] ;  
    arr\[i \- 1\] \= 0 ;  
  
}  
  
// reverses the entire array  
void array :: reverse( )  
{  
    for ( int i \= 0 ; i < N / 2 ; i++ )  
    {  
        int temp \= arr\[i\] ;  
        arr\[i\] \= arr\[N \- 1 \- i\] ;  
        arr\[N \- 1 \- i\] \= temp ;  
    }  
}  
  
// searches array for a given element num  
void array :: search ( int num )  
{  
    // Traverse the array  
    int i;  
    for (  i \= 0 ; i < N ; i++ )  
    {  
        if ( arr\[i\] \=\= num )  
        {  
            cout << "\\n\\nThe element " << num << " is present at "<< ( i + 1 ) << "th position" ;  
            return ;  
        }  
    }  
  
    if ( i \=\= N )  
        cout << "\\n\\nThe element " << num << " is not present in the array" ;  
}  
  
// displays the contents of a array  
void array :: display( )  
{  
    cout<< endl ;  
    // traverse the entire array  
    for ( int i \= 0 ; i < N ; i++ )  
        cout << "  " << arr\[i\] ;  
}  
  
int main( )  
{  
    array a ;  
    a.create(6);  
    a.insert ( 1,11 ) ;  
    a.insert ( 2,12 ) ;  
    a.insert ( 3,13 ) ;  
    a.insert ( 4,14 ) ;  
    a.insert ( 5,15 ) ;  
    a.insert(6, 16);  
  
    cout << "\\nElements of Array: " ;  
    a.display( ) ;  
  
    a.del ( 5 ) ;  
       a.del ( 2 ) ;  
    cout << "\\n\\nAfter deletion: " ;  
    a.display( ) ;  
  
    a.insert ( 2, 222 ) ;  
    a.insert ( 5, 555 ) ;  
    cout << "\\n\\nAfter insertion: " ;  
    a.display( ) ;  
  
    a.reverse( ) ;  
    cout << "\\n\\nAfter reversing: " ;  
    a.display( ) ;  
  
    a.search ( 222 ) ;  
    a.search ( 666 ) ;  
    getch();  
    return 0;  
}  


See also