Learning C++ (was Re: [PyKDE] Line numbers in TextEdit box?)

Frederick Polgardy Jr polgardy at bodacion.com
Wed Aug 13 17:14:01 BST 2003


I definitely have to second this.  (Would that be: this->second()?  I 
guess for a std::pair it would be just this->second.  Ugh, feel free to 
just shoot me at any time. :))

The worst C++ code I've ever seen is written by people who come from a C 
mindset, and are just using C++ to take advantage of some of its 
niceties (streams, methods, etc.).  It's not that good object-oriented 
code can't be written in C, it's just that it's damned hard to do, and 
even harder to extend (anybody here ever try to inherit a Gtk widget?).  
While all things are possible, the reality is that C _encourages_ 
functional programming, while C++ _encourages_ both functional *and* OO 
programming (while claiming, of course, that the whole point of using 
it is for its OO features).

To make things even more interesting, there are "paradigm"-differences 
which are more the result of different levels of programming, than to 
differences in what a language allows or encourages.  Let me 
illustrate.  A co-worker recently made the following comment (more or 
less) while discussing an OO design: "Use a class for this -- actually 
you can just use a struct, since it's public."  I gasped in horror!  
That's it?!  The only difference in your mind between a class and a 
struct is the default access restriction?  While yes, this is 
technically true from a language standpoint, the concepts are 
_completely_ different.  When I see the word struct in a source-code 
file, I say to myself, this is a bag of data, a simple aggregate, 
possibly even a memory overlay.  Semantically, this is borne out by the 
fact that the default access to a struct is public, but what that 
_means_ is that the data members are intended to be exposed just as 
they are.  No encapsulation, just simple aggregation.  The word class 
on the other hand tells me that what is being defined is a higher-level 
entity -- some _thing_ with an interface, a set of responsibilities, 
potentially located in some inheritance hierarchy, etc.  This is _not_ 
an aggregate.  It has a completely different role and function than a 
struct.  As my example illustrates, this distinction is something that 
is often lost on C programmers without OO backgrounds (no offense to 
you C programmers out there!).

Anyway, I've rambled on enough.  Hopefully someone will find a point in 
there somewhere. :)

Fred

On Wednesday 13 August 2003 09:10 am, Michael Lauer wrote:
> Ugh! Please don't advice people wanting to learn C++ to start with C.
> C++ as a multi-paradigm language is fundamentally different from C -
> it just includes it because of compatibility issues and in the past,
> the C->C++ way caused a lot of people to get used to ugly habits and
> writing C-style-programs in C++ just because it seems to work.
>
> Of all students I ever had to teach C++, the majority of the ones
> with C-knowledge turned out to write much worse C++ programs than the
> ones without C-knowledge.




More information about the PyQt mailing list