All pastes #1917306 Raw Edit

[] operator Overloading

public cpp v1 · immutable
#1917306 ·published 2010-08-15 10:06 UTC
rendered paste body
//[] 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;}