Advertisement
steverobinson

[] operator Overloading

Aug 18th, 2010
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. //[] operator Overloading
  2. #include <iostream.h>
  3. #include <process.h>
  4. class matrix
  5. {
  6. private:
  7.      int mat[4];
  8. public:
  9.  
  10.      matrix()
  11.      {
  12.         mat[0]=1;
  13.         mat[1]=2;
  14.         mat[2]=3;
  15.         mat[3]=4;
  16.      }
  17.  
  18.  
  19.      int operator[](int index)
  20.      {
  21.          if(index>=0&&index<=3)
  22.             return mat[index];
  23.          else
  24.             {
  25.             cout<<"Index Out of Array Limits!";
  26.             exit(0);
  27.             }
  28.      }
  29. };
  30.  
  31. int main()
  32. {
  33.     matrix m;
  34.     cout<<"\nSafe Array Access:\nm[0]= "<<m[0]<<endl<<"m[1]= "<<m[1]<<endl<<"m[2]= "<<m[2]<<endl<<"m[3]= "<<m[3]<<endl;
  35.     cout<<"\nUnsafe Access:\n";
  36.     cout<<"m[5]= ";
  37.     cout<<m[5];
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement