Newbie: The C / C++ Issue

Erik Trulsson ertr1013 at student.uu.se
Wed Nov 12 20:21:46 PST 2003


On Wed, Nov 12, 2003 at 09:36:15PM -0500, Lucas Holt wrote:
> 
> On Nov 12, 2003, at 8:37 PM, Marty Leisner wrote:
> 
> >
> >
> >BTW -- I've been doing "object oriented" stuff in C for years --
> >its harder, but its doable.  You have a much simpler language
> >to deal with.
> >
> >First learn how to write good programs in C.
> >Then see if C++ buys you anything extra.
> >If it doesn't, you don't need C++.
> >But I've seen far too much C++ that's just obscure C.
> >
> >Just my experience and opinion.
> >
> >marty
> 
> Am I missing something here?  When does C have OO capability?  Structs 
> don't count.  What about inheritance and polymorphism?

You can write object oriented code in almost any language.
This does not mean that those languages have features that are intended
to facilitate writing object oriented code.

C does not directly support inheritance or polymorphism, but it is
quite easy to "fake" it by hand.  A bit more work than if you had done
it in C++, but quite doable.

> 
> To me a struct is like a VCR with no record button.  You can view the 
> content, but you can't manipulate it with the struct.  If i want to do 
> something to destroy the tape, I must apply a magnet from an outside 
> source (much like a plain old function).  And classes provide security, 
> much like the tab on the front of the tape.  The data is private if the 
> tab is puched out.  (ok thats a bad analogy)
> 
> If C had OO features, then why do we have C++ and Objective C?

Because C does not really have any OO features.
You can write OO code in C, but it is easier to do it in C++ or
Objective C, or some other OO langauge like Smalltalk.

> 
> I would agree that you can write programs that do the same thing in all 
> three languages above, but I don't think that OO is a waste of time.  
> OO promotes code reuse.  That is the whole point.
> 
> Using C++ implies a state of mind.  You can write code like in C, but 
> it defeats the purpose of using an OO language.

C++ is not really an OO langauge.  It is a language with features that
support writing OO programs, but it also has features that help writing
programs in other styles.
That is actually my main complaint against C++.  It has so many
features, and so many special cases that it is almost impossible to
actually understand the whole language.  This means that you either
have to restrict yourself to a small subset of the language (in which
case you probably better off using some other language) or run into
surprises when things don't work as you would expect.
IMO, C++ is almost never the best language for any particular task (but
on the other hand it is almost never the worst language either.)


-- 
<Insert your favourite quote here.>
Erik Trulsson
ertr1013 at student.uu.se


More information about the freebsd-questions mailing list