Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FigureTab
- {
- friend ostream& operator<<(ostream& os, const FigureTab& ob);
- int dlugosc;
- Figure **tab;
- public:
- FigureTab(int dl=0) : dlugosc(dl) //konstruktor
- {
- if (dlugosc>0)
- {
- tab = new Figure *[dlugosc] { nullptr };
- }
- else
- {
- dlugosc=0;
- tab=nullptr;
- }
- }
- ~FigureTab()
- {
- cout << endl << "destruktor FigureTab zadzialal " << endl;
- for (int i=0; i<dlugosc; i++)
- {
- delete tab[i];
- }
- delete[] tab;
- }
- void dodajFigure(Figure* nowa);
- void usunWszystko();
- Figure*& operator[](int index) { return tab[index - 1]; }
- };
- void FigureTab::dodajFigure(Figure* nowa) {
- if (tab == nullptr) { // tablica nie istnieje
- tab = new Figure * [1];
- tab[0] = nowa;
- }
- else {
- // tymczasowa tablica do przechowania dotychczasowej
- Figure** temp = new Figure * [dlugosc + 1];
- // przepisanie elementów z dotychczasowej tablicy do tymczasowo stworzonej
- for (int i{ 0 }; i < dlugosc; ++i) {
- temp[i] = tab[i];
- }
- delete[] tab;
- temp[dlugosc] = nowa;
- tab = temp;
- }
- ++dlugosc;
- }
- void FigureTab::usunWszystko() {
- for (int i{}; i < dlugosc; ++i) {
- delete tab[i];
- }
- delete[] tab;
- tab = nullptr;
- dlugosc = 0;
- }
- ostream& operator<<(ostream& os, const FigureTab& ob) {
- if (ob.dlugosc == 0) {
- os << "Empty tab";
- }
- else {
- for (auto i{1}; i <= ob.dlugosc; ++i) {
- os << ob[i]->print() << '\t';
- }
- }
- return os;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement