/** * \file taul.cpp * Esimerkki dynaamisesta taulukosta * @author vesal * @version 29.4.2010 */ #include #include using namespace std; template class cTaulukko { private: int max_koko; int lkm; TYPE *alkiot; TYPE vika; cTaulukko(const cTaulukko &) {} void operator=(const cTaulukko &) { } public: cTaulukko(int max_koko) { this->max_koko = max_koko; lkm = 0; alkiot = new TYPE[max_koko]; // Huolehti että muistia saatiin??? } ~cTaulukko() { if ( max_koko ) delete [] alkiot; max_koko = 0; } void add(const TYPE &alkio); ostream &tulosta(ostream &os) const { for (int i=0; i void cTaulukko::add(const TYPE &alkio) { if ( lkm >= max_koko ) return; // kasvata tilaa alkiot[lkm++] = alkio; } template const TYPE &cTaulukko::get(int i) const { if ( 0 <= i && i < lkm) return alkiot[i]; // return *(alkiot+i); return vika; } template ostream &operator<< (ostream &os, const cTaulukko &taulukko) // Inserter { taulukko.tulosta(os); return os; } // c = a + b; /// c = a.summaa(b); int main() { cTaulukko taul(7); taul.add(2); taul.add(99); taul.add(77); taul.add(66); int a = taul.get(2); int b = taul[1]; cout << a << " " << b << endl; cout << taul << endl; // cin >> taulukko; cTaulukko staul(7); staul.add("kissa"); staul.add("koira"); //staul.add(2); cout << staul << endl; { cTaulukko taul2(7); // taul2 = taul; // käy huonosti // cout << taul2 << endl; } cout << taul << endl; }