
Object oriented programming with ANSI-C (1993) - xkarga00
https://ritdml.rit.edu/bitstream/handle/1850/8544/ASchreinerBook1993.pdf?sequence=1
======
mikevm
When people tell me that they love C, I don't get it.

C is so tiny that if you're going to write anything non-trivial you either
have to re-implement various common data-structures yourself, or find
appropriate libraries. No built-in string type, no vectors, no nothing. Also,
the lack of generics makes for really ugly code, filled with void pointers and
casts whenever you attempt to write type agnostic data structures such as
lists, queues, etc.

It also seems that whenever I have to write in C, I end up writing code that
painfully wants to be object-oriented, or implementing features that are
already part of C++.

So why would you willfully pick to use C when C++ is around? Heck, even if
some sick reason you don't want to use explicitly use objects, at least you
can enjoy some of the other benefits of C++ (e.g. its standard library).

~~~
ketralnis
And yet your operating system and over half of your userland is in C. Must be
doing something right.

~~~
pjmlp
Only if you are speaking about UNIX distributions, excluding Mac OS X.

~~~
EpicEng
The Windows and OSX kernels are written in C. Not sure where you got your
info.

~~~
pjmlp
Somehow the HN crash removed my comment.

The MacOS X drivers are written in C++.

Windows kernel code is being made C++ compliant given that C is a legacy
language from Microsoft's point of view. Many new APIs since Windows XP are
being made available only as COM objects.

As of Windows 8, the DDK officially supports C++ and many of the samples have
been updated accordingly.

~~~
EpicEng
Yeah, the comments refuting what you said were as well.

OSX kernel is C. Drivers being written in C++ is irrelevant.

Windows kernel is C, the fact that they are making it compilable by a C++
compiler is irrelevant.

So what's your point exactly? Let us not forget you said that nothing outside
*NIX was C, and you are wrong.

~~~
pjmlp
> OSX kernel is C. Drivers being written in C++ is irrelevant.

A kernel without drivers is useless.

> Windows kernel is C, the fact that they are making it compilable by a C++
> compiler is irrelevant.

It is not irrelevant. Code compiled by a C++ compiler is C++, regardless how C
fans try to bend it. Having this discussion since 1993.

Besides many new APIs since Windows XP are COM only as well.

C is legacy according to Microsoft, unless they change their mind about it.
Even the latest C99 additions to their compiler were done as a requirement for
C++11/14 compliance, not because they changed their mind.

> So what's your point exactly? Let us not forget you said that nothing
> outside *NIX was C, and you are wrong.

L4, Genode, OS/400, BeOS, Symbian are also done in C++.

What other OS, non UNIX based with the same maturity as the list above, can
you point out being done in C?

UNIX based systems are the only ones that will be fully done in C due to their
symbiotic relationship and the way their developers consider a sacrilege to
use something else.

~~~
EpicEng
> A kernel without drivers is useless.

So... that means that the kernel is not written in C then? I'm confused.

> It is not irrelevant. Code compiled by a C++ compiler is C++, regardless how
> C fans try to bend it. Having this discussion since 1993.

Except they don't compile it with a C++ compiler, it simply _can be_ compiled
with one.

However, what would you call this, C or C++?

    
    
      #include <stdio.h>
    
      int main(int argc, char **argv)
      {
        char line[256];
        fgets(line, sizeof(line), stdin)
        puts(line);
        return 0;
      }
    

> What other OS, non UNIX based with the same maturity as the list above, can
> you point out being done in C?

Windows. Already did that. This discussion is now feeling circular.

~~~
pjmlp
> So... that means that the kernel is not written in C then? I'm confused.

It depends how important you consider the kernel, written in C, requires the
drivers, written in C++, to perform any useful task.

> Except they don't compile it with a C++ compiler, it simply can be compiled
> with one.

Right now maybe not. Just wait when MSVC++ no longer does C.

> However, what would you call this, C or C++?

Actually,

It is C code according to ANSI C.

It is C++ code according to ANSI C++.

It is Objective-C code according to Objective-C language specification.

~~~
EpicEng
So... I guess I am confused. Here is how this has gone:

You: The only widely used, mature OS which is written in C is *NIX.

Me: No. The Windows and OSX kernels are written in C.

You: Yes, but OSX drivers are C++ and Windows is moving toward being able to
be compiled by a C++ compiler.

Me: Right... but they are still written in C.

You: Yes, but that doesn't matter, you're wrong.

Me: ?

The discussion was centered around these kernels being written in C. They are,
you have even conceded to that, yet you continue to argue (what point I don't
even know) because OSX drivers are in C++ and Windows may some day in the
theoretical future be compiled by a C++ compiler? I don't get it.

~~~
pjmlp
So you don't get that a kernel without drivers cannot even boot. Drivers that
are an _integral_ part of the kernel, thus making an C/C++ hybrid, not pure C.

As for Windows, given the ongoing transition, and some of the chapters of
Windows Internals book series, how can you prove that everything is still
compiled with the C compiler and not C++ one?

You are right, it is silly to keep this discussion.

------
d4rti
Zed Shaw's Learn C The Hard way has an exercise that builds a simple prototype
based OO system using C[1].

[1]:
[http://c.learncodethehardway.org/book/ex19.html](http://c.learncodethehardway.org/book/ex19.html)

------
filkatron
Relative noob here, I'm learning programming in general and I picked C to
start with. My university courses are pretty shitty so I have to do it by
myself. OOP is pretty repulsive to me. I don't know why. Too many abstraction
that I can't see clearly.

Would any of you recommend me to try and get the hang of OOP using C or should
I just pick up Java or C++ ?

(sorry if this is a inappropriate comment on this post)

~~~
dsego
You could try Objective-C. It's proper OOP with messages, and you can write
normal C style stuff. None of C++ stupidity (like cout<<"Hello World", yuck).

~~~
pjmlp
Except Objective-C is Apple's toy.

GNUStep does not count, as it is ages behind Cocoa.

------
Roboprog
I had to make an application framework something like this with ANSI C in the
90s. Make a structure (or two) full of function pointers as an "interface",
pass it (them) as the first parameter to all the relevant functions as
"this"/"self". (wait, that kinda looks like what "Go" does - hmmmm)

Of course, some in the company tried to pad their resumes with actual (gahd-
awwwful) C++. Lovely jewels like this:

    
    
        if ( output_format == A) {
            a.write_pg_header();
        } else {
            b.write_pg_header();
        }
    

Guess they never heard of a virtual method. Then there's the fun of all the
memory leaks from copy constructors and other silly C++ pitfalls.

Simulated OOP in C is an interesting exercise. I suppose if I had not learned
an OOP language other than C++ first, I would have thought C++ is just how
(hard) it's supposed to be :-)

~~~
Roboprog
Also did an OOP emulation a few years earlier than this using Clipper 5, of
all things. No structs, but you could make constants for array indices, and
assign a closure/block to each array element, using the array as a virtual
method table.

------
blacksqr
I ran into this a while back. Worth knowing about I think. I'd like to see a
compare-and-contrast with "Writing Bug-free C Code."
([http://www.duckware.com/bugfreec/index.html](http://www.duckware.com/bugfreec/index.html))

~~~
deletes
I have read Object oriented programming with ANSI-C and implemented my own
class system in C with inheritance and polymorphism.

Taking a look at Writing Bug-free C Code the chapter about class is really
just an example how to use structs with pointers that don't expose the
underlying implementation.

If you want to learn oop read the Oop with Ansi-C, where classes similar to
c++ with certain limitations are possible. The book explains everything in
detail with including source
code[[http://www.cs.rit.edu/~ats/books/ooc-14.01.03.tar.gz](http://www.cs.rit.edu/~ats/books/ooc-14.01.03.tar.gz)].

~~~
ExpiredLink
This seems to be the horrible Schreiner book. IIRC, he tries to do OO in C by
using a special pre-processor. Sorry, this approach has never been relevant.

~~~
Roboprog
Sarcasm? The original "C with Classes" was a preprocessor, I believe.

~~~
bitwize
And then it was found to be inadequate for real work and turned into a
separate language.

~~~
UNIXgod
It was used to bootstrap it. Sounds like real work to me.

------
pmelendez
For page 33:

" struct Circle { const struct Point _; int rad; }; We let the derived
structure start with a copy of the base structure that we are extending.
Information hiding demands that we should never reach into the base structure
directly; therefore, __we use an almost invisible underscore as its name __and
we declare it to be constto ward off careless assignments. "

This looks so manual that reminds me "C with classes". It is a very pedagogic
read though.

------
davidgerard
Nice book, but please change the title to note this is a 221-page PDF!

------
daviddlhg
Historical interest: Amiga BOOPSI. Not saying it was perfect, but it certainly
existed.

[http://en.wikipedia.org/wiki/BOOPSI](http://en.wikipedia.org/wiki/BOOPSI)
[http://en.wikibooks.org/wiki/Aros/Developer/Docs/Libraries/I...](http://en.wikibooks.org/wiki/Aros/Developer/Docs/Libraries/Intuition/BOOPSI)

------
ithinkso
Big C/C++ fan here, although I'm sure writing software for PC in C is
extremely painful. Personally for PC soft I'm using C++ (Qt) but I actually
really had to learn some 'higher level' language for this particular job.
However, my main area of interest are microcontrollers and there is NO other
option than learning C (C++ compilers costs too much for a hobby projects).

------
bluedino
I was kind of expecting this to be a link to Zhivago's article on OOP with C

[http://www.codersger.de/mags/cscene/CS1/CS1-02.html](http://www.codersger.de/mags/cscene/CS1/CS1-02.html)

