Ahoj,
jsem začátečník a snažím se mít jednoho předka a z něj dělat potomky. Tyto potomky chci vkládat do jednoho seznamu a pak mít možnost je z něj zase vytahovat. Chci se zeptat, jestli si myslíte, že to nějak lze. Uvádím můj pokus, který snad jasně ukazuje o co mi jde, ale nefunguje mi :-(
Díky za každé rady a tipy.
#include <cstdlib>
#include <iostream>
#include <list>
#include <string.h>
using namespace std;
class Tvar {
protected:
int x, y;
public:
Tvar() {x = 0; y = 0;} ;
int getX() {return x;};
int getY() {return y;};
};
class Obdelnik: public Tvar {
int sirka, vyska;
public:
Obdelnik(const int &sx, const int &sy, const int &sir, const int &vys) {
x = sx;
y = sy;
sirka = sir;
vyska = vys;
};
int getSirka() {return sirka;};
};
int main(int argc, char *argv[])
{
list<Tvar> seznam;
Obdelnik obd(2, 0, 0, 0);
seznam.push_back(obd);
Obdelnik obd1(3, 1, 1, 1);
seznam.push_back(obd1);
list<Tvar>::iterator it;
for (it=seznam.begin(); it!=seznam.end(); it++) {
Obdelnik *obd3 = (Obdelnik*) &*it;
cout << obd3->getSirka();
}
system("PAUSE");
return EXIT_SUCCESS;
}
Fórum › C / C++
Ukládání potomků jedné nadtřídy do jednoho seznamu

Přikládám moje druhé řešení pomocí pointerů. Tady ale zase narážím na problém, že se mi ruší mnou vytvořené objekty a pak seznam ukazuje na neplatné pointery. Už si nevím rady.
#include <cstdlib>
#include <iostream>
#include <list>
#include <string.h>
using namespace std;
class Tvar {
protected:
int x, y;
public:
Tvar() {x = 0; y = 0;} ;
int getX() {return x;};
int getY() {return y;};
};
class Obdelnik: public Tvar {
int sirka, vyska;
public:
Obdelnik(const int &sx, const int &sy, const int &sir, const int &vys) {
x = sx;
y = sy;
sirka = sir;
vyska = vys;
};
int getSirka() {return sirka;};
~Obdelnik() {cout << "zrusen";};
};
class Seznam {
private:
list<Tvar*> seznam;
public:
Seznam() {};
void vloz(Obdelnik &obd) {
seznam.push_back(&obd);
};
void vyjmi() {
list<Tvar*>::iterator it;
for (it=seznam.begin(); it!=seznam.end(); it++) {
Obdelnik *obd = (Obdelnik*) *it;
cout << obd->getSirka();
}
}
};
int main(int argc, char *argv[])
{
int poradi = 0;
// toto funguje, ale je to neprakticke na pouziti
// Obdelnik obd(2, 0, 0, 0);
// sez.vloz(obd);
// Obdelnik obd2(2, 0, 1, 0);
// sez.vloz(obd2);
// Obdelnik obd3(5, 0, 5, 0);
// sez.vloz(obd3);
// sez.vyjmi();
// po provedeni tohoto jsou spatne pointery, protoze se objek Obdelnik prubezne rusi, jak to vyresit?
Seznam sez;
for (poradi = 0; poradi < 5; poradi++) {
Obdelnik obd(2, 0, poradi, 0);
sez.vloz(obd);
};
sez.vyjmi();
system("PAUSE");
return EXIT_SUCCESS;
}
pokud vim tak kdyz tu rodicovskou tridu pouzijes jako prvek seznamu melo by do toho jit nacpat i potomky .. potom je ale musis umet zase vytahat.. a taky asi pro pouzivany metody ktery se jmenujou vsude stejne pouzit virtual - aby se pouzila metoda te tridy kterou zrovna zpracovavas a ne ta rodice

Přidej příspěvek
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
































Uživatelé prohlížející si toto vlákno
Podobná vlákna
Přidání seznamů v seznamu do 2.seznamu — založil Ahoj3
Z jednoho řetězce tři — založil SuperAdmin
Sloučení obrázků do jednoho — založil Lolo24
Spojení příkazů do jednoho — založil Lusk
Kontrola jednoho bloku sudoku — založil Petr
Moderátoři diskuze