CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2002
    Location
    india
    Posts
    15

    nesting of classes and inheritance

    What is the use of nested classes?
    My doubt is whatever a nested class provides is provided by inheritance. But whatever inheritance provides is not provided by nested classes(such as run time polymorphism and virtual functions). Also imparting properties of a class to another classes through inheritance is easier than using nested classes.
    Why then we need nested classes?

  2. #2
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715
    Nesting classes provide versitility just like inheritance does but it isn't the same kind of versitility.

    If you had a hammer tool class then your hammer tool class could make use of all different' types of nail classes as well as perhaps some screw classes. Here your nail classes might all inherit their hammer interface from a parent class but that parent class wouldn't be a hammer. Get it?

    Can think of many examples but the basic truth is you can probable make due with Inheritance without nesting classes for some logic problems. But having both relationships availible and the knowledge of how and when to use both will enable you to write clearer more understandable and maintainable code.

    Objects can have different types of relationships to each other. Some objects are made up of other objects, like a house made of bricks. And some objects which don't exist at the same time can still be associated to each other through inheritance.

    It's the clasic "is a" or "has a" paradiam.

  3. #3
    Join Date
    May 2002
    Location
    Quebec City, Canada
    Posts
    374

    Excuse my ignorance, but...

    Is there another word for "nested class" I haven't read about this concept in those words.
    Martin Breton
    3D vision software developer and system integrator.

  4. #4
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715
    Another name for nested class is "has a". In object orienented terminalogy what he is describing is the difference between "is a" and "has a". A horsie "is a" mamal. A horsie "has a" tail.

  5. #5
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880

    Re: Excuse my ignorance, but...

    Originally posted by proxima centaur
    Is there another word for "nested class" I haven't read about this concept in those words.
    I don't know about another name. I have heard his name already for the same concept. It is just the definition of a class inside another class.

    It is not always very nice to read such a code as the definition of the class is bigger, but the concept is interesting and can lead to a conception easier to read.
    Elrond
    A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
    -- George Steiner

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured