473,386 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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!
Jul 19 '05 #1
3 6868
"DPfan" <DP***@yahoo.com> wrote in message
news:Mc*********************@bgtnsc05-news.ops.worldnet.att.net...
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
Jul 19 '05 #2
On Sun, 26 Oct 2003 19:25:32 GMT, "DPfan" <DP***@yahoo.com> 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
Jul 19 '05 #3
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.

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
6
by: patrick t music-images dt nl | last post by:
Hi, I'm evaluating Visual Studio and the .NET concept for his company, which is currently using Borland C++ Builder. Now I tried to create components/controls etc. in .NET and I can reuse it...
3
by: Simon | last post by:
Hi all, I'm hoping that some of you clever chaps could offer me some advice on code reuse. You see, whenever I make applications, I typically only find very limited
15
by: CR | last post by:
I've noticed that the trend these days is to declare variables in the middle of code instead of at the top. What is the advantage of this? It seems like it makes it hard to reuse variables. Here...
4
by: Simon | last post by:
Hi all, I'm hoping that some of you clever chaps could offer me some advice on code reuse. You see, whenever I make applications, I typically only find very limited
16
by: sailor.gu | last post by:
Hi all guys, As an embeded programmer with five year C experience, I did read many great books related with design, coding, test,debug,algorithms, compiler, design, os, pm and others. I...
6
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused...
2
by: Jeff Dege | last post by:
I'm working with a group that's been doing C++ coding for quite a long time, now, and in that environment we've pretty much worked out development practices that serve us well. We've been doing...
7
by: RichB | last post by:
I am just trying to get to grips with C# and OOP, and one of the benefits would seem to be code reuse. However I am not sure where to draw the line. I have the following section of code: if...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.