Let's say I have a class called "CDlibrary", and inside this class, I have a class called "CD" as a private member. An instance of CDlibrary can contain multiple CDs. I don't know how many CDs I have before I initialize the CDlibrary instance. How do I design the CDlibrary class so that different instances can contain different # of CDs?
eg. If every library has 1 CD then:
class CDlibrary {
private:
CD disc1;
};
Thanks for your help in advance.
You could try declaring a pointer to an array of class CD. In the class constructor for CDLibrary, you can initialize the pointer to null. Then, when you know how many CDs you have, you can allocate the memory as needed. Also, in your destructor, you can check if you allocated memory to hold the CDs or not and destroy the array appropriately E.g.
class CDlibrary {
private:
CD *discs;
int nDiscs;
public:
CDLibrary();
~CDLibrary();
void CreateCDs(int num);
};