A document can have many containers and each container may or may not have sub-containers. Each container has name and container id.
In C++ I have modeled it as follows
class Container
{
string ContainerName;
int ContainerID;
int NumberofSubContainers; //number of sub-containers this contain contains
Container* subcontainerlist;
};
class Document
{
string DocumentName;
int NumofContainers; //number of containers document contains
Container* containerlist;
};
Is this approach correct or can there be a better alternative ?
It is better to use the containers provided by STL rather than describing your own (unless you have proven it to be unsuitable). If the (sub)containers are ordered, but not sorted by their ID, then a
vectorordequewould probably be good choices. All STL containers have asize()method that reports the number of elements held by the container.You also did not make any of your members public in your model. You will either need to make them public, or provide public accessors, or define friends. As a model, you should probably define public interfaces, so that you will be free to modify your implementation later while leaving your model intact.
In your model,
Documentlooks exactly like aContainerexcept for the ID, so it could be factored out.