#include #include using namespace std; class cLuku { int arvo; public: cLuku(int arvo=0) { this->arvo = arvo; } int get() const { return arvo; } friend ostream &operator<< (ostream &os, const cLuku &luku); }; ostream &operator<< (ostream &os, const cLuku &luku) // Inserter { os << luku.arvo; return os; } class cTaulukko { int max_koko; int lkm; cLuku **alkiot; public: cTaulukko(int akoko) { max_koko = 0; alkiot = new(nothrow) cLuku*[akoko]; if ( alkiot ) max_koko = akoko; lkm = 0; } ~cTaulukko(); int lisaa(const cLuku &luku) { if ( lkm >= max_koko ) return 1; cLuku *uusi = new(nothrow) cLuku(luku); alkiot[lkm] = uusi; lkm++; return 0; } void tulosta(ostream &os=cout) const; }; // HUOM! Luokasta puuttuu vielä kopionmuodostin ja kopionsijoitus!!! cTaulukko::~cTaulukko() { if ( !max_koko ) return; for (int i=0; i < lkm; i++) delete alkiot[i]; delete [] alkiot; max_koko = 0; } void cTaulukko::tulosta(ostream &os) const { int i; for (i=0; i < lkm; i++) os << setw(5) << *alkiot[i]; os << endl; } int main(void) { cTaulukko luvut(7); luvut.lisaa(cLuku(0)); luvut.lisaa(cLuku(2)); // Ilo on täällä!!! luvut.tulosta(); return 0; }