Hi, I'm doing an assignment, where we need to use a class Domain and correctly output the URL address.
The given class Domain is like this:
class Domain
{
public:
Domain(string lbl) : m_label(lbl) {}
string label() const { return m_label; }
const vector<Domain*>& subdomains() const { return m_subdomains; }
void add(Domain* d) { m_subdomains.push_back(d); }
~Domain();
private:
string m_label;
vector<Domain*> m_subdomains;
};
void listAllAuxiliary(string path, const Domain* d)
{
[B] You will write this code.[/B]
}
void listAll(const Domain* d)
{
listAllAuxiliary(d->label(), d);
}
int main()
{
Domain* d1 = new Domain("google");
d1->add(new Domain("map"));
d1->add(new Domain("image"));
d1->add(new Domain("orkut"));
Domain* d2 = new Domain("microsoft");
d2->add(new Domain("windows"));
d2->add(new Domain("msn"));
Domain* d3 = new Domain("com");
d3->add(d1);
d3->add(d2);
Domain* root = new Domain("");
root->add(d3);
listAll(root);
}
The output should look like this:
map.google.com
image.google.com
orkut.google.com
windows.microsoft.com
msn.microsoft.com
So, what I have so far is this, it compiles, but the output is incorrect:
void listAllAuxiliary(string path, const Domain* d)
{
path += d->label();
if (!path.empty())
{
cout << path << endl;
path += '.';
}
if ( !d->subdomains().empty())
{
const vector<Domain*> sd = d->subdomains();
for (size_t k = 0; k != sd.size(); k++)
listAllAuxiliary(path, sd[k]);
}
}
The output I'm getting is:
com
com.google
com.google.map
com.google.image
com.google.orkut
com.microsoft
com.microsoft.windows
com.microsoft.msn
Press any key to continue . . .
So, I'm not sure how to correct my code. I know it must be the ORDER in which I'm concatenating the label() to the path and COUTing it in the wrong order too. Any suggestions?