Very basic Nested Classes help

Thanks guys! I like the idea of making the display() a little more interesting. I'm reading up more on the relationships and purposes of nested classes. Trying to show how they are related in the program, I haven't yet come up with the idea, but I'll keep going at it.
Last edited on
> this is a homework assignment(...)
> I'm not even sure anything is wrong, but it seems so simple.
¿what's the assignment?

> My goal is to create a Egg class, nested in a Nest class, nested in a Hen class
¿what's the purpose? (¿what problem are you trying to solve?)

> My main simply creates a new instance, displays it, and then deletes
unnecessary dynamic memory allocation http://www.cplusplus.com/forum/general/138037/
1
2
3
4
{
   Hen hen1;
   hen1.display();
}
I have to create a Hen class, and inside that a Nest class, and inside that an Egg class. Each has a display() function, and each has a constructor and destructor that displays a message when called. In main() I have to create an instance of each using new, call the display, then free the storage using delete (hence the dynamic memory allocation).

I think I got this all right. But I can't begin to think of how to properly test it.
the display seems a little trivial in itself. I'd recommend designing a creative display
then it's fine, you're done.

the problem I have is that the assignment is not showing any porpuse of nesting. there are no relationships between the classes, you just write Hen::Nest::Egg instead of hen_egg.
compare against map::iterator to operate on `map' or list::node (hidden) to construct a `list'
Ok, so I have made these changes, but I want to make sure I understand nested classes.

1
2
3
removed.  figured things out.

}


What I would like to do is change the two bolded lines to somehow reference that is a nested class in the display() function. I don't know if this is possible with nested classes alone though. When I try, I get errors:

1
2
3
4
5
6
In member function 'void Hen::Nest::display()':
error: invalid use of non-static data member 'Hen::henName'
  cout << Hen::henName <<" lives in nest number " << nestID << "!\n";     // class Nest display() function
               ^~~~~~~
note: declared here
  string henName;     // name of Hen 


Which i believe is telling me that my object hen1, can't pass it's name to the display() function in the new object nest1. And that makes sense as there is nowhere in the code that seems to reference each other.
Last edited on
Topic archived. No new replies allowed.