Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Combination or inheritance

Combination or inheritance

Scheduled Pinned Locked Moved C / C++ / MFC
ooptutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 元 Offline
    元 Offline
    元昊 潘
    wrote on last edited by
    #1

    In object-oriented programming, why is combination better than inheritance in code reuse, for example, given the coordinates of two points to calculate the distance between two points, should the Distance class be inherited from the Point class or an object of the Point class as a data member of the Distance class?

    L D 2 Replies Last reply
    0
    • 元 元昊 潘

      In object-oriented programming, why is combination better than inheritance in code reuse, for example, given the coordinates of two points to calculate the distance between two points, should the Distance class be inherited from the Point class or an object of the Point class as a data member of the Distance class?

      L Offline
      L Offline
      leon de boer
      wrote on last edited by
      #2

      The question comes down to is distance fundemental to point or is it a function executed and that is up to how you intend to use it. The general rule is function over inheritance and it's clearly covered by wikipedia Composition over inheritance - Wikipedia[^] If point has shapes that derive from it and distance is the closest point between the two shapes then we can provide a clear implementation of how it must work. The only way to reconcile the closest point between two complex shapes will be to run each geometry (line,arc,spline etc) of each shape against the other shape or scanline the two shapes in a minimal box that includes both shapes. Either of those two processes will require these functions 1.) reset or seek so a geometry index will be the next out (seek_geometry(i)) 2.) retrieve the next geometry from the shape (read_geometry(i)) 3.) check if this geometry was last, if not back to 2 we go (check_geometry_end). Essentially the geometry callback function(s) will be inherited from point but the distance will be another object because it will take two objects and run the process. Distance can then evolve to take a list of objects or different sorts of objects. It is easy to see in that situation distance is not really a function of a shape it is a function between two shapes. However it could also be between a shape and something else such as a bounding box or grid or something yet to be thought of. So from a flexibility and reuse point of view you code for functionality and make point and distance two different objects. In the simplest example it would make little difference but the functionality coding would make it easier to extend, reuse and more flexible. Ultimately you are more likely to reuse the more flexible code.

      In vino veritas

      1 Reply Last reply
      0
      • 元 元昊 潘

        In object-oriented programming, why is combination better than inheritance in code reuse, for example, given the coordinates of two points to calculate the distance between two points, should the Distance class be inherited from the Point class or an object of the Point class as a data member of the Distance class?

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        元昊 潘 wrote:

        ...should the Distance class be inherited from the Point class or an object of the Point class as a data member of the Distance class?

        Is a vs. Has a

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups