Concepts of Programming Languages

(Sean Pound) #1

We can easily generalize this to any container that
meets the C++ standard library requirements:


template
void draw_all(const C& c)
{
typedef typename C::
const_iterator CI;
for (CI p = c.begin();
p!=c.end(); ++p)
(*p)->draw();
}


Using iterators allows us to apply this draw_all()
to containers that do not support subscripts, such as a
standard library list:


vector<Shape> vs;
list<Shape
> ls;
//...
draw_all(vs);
draw_all(ls);


We can even generalize this further to handle any
sequence of elements defined by a pair of iterators:


template void
draw_all(Iterator b, Iterator e)
{
for_each(b,e,mem_fun(&Shape::draw));
}


To simplify the implementation, I used the standard
library algorithm for_each.


We might call this last version of draw_all() for
a standard library list and an array:
list<Shape*> ls;
Shape* as[100];
//...
draw_all(ls.begin(),ls.end());
draw_all(as,as+100);

SELECTING THE “RIGHT” LANGUAGE


FOR THE JOB


How useful is it to have this background in
numerous paradigms? Or would it be better to
invest time in becoming even more familiar
with OO languages rather than learning these
other paradigms? It is essential for anyone who
wants to be considered a professional in the areas of
software to know several languages and several
programming paradigms. Currently, C++ is the best
language for multiparadigm programming and a
good language for learning various forms of
programming. However, it’s not a good idea to know
just C++, let alone to know just a single-paradigm
language. That would be a bit like being colorblind or
monoglot: You would hardly know what you
were missing. Much of the inspiration to good
programming comes from having learned and
appreciated several programming styles and seen
how they can be used in different languages.
Furthermore, I consider programming of any non-
trivial program a job for professionals with a solid and
broad education, rather than for people with a hurried
and narrow “training.”

537

Free download pdf