Stworzyłem klasę Cint przechowującą dwa inty (a,b)
I klase Ctab która składa się z obiektów Cint.
Mój kod wygląda tak
#include <iostream>
using namespace std;
class Cint{
public:
int a,b;
Cint(char _a,int _b):a(_a),b(_b){}
Cint():a(0),b(0){}
void wypisz(){cout<<"A "<<a<<" B "<<b<<"\n";}
};
class Ctab{
public:
Cint *tab;
int numbers_of_objects;
Ctab():numbers_of_objects(0){}
void wypiszo(){
for(int i=0;i<numbers_of_objects;i++){
tab->wypisz();
}
}
void dodaj(Cint o){
Cint *buff=new Cint[numbers_of_objects];
for(int i=0; i<numbers_of_objects; i++){
buff[i]=tab[i];
}
tab = new Cint[numbers_of_objects+1];
for(int i=0; i<numbers_of_objects; i++)
tab[i]=buff[i];
tab[numbers_of_objects]=o;
delete []buff;
numbers_of_objects++;
}
};
int main(){
Cint a(1,2),b(10,20);
Ctab object;
object.dodaj(a);
object.dodaj(b);
object.wypiszo();
return 0;
}
Jak wypisać zmienne a,b wszystkich obiektów znajdujących się w klasie Ctab?
void wypiszo(){
for(int i=0;i<numbers_of_objects;i++){
tab->wypisz();
}
}
próbowałem dodać do tab=tab+2*4 (klasa cint to dwa inty), lecz to nie pomaga,
Niestety nie mogę używać vector,list itd, tylko muszę dynamiczne przydzielać pamięć