Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 08:40 PM
DPfan
Guest
 
Posts: n/a
Default Code reuse in C++

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!


  #2  
Old July 19th, 2005, 08:40 PM
David B. Held
Guest
 
Posts: n/a
Default Re: Code reuse in C++

"DPfan" <DPfan@yahoo.com> wrote in message
news:McVmb.15536$Ec1.1411991@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
> What's exactly the meaning of "code reuse" in C++?[/color]

I would say the best example of code reuse is the C++
library.
[color=blue]
> Why such kind of reuse have more advantages over the
> counterpart in other language like in C?[/color]

Reuse is good in any language. C++ just happens to make
it fairly easy in some cases.
[color=blue]
> How is "code reuse" realized in C++? By composition
> mainly? What're others?[/color]

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


  #3  
Old July 19th, 2005, 08:40 PM
rossum
Guest
 
Posts: n/a
Default Re: Code reuse in C++

On Sun, 26 Oct 2003 19:25:32 GMT, "DPfan" <DPfan@yahoo.com> wrote:
[color=blue]
>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!
>[/color]
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
  #4  
Old July 19th, 2005, 08:40 PM
lilburne
Guest
 
Posts: n/a
Default Re: Code reuse in C++

DPfan wrote:
[color=blue]
> What's exactly the meaning of "code reuse" in C++?[/color]

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.
[color=blue]
> Why such kind of reuse have more advantages over the counterpart in other
> language like in C?[/color]

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.
[color=blue]
> How is "code reuse" realized in C++? By composition mainly? What're
> others?[/color]

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.

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles