Code reuse in C++

Discussion in 'C++' started by DPfan, Oct 26, 2003.

  1. DPfan

    DPfan Guest

    What's exactly the meaning of "code reuse" in C++?

    Why such kind of reuse have more advantages over the counterpart in other
    language like in C?

    How is "code reuse" realized in C++? By composition mainly? What're
    others?

    Thanks in advance for your comments!
     
    DPfan, Oct 26, 2003
    #1
    1. Advertising

  2. "DPfan" <> wrote in message
    news:McVmb.15536$...
    > What's exactly the meaning of "code reuse" in C++?


    I would say the best example of code reuse is the C++
    library.

    > Why such kind of reuse have more advantages over the
    > counterpart in other language like in C?


    Reuse is good in any language. C++ just happens to make
    it fairly easy in some cases.

    > How is "code reuse" realized in C++? By composition
    > mainly? What're others?


    The best kind of code reuse is realized in template libraries,
    where types don't have to be rewritten because of minor
    dependencies.

    Dave



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
     
    David B. Held, Oct 26, 2003
    #2
    1. Advertising

  3. DPfan

    rossum Guest

    On Sun, 26 Oct 2003 19:25:32 GMT, "DPfan" <> wrote:

    >What's exactly the meaning of "code reuse" in C++?
    >
    >Why such kind of reuse have more advantages over the counterpart in other
    >language like in C?
    >
    >How is "code reuse" realized in C++? By composition mainly? What're
    >others?
    >
    >Thanks in advance for your comments!
    >

    Classes: not only can old code be used in the normal way, by including
    an existing class in your code. You can also get the old code in the
    class to call a new function without having to rewrite the old code.
    As long as the function is declared virtual in the old code then the
    old code will happily call the new function if you provide a new
    version of the virtual function.

    rossum

    --

    The Ultimate Truth is that there is no Ultimate Truth
     
    rossum, Oct 26, 2003
    #3
  4. DPfan

    lilburne Guest

    DPfan wrote:

    > What's exactly the meaning of "code reuse" in C++?


    Code reuse is something you have to consider in the design
    of the components you are building. In particular you need
    to think in terms other than your immediate needs, and
    devise code that does only one particular job. The standard
    C library is good example of code that can be reused, as is
    the UNIX text utilities.

    But you must realise that there is a cost involved in
    writing code that can be reused. Just by writing in C++
    won't guarantee that the code you write is reusable.

    > Why such kind of reuse have more advantages over the counterpart in other
    > language like in C?


    In C the things that you can reuse are functions, and to
    some extent data structures. C++ gives you other elements
    like classes, templates, and polymorphism. You therefore
    have a richer set of things that you can reuse.

    > How is "code reuse" realized in C++? By composition mainly? What're
    > others?


    I'll thow polymorphism into the pot. Say you have a base
    class Person from which you can obtain the city where the
    person lives and you want to find all the instances of
    Person in a list that live in city X. Write a function to do
    that is pretty simple either in C or C++ but not
    particularly reusable, because you can only process Person
    objects. Now consider that your list contains pointers to
    instances of Person rather than Person objects directly
    (perhaps using some smart pointer type). Now someone having
    derived a class Employee from Person could build a list of
    employees which they pass to the find function. The function
    is now being reused to find employees. Perhaps Person is
    used to derive a class Player in some game scenario, your
    find function will still work. In fact it will still work
    given a list of Employees and Players.

    And there is a deeper reuse going on here, which is one of
    the main rationals for OO, the reuse of the concept Person.
    Personally I always prefer to see code that operates on the
    root of a class heirarchy, I'd much rather code in terms of
    Curve or Surface, than Bezier or SweptSurface. The result is
    far more reusable.
     
    lilburne, Oct 26, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    5
    Views:
    535
    Steve C. Orr [MVP, MCSD]
    May 17, 2005
  2. Randall Parker
    Replies:
    2
    Views:
    493
    intrader
    Nov 1, 2005
  3. Hylander

    To reuse or not to reuse....

    Hylander, Feb 26, 2004, in forum: Java
    Replies:
    0
    Views:
    422
    Hylander
    Feb 26, 2004
  4. code reuse and design reuse

    , Feb 7, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,025
    Malcolm
    Feb 12, 2006
  5. jacob navia

    To reuse or not to reuse

    jacob navia, Nov 5, 2006, in forum: C Programming
    Replies:
    19
    Views:
    530
    Dave Thompson
    Dec 18, 2006
Loading...

Share This Page