/* ************************************************************************** zadanie domowe nr: 3 plik: elem.cpp ************************************************************************** Poprawiał : CEZARY PRZYWUSKI ID212 Zmiany w pliku w liniach 72 - 99 */ #include #include #include "elem.h" elem::elem () { pnext = 0; cout << "konstruktor klasy elem nr." << id << endl; } elem::~elem () { if (pnext) delete pnext; cout << "destruktor klasy elem nr."<< id << endl; } elem * elem::insert_after (elem * ael) { if (!pnext) return (pnext = ael); if (!ael) return 0; ael->set_next (pnext); return pnext = ael; } elem * elem::set_next (elem * ael) { if (!ael) return 0; return pnext = ael; } elem_str::elem_str (char *val) { cout << "konstruktor klasy elem_str" << endl; if (val) { my_val = new char[strlen (val) + 1]; strcpy (my_val, val); } else my_val = 0; } elem_str::~elem_str () { if (my_val) cout << "destruktor elem_str, przechowywana wartosc: " << my_val << endl; else cout << "destruktor elem_str - brak wartoœci" << endl; if (my_val) delete[]my_val; } /* Bardzo niepoprawna implementacja operatora [] */ elem_str null_elem("(null)"); /* elem_str &elem_str::operator[](int indeks) { elem_str *active=this; for(int n=0;nget_next()) { active=(elem_str *) active->get_next(); } else return null_elem; } return *active; } */ elem_str &elem_str::operator[](int indeks) { elem_str *active; if (indeks==0) { return *this ; } active=(elem_str *) this->get_next(); if (active) { active[indeks--]; } else return null_elem; } /* Przeciążenie operatora przyrówania */ const elem_str &elem_str::operator = (const elem_str &prawy) { if(prawy.my_val) // czy nie wpisujemy nulla { delete [] my_val; // zwalniamy pamięć po starym napisie my_val = new char[strlen(prawy.my_val)]; // tworzymy nowy łańcuch o długości wpisywanego napisu strcpy(my_val,prawy.my_val); // lub bardziej inteligentna /* for(int i=0;i