Help implementing a dynamically allocated array of objects.

Removed
Last edited on
Its makes more sense for the addProduct function to take a Product object as a parameter. You can build this Product object using the constructor in the product class. Then call the addProduct function, sending it the Product object. This Product object is then added to the productList array.

Also:
You don't need to make the Catalog class a 'friend' of the Product class.
A destructor is not necessary in the Product class.
Last edited on
Make sure you follow the rule of three when implementing a class that has pointer variables (You do in the Catalog class).
https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)

Basically states that if you need to provide your own definition for one of the following three, you need to provide your own definition for the other 2 as well.
1. Destructor
2. Copy Constructor
3. Copy Assignment

Destructor is necessary in Catalog to prevent memory leak. Copy constructor and copy assignments are necessary to prevent shallow copies.
http://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy

With C++11, there is rule of five, which includes Move constructor and Move Assignment. It's not absolutely necessary though.
http://en.cppreference.com/w/cpp/language/rule_of_three
Unlike Rule of Three, failing to provide move constructor and move assignment is usually not an error, but a missed optimization opportunity


[EDIT] By the way, you need to increment numProducts whenever you add a product because as far as I can tell, int numProducts keeps track of how many products the Catalog has. Once numProducts equals maxProducts, it means the Catalog is full and user should have the option of "increasing" maxProducts or simply leave the catalog as full depending on your assignment requirements.

Also, in the Default Constructor of Catalog, you set productList as nullptr and in void addProduct, you are trying to dereference nullptr (This will crash the program). Probably better if you set a default maxProducts (Whether it be 10 or whatever arbitrary number you set) and allocate memory in your default constructor. Of course, numProducts will be 0 since default constructed catalog would not have any products added.

If you are against the idea of "default maxProducts", then make sure you check for nullptr in addProduct function before you derefence it.
Last edited on
Thank you for the guidance! let's see what I can do with it. Really appreciate the detailed response mpark4656

EDIT:

So, in my default constructor for Catalog, allocate the dynamic Catalog array of Product objects.
Using a default maxProducts value that I can initialize in the private member definitions(to say 10)?

I know I should have a constructor that receives maxProducts value from somewhere else, any ideas what should be done there? Simply set the maxProducts member to the actual max size specified elsewhere?
Last edited on
That makes sense Arslan7041, the parameter for the addProduct function should be an arbitrary Product class object(using a constant reference(as alias))?

Am I assigning the members of the Product object correctly? I don't really understand how I am accessing each product element in the Catalog array. Should I be using a for loop, and increment numProducts there... Or use a recursive function and increment numProducts at the end of the function?

EDIT:
Should the assignment in addProducts remain the same, increment numProducts, and then be called from function that has a loop that assigns each element elsewhere?

Also, it seems the addProduct function cannot access the Product data members otherwise...

Thank you so much.
Last edited on
As far as the constructors for Catalog goes, would it be correct to:

Create a default constructor to... initialize maxProducts with a default size?

Create a copy constructor that accepts the address of one of it's objects, makes a deep copy of the catalog array dynamically.

Create a second constructor that accepts a value to spec the maximum number of products in the catalog, then dynamically allocate memory for that maximum.

Still having trouble understanding the true purpose of a copy constructor...
I would suggest the following:

(1) In the private section of your Catalog class, set maxProducts to some maximum number (say 50, for example).

(2) In your Catalog default constructor, dynamically allocate the array 'productList' of size 'maxProducts', and set 'numProducts' to 0.

(3) Your addProduct() function should look like this:

1
2
3
4
5
6
7
8
9
   void addProduct(Product& p) {
           
       if( !isFull() )
       {
            productList[numProducts] = p;
            numProdcuts++;
       }
       else //throw and overflow exception
   }


You can also choose to "increase" the size of your array once it is full instead of throwing an exception. You would have to double maxProducts, allocate a new array of size maxProducts, and copy over everything from the original array. Once this is done, you'll have the space available to add additional 'Product' objects to the array.

(4) Create a copy constructor for the Catalog class.

(5)
I know I should have a constructor that receives maxProducts value from somewhere else

I don't think that should be necessary. The maximum capacity of the catalog is not something the client code should be able to decide upon construction.
Last edited on
Might wanna change the price in the product constructor from 0 to 0.0 since its a double
-Dr. Panagadan
Topic archived. No new replies allowed.