C++ examples for Class:Operator Overload
Overload the Subscripting Operator []
#include <iostream> #include <new> #include <cstdlib> using namespace std; template <class T, int len> class MyArray {//w ww . ja v a2 s .c o m T *aptr; int length; public: // The MyArray constructor. MyArray(); // The MyArray copy constructor. MyArray(const MyArray &obj); // Release the allocated memory when a MyArray object // goes out of scope. ~MyArray() { delete [] aptr; } // Overload assignment. MyArray &operator=(const MyArray<T,len> &rh_op); T &operator[](int i); int getlen() { return length; } }; template <class T, int len> MyArray<T, len>::MyArray() { try { // Allocate the array. aptr = new T[len]; } catch(bad_alloc ba) { cout << "Can't allocate array.\n"; // Take appropriate action here. This is just // a placeholder response. exit(1); } // Initialize the array elements to their default value. for(int i=0; i < len; ++i) aptr[i] = T(); length = len; } template <class T, int len> MyArray<T, len>::MyArray(const MyArray &obj) { cout << "Using MyArray's copy constructor to make a copy.\n"; try { aptr = new T[obj.length]; } catch(bad_alloc ba) { cout << "Can't allocate array.\n"; exit(1); } length = obj.length; // Copy contents of the array. for(int i=0; i < length; ++i) aptr[i] = obj.aptr[i]; } template<class T, int len> MyArray<T, len> & MyArray<T, len>::operator=(const MyArray<T, len> &rh_op) { cout << "Assigning one MyArray object to another.\n"; if(aptr && (length != rh_op.length)) { delete aptr; try { aptr = new T[rh_op.length]; } catch(bad_alloc ba) { cout << "Can't allocate array.\n"; exit(1); } } length = rh_op.length; for(int i=0; i < length; ++i) aptr[i] = rh_op.aptr[i]; return *this; } template <class T, int len> T &MyArray<T, len>::operator[](int i) { if(i < 0 || i > length) { // Take appropriate action here. This is just // a placeholder response. cout << "\nIndex value of " << i << " is out-of-bounds.\n"; exit(1); } return aptr[i]; } template <class T, int len> MyArray<T, len> f(MyArray<T, len> x) { cout << "f() is returning a copy of x.\n"; return x; } class myclass { public: int x; myclass(int i) { x = i; }; myclass() { x = -1; } }; int main() { // Use the integer array. MyArray<int, 5> i_ar; for(int i=0; i < i_ar.getlen(); ++i) i_ar[i] = i; cout << "Contents of i_ar: "; for(int i=0; i < i_ar.getlen(); ++i) cout << i_ar[i] << " "; cout << "\n\n"; // To generate a boundary overrun, uncomment the following line: // i_ar[19] = 10; // To generate a boundary underrun, uncomment the following line: // i_ar[-2] = 10; // Create a copy of i_ar. This will invoke MyArray's copy constructor. cout << "Create i_ar2 and initialize it with i_ar. This results\n" << "in MyArray's copy constructor being called.\n\n"; MyArray<int, 5> i_ar2 = i_ar; cout << "Contents of i_ar2: "; for(int i=0; i < i_ar2.getlen(); ++i) cout << i_ar2[i] << " "; cout << "\n\n"; cout << "Create i_ar3.\n"; MyArray<int, 5> i_ar3; cout << "Original contents of i_ar3: "; for(int i=0; i < i_ar3.getlen(); ++i) cout << i_ar3[i] << " "; cout <<"\n\n"; i_ar3 = f(i_ar); cout << "Contents of i_ar3 after receiving value from f(i_ar): "; for(int i=0; i < i_ar3.getlen(); ++i) cout << i_ar3[i] << " "; cout << "\n\n"; MyArray<myclass, 3> mc_ar; cout << "Original contents of mc_ar: "; for(int i=0; i < mc_ar.getlen(); ++i) cout << mc_ar[i].x << " "; cout << endl; mc_ar[0].x = 9; mc_ar[1].x = 8; mc_ar[2].x = 7; cout << "Values in mc_ar after setting them: "; for(int i=0; i < mc_ar.getlen(); ++i) cout << mc_ar[i].x << " "; cout << "\n\n"; MyArray<myclass, 3> mc_ar2; mc_ar2 = f(mc_ar); cout << "Contents of mc_ar2 after receiving f(mc_ar): "; for(int i=0; i < mc_ar2.getlen(); ++i) cout << mc_ar2[i].x << " "; cout << endl; return 0; }