C versus C++

J

JohnQ

I would say that, in general, you don't need to learn C before
learning C++. In fact, I would go as far as saying that there's
really no reason to learn C at all. Most programming tasks are better
handled in C++, in my opinion. But there is one significant exception
I think.

I'm sure there are many people here who will disagree with me, but it
seems to me that the C programming style is somewhat more well suited
to database and/or file-system programming than C++. Of course, you
*could* program a database or a file system in C++, but in practice,
it's mostly done in pure C.

The reason has nothing to do with language capability. As a superset
of C, C++ is more than capable of doing anything C can do, obviously.
But C++ encourages a certain, "style" or "mindset" or programming,
which is totally different than C style programming.

C++ programmers tend to think in terms of objects - non-permanent,
dynamic data structures which are stored in memory. C programmers,
however, tend to think in terms of raw data: pointers to contiguous
memory blocks, structs, etc. And objects don't always translate well
to concepts involving permanent storage, like disk blocks, etc.
That's why major file systems like XFS, reiser, etc., as well as
database systems are usually programmed in C, even though they *could*
just as well be programmed in C++.

Just to avoid confusion: I'm *NOT* saying that C is better than C++
for anything. I'm saying that, in practice, C-style programming
usually lends itself better to programs which manipulate raw data on
disk, such as file-systems or databases, rather than C++ style
programming, which usually revolves around objects in memory.

By saying "C-style" and "C++ style" you are making the assumption that C++
developers have either forgotten how to engineer stuff since they moved from
C to C++ or that they grew up with C++ and therefor never knew how to in the
first place. The latter scenario is more plausible than the former. It's not
a question of language-associated-"style" but rather a question of developer
knowledge/skill. Just knowing the language at a high level is not enough.
HOW one uses it is important too.

I wouldn't say that "C++ encourages a mindset" but rather that C++'s vast
complexity makes it difficult for one to get their mind around it,
especially the new developer. It's, of course, up to the developer to
"figure out C++" before applying it. Indeed, C++ isn't going to hold your
hand and enable you automagically. The issue you raised (the hindrance to
the uninitiated, IOW IMO) seems to be directly related to the decision to
make a struct and a class the same. Doing so obscured the "styles" you put
forth above. Of course, you don't have to fall prey to it. You can still
program either way but don't expect to get compiler warnings and such to
keep from shooting yourself in the foot at the low level design (not in
C++'s current incarnation that is).

(The people here who responded to your post and thought it was a C vs. C++
flamewar post obviously missed your point contained within, but you did help
that along by titling the original post "C vs. C++").

John
 
P

Paul M. Dubuc

Erik said:
I have not looked one any DB code but most OS code I've seen have been
quite object oriented, with structs being the objects and the functions
that operate on them the methods, and there are both constructors and
destructors. However due to the lack of OO support in C all data is
public and the methods are not members, and constructors have to be
called manually.

When I was a C Programmer trying to learn C++, I found Allen Holub's book,
"C+C++: Programming With Objects in C and C++" very helpful. He devoted the
first chapter (62 pages) to object-oriented programming in C. I was happy to
find that I'd already been using some of these techniques.

In C, functions can't be members of struct, but pointers to functions can.
You can't have truly private data, but you can hide data that you don't want
clients to access behind a void* data member which is internally cast to the
appropriate type. C has many limitations w.r.t. object-oriented programming
compared to C++, obviously, but o-o programming is a discipline more than it
is a feature of a any particular programming language. Once I figured that
out, it was much easier for me to understand the features that C++ has which
facilitate that discipline.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top