I am getting crazy

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef PRODUCT_H
#define PRODUCT_H
#pragma once

#include <string>
#include <vector>
using namespace std;

class Product
{
public:
	//Product(void);
	static int getNumberOfProducts();
	Product (string name, double price = 10, string description = "");
	~Product(void);
	string getName();
	void setName(string name);
	string getDescription();
	void setDescription(string description);
	double getPrice();
	void setPrice(double price);

private:

	static int NumberOfInstances;
	string mName;
	double mPrice;
	string mDescription;
	
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "Product.h"
#include <iostream>
using namespace std;
 
int Product::NumberOfInstances = 3;

//Product::Product(void)
//{
//}
///////////////////////////////////////////
Product::Product(string name, double price, string description):mName(name), mPrice(price), mDescription(description)
{
	NumberOfInstances++;
}
///////////////////////////////////////////
Product::~Product(void)
{
	NumberOfInstances--;
}
///////////////////////////////////////////
string Product::getName()
{
	return mName;
}
///////////////////////////////////////////
void Product::setName(string name)
{
	mName = name;
}
///////////////////////////////////////////
double Product::getPrice()
{
	return mPrice;
}
///////////////////////////////////////////
void Product::setPrice(double price)
{
	mPrice = price;
}
///////////////////////////////////////////
string Product::getDescription()
{
	return mDescription;
}
///////////////////////////////////////////
void Product::setDescription(string description)
{
	mDescription = description;
}
///////////////////////////////////////////
int Product::getNumberOfProducts()
{
    return NumberOfInstances;
}
/////////////////////////////////////////// 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <vector>
#include "Product.h"

using namespace std;

// show one produkt
void displayProduct( Product& product );

// show vektor with produkts
void displayProducts( vector<Product*> products );

// show advertisment
void advertiseCheap( Product product );

int main()
{
	
    // create base products

    Product pineapple = Product( "pineapple" );
    Product milk = Product( "Milk", 7 );
    Product mustard = Product( "Gotlandssenap", 35, "very nice" );

    // show all products
    displayProduct( pineapple );
    displayProduct( milk );
    displayProduct( mustard );

    // Now we have 3 products
    cout << "Now we have " << Product::getNumberOfProducts() << " products." << endl << endl;

    // add some kind of teas
    string teaName[] = {"Guteblandning", "Kalkstensdrömmar", "Munkte", "Gotländsk sommarblandning", "Sylves lilla gröna", "Den Raude"};
    vector<Product*> teas;
    for( int i = 0; i < sizeof(teaName)/sizeof(string); i++ )
    {
        Product* tea = new Product( teaName[i], 22, "Gotländskt te" );
        teas.push_back( tea );
    }

    // show our teas
    displayProducts( teas );
    // Now we have 9 products
    cout << "Now we have " << Product::getNumberOfProducts() << " products." << endl << endl ;

    // get rid of teas
    cout << "Take away all the tea we have" << endl;
    for( vector<Product*>::iterator tea = teas.begin(); tea != teas.end(); tea++ )
    {
        delete *tea;
    }
    // Now we have 3 products
    cout << "Now we have " << Product::getNumberOfProducts() << " products." << endl << endl;

    // advertise our pineapple
    pineapple.setPrice( 15 );
    pineapple.setDescription( "TRY IT! TRY IT! VERY CHEAP!" );
    advertiseCheap( pineapple );

    // Now we have 3 products, extra check
    cout << "Now we have " << Product::getNumberOfProducts() << " products." << endl << endl;

}

void displayProduct( Product& product )
{
    cout << product.getName() << " cost " << product.getPrice() << " kr: " << product.getDescription() << endl;
}

void displayProducts( vector<Product*> products )
{
    for( vector<Product*>::iterator product = products.begin(); product != products.end(); product++ )
    {
        displayProduct( **product );
    }
}

void advertiseCheap( Product product )
{
    cout << "EXTRA! EXTRA!" << endl;
    cout << product.getName() << ": " << product.getDescription() << endl
         << "for the price of " << product.getPrice() << " kr" << endl
         << "Try it now!" << endl;
}


thanx in advance
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
Why is the static member initialized to 3 on line 5 of Product.cpp?
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int Product::NumberOfInstances = 0;

Product::Product(const Product& product)
{
mName = product.mName; 
mPrice = product.mPrice; 
mWieght = product.mWieght;
NumberOfInstances++;

}
/////////////////////////////////////////////
Product::Product(string name, double price, int wieght):mName(name), mPrice(price), mWieght(wieght)
{
	NumberOfInstances++;
}
/////////////////////////////////////////////
Product::~Product()
{
	NumberOfInstances--;
}


and it's working nice :) thanx for all help I got from guyz
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)
Last edited on
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?


thanx alot in advance
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.

1
2
3
4
5
6
class Product
{
public:
    Product();
    Product & Product( const Product & product );
};


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.
Last edited on
you people are simply GENIUS
thanx alot for the help
I will be back though :) like any other beginer


have a nice time
Topic archived. No new replies allowed.