Class inheritance VS. class collections

Posted on 2007-10-18
Last Modified: 2010-04-01

im having a bit of a problem trying to plan for my solution. its a very basic drawing program with shapes.

im not exactly sure which way to hit this, these are the elements i should have in there:
a shape class - base class for any inheritence
a point class - is a single point object, like a coordinate
a line class - is a line between two 'points'
a rectangle class - is a square (consists of four lines)
a polygon class - is a 'n' sided shape (consists of 'n' lines)

the model i have so far is something like this:

Shape Class                // Base class
 Point Class                 // Inherits from Shape
 Line Class                 // Inherits from Point
     |       |______________________
     |                                                   |
Rectangle Class                  Polygon Class                 // These classes both inherit from Line class

This is something along the lines of what i was thinking, but im also thinking it wont work very well because for example the 'Rectangles' class inherits from 'Line', however in reality the 'Rec' class should have a collection of 'Line' (4 lines in a square) right?

so in this case would everything just equally inherit from 'Shape' ? or will i be able to have collections of base classes in my sub (inherited) classes?

Question by:gem56
    LVL 28

    Accepted Solution

    It depends on how you want to use the objects. If you want to use polymorphism--i.e. Rectangle "is a" Shape, Polygon is a shape, Line is a Shape--and you have, for example, a collection of shapes and want to say "Draw all shapes from the collection, then each of your classes have to inherit from Shape.

    But it probably does not make sense to think in terms "line is a point". Rather, "line is a shape that CONSISTS of two points". This way Line should derive from Shape but it only USES the two intenal members of the Point class (if ever). But using the two Points internally is an implementation detail. It should not be visible on the Line object from the outside.

    The same way, Rectangle IS A Shape. But it is questionable whether using 4 points instead of 4 lines, or some other representation is not better or will not be better for future implementation. This way, Rectangle should derive from Shape but should ony USE the other classes (if any).

    In other words, when deciding whether to use inheritance or composition, think about the derived class that "it behaves almost the same except in some special cases, special sense".

    When in doubt (both solutions seems equal from the first point of view), it is recommended to use a composition instead of inheritance. It leads to simpler interfaces and easier modifications.


    Author Comment

    Hi pepr,

    yeah that seems more logical, looks like thats the way ill fly.

    Thanks for your help :)
    LVL 28

    Expert Comment

    You are welcome ;)    And thanks for <strike>all the fish</strike> the points...
    LVL 53

    Expert Comment

    >> because for example the 'Rectangles' class inherits from 'Line', however in reality the 'Rec' class should have a collection of 'Line' (4 lines in a square) right?

    As pepr already said : that's exactly why you shouldn't use inheritance between a rectangle and a line (for example). You can't say "a rectangle is a line".

    Anyway, an example of where you could use inheritance (I didn't say you have to, because that depends on what you want to do with the application) is :

            a square is a kind of rectangle
            a rectangle is a polygon

    As I said, that doesn't necessarily warrant inheritance, because the internal representation of a square can be very different from a rectangle (the size of a square can be represented with one value width, while a rectangle needs width and height for example).

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now