Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //[] operator Overloading
- #include <iostream.h>
- #include <process.h>
- class matrix
- {
- private:
- int mat[4];
- public:
- matrix()
- {
- mat[0]=1;
- mat[1]=2;
- mat[2]=3;
- mat[3]=4;
- }
- int operator[](int index)
- {
- if(index>=0&&index<=3)
- return mat[index];
- else
- {
- cout<<"Index Out of Array Limits!";
- exit(0);
- }
- }
- };
- int main()
- {
- matrix m;
- cout<<"\nSafe Array Access:\nm[0]= "<<m[0]<<endl<<"m[1]= "<<m[1]<<endl<<"m[2]= "<<m[2]<<endl<<"m[3]= "<<m[3]<<endl;
- cout<<"\nUnsafe Access:\n";
- cout<<"m[5]= ";
- cout<<m[5];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement