-
July 8th, 2009, 08:09 AM
#1
array of pointers to class
Hello! I need help about this problem. I got 4 class, one base class 'Vozilo' (Vehicle), and derived classes 'Avtobus' (Autobus), 'Avtomobil' (Car), 'Kamion' (Truck).
Code:
#include<iostream>
#include<string.h>
using namespace std;
class vozilo
{
protected:
char marka[20], reg_broj[20];
int god_proizvodstvo, datum_reg;
public:
vozilo (char Marka[], char registracija[], int proizvodstvo, int Registracija)
{
strcpy(marka, Marka);
strcpy(reg_broj,registracija);
god_proizvodstvo=proizvodstvo;
datum_reg=Registracija;
}
virtual void prikazi()
{
cout<<"Marka: "<<marka<<endl;
cout<<"Registarski broj: "<<reg_broj<<endl;
cout<<"Godina na proizvodstvo: "<<god_proizvodstvo<<endl;
cout<<"Datum na registracija: "<<datum_reg<<endl;
}
};
class avtomobil : public vozilo
{
int br_sedista;
public:
avtomobil (char Marka[], char registracija[], int proizvodstvo, int Registracija, int sedista) : vozilo (Marka, registracija, proizvodstvo, Registracija)
{
br_sedista=sedista;
}
void prikazi()
{
cout<<"***AVTOMOBIL***"<<endl;
vozilo::prikazi();
cout<<"Broj na sedista: "<<br_sedista<<endl;
}
};
class kamion : public vozilo
{
double nosivost;
public:
kamion (char Marka[], char registracija[], int proizvodstvo, int Registracija, double Nosivost) : vozilo (Marka, registracija, proizvodstvo, Registracija)
{
nosivost=Nosivost;
}
void prikazi()
{
cout<<"***KAMION***"<<endl;
vozilo::prikazi();
cout<<"Nosivost (t): "<<nosivost<<endl;
}
};
class avtobus : public vozilo
{
int br_patnici;
public:
avtobus (char Marka[], char registracija[], int proizvodstvo, int Registracija, int patnici) : vozilo (Marka, registracija, proizvodstvo, Registracija)
{
br_patnici=patnici;
}
void prikazi()
{
cout<<"***AVTOBUS***"<<endl;
vozilo::prikazi();
cout<<"Broj na patnici: "<<br_patnici<<endl;
}
};
int n=0;
int br_kola=0, br_kamion=0, br_bus=0;
void postavi(vozilo *v[])
{
char marka[20], reg_broj[20];
int izbor,god_proizvodstvo, dat_reg, avt_sedista, avt_patnici;
double kamion_nosivost;
cout<<"Vnesuvanje novo vozilo"<<endl;
cout<<"--------------------------------------"<<endl;
cout<<"1. Avtomobil"<<endl;
cout<<"2. Kamion"<<endl;
cout<<"3. Avtobus"<<endl;
cout<<"4. Nazad kon glavnoto meni"<<endl;
cout<<endl;
cin>>izbor;
cout<<"Marka: ";
cin>>marka;
cout<<"Registarsk broj: ";
cin>>reg_broj;
cout<<"Godina na proizvostvo: ";
cin>>god_proizvodstvo;
cout<<"Datum na registracija: ";
cin>>dat_reg;
switch (izbor)
{
case 1:
n++;
br_kola++;
cout<<"Broj na sedista: ";
cin>>avt_sedista;
v[n]=new avtomobil(marka, reg_broj,god_proizvodstvo, dat_reg, avt_sedista);
cout<<endl;
break;
case 2:
n++;
br_kamion++;
cout<<"Nosivost: ";
cin>>kamion_nosivost;
v[n]=new kamion(marka, reg_broj, god_proizvodstvo, dat_reg, kamion_nosivost);
cout<<endl;
break;
case 3:
n++;
br_bus++;
cout<<"Broj na patnici: ";
cin>>avt_patnici;
v[n]=new avtobus(marka, reg_broj, god_proizvodstvo, dat_reg, avt_patnici);
cout<<endl;
break;
case 4:
break;
}
}
int main()
{
int kod;
vozilo *v[100];
do
{
cout<<endl;
cout<<"Odberete funkcija od meni-to"<<endl;
cout<<"---------------------------------"<<endl;
cout<<"1. Dodavanje novo vozilo"<<endl;
cout<<"2. Prikazuvanje na listata na vozila"<<endl;
cout<<"3. Prikazuvanje na brojnata sostojba"<<endl;
cout<<"4. Izlez"<<endl;
cin>>kod;
switch(kod)
{
case 1:
postavi(v);
break;
case 2:
int i;
cout<<"Prikazuvanje na vnesenite vozila"<<endl;
cout<<"---------------------------------"<<endl;
cout<<endl;
for (i=1; i<=n; i++)
{
v[i]->prikazi();
cout<<endl;
}
cout<<endl;
break;
case 3:
cout<<"Prikaz na brojanta sostojba"<<endl;
cout<<"---------------------------------"<<endl;
cout<<"Broj na avtomobili: "<<br_kola<<endl;
cout<<"Broj na kamioni: "<<br_kamion<<endl;
cout<<"Broj na avtobusi: "<<br_bus<<endl;
cout<<"--------"<<endl;
cout<<"Vkupen broj na vozila: "<<n<<endl;
cout<<endl;
break;
case 4:
break;
}
} while (kod!=4);
system("PAUSE");
return 0;
}
Why the pointer is not destoryed here, and the program works perfectly:
Code:
void postavi(vozilo *v[])
{
char marka[20], reg_broj[20];
int izbor,god_proizvodstvo, dat_reg, avt_sedista, avt_patnici;
double kamion_nosivost;
cout<<"Vnesuvanje novo vozilo"<<endl;
cout<<"--------------------------------------"<<endl;
cout<<"1. Avtomobil"<<endl;
cout<<"2. Kamion"<<endl;
cout<<"3. Avtobus"<<endl;
cout<<"4. Nazad kon glavnoto meni"<<endl;
cout<<endl;
cin>>izbor;
cout<<"Marka: ";
cin>>marka;
cout<<"Registarsk broj: ";
cin>>reg_broj;
cout<<"Godina na proizvostvo: ";
cin>>god_proizvodstvo;
cout<<"Datum na registracija: ";
cin>>dat_reg;
switch (izbor)
{
case 1:
n++;
br_kola++;
cout<<"Broj na sedista: ";
cin>>avt_sedista;
v[n]=new avtomobil(marka, reg_broj,god_proizvodstvo, dat_reg, avt_sedista);
cout<<endl;
break;
case 2:
n++;
br_kamion++;
cout<<"Nosivost: ";
cin>>kamion_nosivost;
v[n]=new kamion(marka, reg_broj, god_proizvodstvo, dat_reg, kamion_nosivost);
cout<<endl;
break;
case 3:
n++;
br_bus++;
cout<<"Broj na patnici: ";
cin>>avt_patnici;
v[n]=new avtobus(marka, reg_broj, god_proizvodstvo, dat_reg, avt_patnici);
cout<<endl;
break;
case 4:
break;
}
}
Shouldn't be vozilo &*p[] ??
Thanks in advance
-
July 8th, 2009, 08:22 AM
#2
Re: array of pointers to class
I could be wrong:
(1) What you are passing is not a pointer to an array, but an array of pointers, so:
(2) In c++ arrays are always implicitly passed by reference.
I'd like someone more experienced to confirm though.
-
July 8th, 2009, 08:27 AM
#3
Re: array of pointers to class
Your program does not work perfectly, it leaks memory. While this may not be noticable during short tests it certainly is a defect.
When passing arrays to a function, arrays degenerate to pointers to their first element. There is no need to pass these pointers by reference as the pointers themselves are not changed by the function, neither would it be faster.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
July 8th, 2009, 08:47 AM
#4
Re: array of pointers to class
Originally Posted by monarch_dodra
I could be wrong:
(1) What you are passing is not a pointer to an array, but an array of pointers, so:
(2) In c++ arrays are always implicitly passed by reference.
I'd like someone more experienced to confirm though.
Oh.. I missed that. Thank you very much.
Originally Posted by treuss
Your program does not work perfectly, it leaks memory. While this may not be noticable during short tests it certainly is a defect.
When passing arrays to a function, arrays degenerate to pointers to their first element. There is no need to pass these pointers by reference as the pointers themselves are not changed by the function, neither would it be faster.
Why my program is not working perfectly?
Also is it same *& and &* ??
-
July 8th, 2009, 08:52 AM
#5
Re: array of pointers to class
Originally Posted by StGuru
Why my program is not working perfectly?
As stated: It leaks memory. You are allocating memory using new but you never free that memory again. Additionally you should look up the importance of virtual destructors.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
July 8th, 2009, 09:04 AM
#6
Re: array of pointers to class
Originally Posted by StGuru
Also is it same *& and &* ??
*& is a reference to a pointer, and is usually used as function argument.
&* is a pointer to a reference. This does not make sense and doesn't compile
Code:
void ptrfun(int *& ip)
{
ip = new int;
}
standard example of pointer passed as reference.
As a rule of thumb, try reading Right to Left:
int * & p
P is a REFERENCE to a POINTER of type INT.
This is especially helpful if arrays or the const keyword get involved.
-
July 8th, 2009, 10:35 AM
#7
Re: array of pointers to class
Thanks for the replies.
So &* makes no sense, right?
And why I need to delete it? Doesn't it deletes automatically?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|