/* intcount.cpp * Anthony Larrain */ #include #include #include using namespace std; class IntCount { private: static const int UPPER = 11; int list[UPPER]; public: IntCount(); int& operator[](int i); friend ostream& operator<<(ostream& out, const IntCount obj); }; int main(){ IntCount count; ++count[65]; ++count[2]; ++count[4]; ++count[4]; ++count[9]; for(int i = 0; i < 200 ; i++){ ++count[10]; ++count[1]; } cout << endl << count << endl; system("pause"); return 0; } IntCount::IntCount() { for (int i = 1; i < UPPER; i++) list[i] = 0; } int& IntCount::operator[](int i) { if (i < 1 || i >= UPPER){ cout << "index " << i << " is out of range." << endl; return list[0]; // list[0] is not in our range so we are okay. } return list[i]; } ostream& operator<<(ostream& out, const IntCount obj){ for (int i = 1; i < obj.UPPER; i++){ cout << setw(2) << i << " is " << obj.list[i] << endl; } return out; }