hello
first of all, I realy would like to thank you people for this awsome site..
second of all, my problem with c++
lol, it's classes.. very confused on how to manage things between them
on the code I'm trying to count the products on my vector by using NumberOfInstances, but it gives me wrong amount and I have no idea why!!
any ideas please??
PS! the main program should stay untouched I can only modify my class
Vectors have a member function .size() which tells you how many elements are in the vector, so you could use a vector <product> container to hold the products.
However, you don't seem to be using the vector the way you think you are:
1 2 3 4 5
for( int i = 0; i < sizeof(teaName)/sizeof(string); i++ ) //Oo, sizeof(string) won't give you the size of the string you want, since your strings are different lengths, you have to iterate through the array yourself using 5 or something
{
Product* tea = new Product( teaName[i], 22, "Gotländskt te" );
teas.push_back( tea );
}
Thanx alot for the replay
the thing is, the main program is not mine and I'm not alloed to change it else I would do things very deferently
Anyway, I will try to check if things can be easyer with .size() as you suggested and will replay here with the results..
until then.. have anice time
I had a problem to get the right number of products.. the only way was to intialize it to 3 to get the right number.. but now I think I figured it out
I don't realy understand why but this is what I did in Product.cpp
There you go, that's more like it. Here's the why part: Your counter should be initialized to 0 before any objects are created. In all constructors increment the counter, because an object is being created. In the destructor, decrement the counter because an object is being destroyed. That way, the counter should always hold the current number of objects.
Now, one last comment. Note that you did not include a default constructor or a copy constructor. In the event that your class is constructed without arguments or copied from another object, the instance counter will not be increased. (Remember, those two constructors will be generated automatically by the compiler if you do not explicitly declare them and the compiler will not increment your instance counter)
wow.. thanx alot man.. nice name and it suites you to be hounest
but I think you are talking about something more advance than what I learned until now:
you said "Note that you did not include a default constructor or a copy constructor."
what do you mean? how should they look like?
To see an example of how your instance count can be "broken", try this in main:
1 2 3 4 5 6 7 8 9 10 11 12
Product p;
/*
At this point there IS a Product object that was created, but because it
was not constructed with any arguments, it did not call the constructors that
you have explicitly defined. In that case, you will still have an instance count
of 0.
*/
Product p2( p );
/*
Now, another Product object is created that is a copy of p. Once again, the
instance count is still 0.
*/
Now, without the following two signatures, the compiler will generate them.
In the case above, I declared them but didn't implement them... In their
definition you would want to increment the instance count and do whatever it is that you do to initialize a Product.