Help with OSTREAM operator

May 21, 2012 at 8:16pm
I have a class, which has an object of another class in it.

Like this:

1
2
3
4
5
class parking
{
char adresa[50];
int cena;
vozilo *voz;


Now I have to write an operator that prints out all the VOZ'es that are contained in the 'parking' class. But I don't know how many VOZ-es are there.
The exercise asks to create an object of VOZILO inside the class PARKING, and create an += operator that adds new VOZ'es.

And then create a OSTREAM operator, that prints all the VOZ'es. But as I don't know the exact number, I've no idea how to do this ://

The exercise says that there is a maximum of 500voz'es to be added, but what if not all of them are filled? maybe only 10will be added?
Last edited on May 21, 2012 at 8:19pm
May 21, 2012 at 8:21pm
You shall keep in your class parking the total number of added VOZILO.
May 21, 2012 at 8:29pm
the number of VOZILO is 0 at the beggining, cause they are added after, using the += operator.

Is there a way to put a COUNTER somewhere, so I keep track on how many VOZILOs are added ?!
May 21, 2012 at 8:40pm
Yes of course, you can define a data member in your class parking that will store the number of added VOZILOs

By the way VOZILO - is word of which language?
May 21, 2012 at 8:47pm
it's in macedonian. it means CAR. I usually translate these parts, but I've no time, cause tomorow is the BIG day lol.

back to the problem.
I tried many ways to declare a counter inside the class, but none works :/

check:

http://i2.lulzimg.com/d032e55a36.jpg

I think the += operator is not declared properly neither , nor the counter :/
Last edited on May 21, 2012 at 8:48pm
May 21, 2012 at 8:50pm
What is the problem with declaring the counter?!

class parking
{
char adresa[50];
int cena;
size_t counter;
vozilo *voz;

Thoug the best way is to use std::vector<vozilo> instead of vozilo *voz
Last edited on May 21, 2012 at 8:52pm
May 21, 2012 at 8:53pm
I see someone did this too:
http://i2.lulzimg.com/4cc336ba47.jpg


if this is correct, that this should be acceptable in the exam, cause the one you mentioned surely won't, we haven't learned about those yet, so I can't use them.

May 21, 2012 at 9:00pm
If you may not use std::vector then use the pair the count and the pointer
Last edited on May 21, 2012 at 9:00pm
May 21, 2012 at 9:03pm
I'm afraid I didn't get you :/
Does the last screen shot work? Is that ok to do ?
May 21, 2012 at 9:08pm
As I understand you shall add to parking objects of type vozilo.

So your operator will look the following way

parking & operator +=( const vozilo & );

I'd like to ask is there such a name as Lopa in macedonian?
Last edited on May 21, 2012 at 9:09pm
May 21, 2012 at 9:10pm
Never heard of any.
But it LOPA means COW in albanian :P
May 21, 2012 at 9:18pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
parking & operator +=( const vozilo &rhs )
{
   vozilo *tmp = new vozilo[count+1];

   // or you can use std::copy( voz, voz + count, tmp );
   for ( size_t i = 0; i < count; i++ )
   {
      tmp[i] = voz[i];
   }
   tmp[count] = rhs;
   delete []voz;
   voz = tmp;
   count++;

   return ( *this );
} 


In your constructors you shall set count and voz to 0.
Last edited on May 21, 2012 at 9:24pm
Topic archived. No new replies allowed.