Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Trying to understand the meaning of code in my program more clearly.

$
0
0
This is a header file for my program, and what its function is to be an array class as you can tell. But there are some question I have about it.
My question is, what exactly is the function of 'arry = new int[size];' in line 9?
And what are the functions of these operators 'int& operator[](int index)' & 'int operator[] (int index) const ' in line 12 and 17? I just want to make sure I understand all the elements of this program before I move ahead. And thanks for reading this guys, I really appreciate it. :)

 #include <iostream>

using namespace std;
class Array {
      public:

           Array(int sizeofarray){
             size = sizeofarray;
             arry = new int[size]; 
           }
           ~Array() { delete [] arry; }
           int& operator[](int index) //overloading operator[] . this is for the left hand side of assignment
           {
                return arry[index];
           }

           int operator[] (int index) const 
           {
               return arry[index];
           }
           void print()
           {
                for (int i = 0; i < size; i++)
                   cout<<arry[i]<<" ";
                cout<<endl;
            }
      private:
              int size;
              int* arry;//this is the built-in array that Array class is really manipulating/processing
};



Oops, seems like it double posted. :/ sorry, is there a way to delete this thread?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>