15 ms·
Object oriented programming with ANSI-C (1993)
- blacksqr 13y agoI 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 13y agoI 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 13y agoThis 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.
- deletes 13y agoYes a single chapter is dedicated to that, all the code is beforehand explained and then implemented in the preprocessor. Everything can be implemented by hand. The point of the books seems to be mostly educational, if you would decide to actually use oop in serious projects then writing your own preprocessor would solve two problems. The tedious manual table/pointer writing and avoiding mistakes coming from the former. I would love to get some book recommendation regarding the subject from you.
- Roboprog 13y agoSarcasm? The original "C with Classes" was a preprocessor, I believe.
- pmelendez 13y agoFor 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.
- Roboprog 13y agoI 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 13y agoAlso 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.
- coldcode 13y agoI did as well in 1988, but mine was missing a lot of things as I didn't have any good examples to base it on. All I had ever read was the Smalltalk issue of Byte.
- filkatron 13y agoRelative 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)
- cypher543 13y agoUnless you're working on ancient software or writing an OO programming language that translates to C, you really shouldn't need to worry about OOP in C. I suggest learning as much as you can about C and then graduate to C++. Java would be fine, too (as would C# or Python or whatever), but I personally like writing C++ whenever I can. It's a nice blend of low-level system programming and high-level abstractions. C++11 is even better with standard threads, clocks, promises, futures, anonymous functions, and so on.
- UNIXgod 13y agoConsider a dynamic language to get a grounding on the abstaction. OOPS concepts make more sense in those especially if your doing windowing and graphics. May be worth it to look at smalltalk or ruby.
- clarry 13y agoPerhaps you could forget the buzzword and just learn to code well in your chosen language. Then try another language -- preferrably one that is quite different from what you started with -- to learn a completely different approach.
- deleted 13y ago[deleted]
- wylee 13y agoTo just get the hang of OOP, you might be better off starting with Python or Ruby (or some other language with a REPL). Although you can do OOP style programming in C (and it's a fun exercise), there's a lot of low level stuff that's really interesting but not particularly relevant to the basic concepts of OOP. If the choice is between just Java and C++, I'd go with Java, but it probably depends on what your long term goals are. In any case, learning any language can't hurt, so I don't think there's any danger of making the wrong choice--unless you get so stuck that you're not making any progress. Just pick whatever "fits your brain" then branch out later.
- d4rti 13y agoZed 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
- davidgerard 13y agoNice book, but please change the title to note this is a 221-page PDF!
- mikevm 13y agoWhen 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).
- EliRivers 13y agoThere are many platforms that have a C compiler, but not a C++ compiler. Sometimes you just don't have the choice.
- georgemcbay 13y ago"...or find appropriate libraries." But... finding appropriate libraries is generally simple. Due to C++'s ongoing ABI issues C is, to this day, still the predominate language for library authors who want their library to be nearly universally consumable (in C, C++, Python, Go, Ruby, node/JavaScript, etc). That said, I agree with part of your point which is why I use Go when I can today (though it also isn't suitable to be a C-like universal library language for other reasons). Built in strings, interfaces, maps, etc remove a lot of the basic code level pain points without also introducing the overbearing level of language complexity of C++/stl/boost (and I say that as someone who spent nearly 10 years programming professionally in C++ and at one time misguidedly considered myself a fan of the language). Go is not so big on the full spectrum of traditional object orientation or generics, but once adapting to OO solely by composition and duck-type interfaces I haven't found either of those to be a big issue for me.
- to3m 13y agoI've found this too. It's rather turned me off using C for anything; in no time at all, I always find myself doing something that would be very straightforward in C++, but is a certain amount of bother in C. So (I ask myself)... what is this buying me again? What's the point? Fortunately, most C++ compilers' support for inline C code is excellent. Even gcc didn't get it wrong ;)
- daviddlhg 13y agoHistorical 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/Intuition/BOOPSI http://en.wikibooks.org/wiki/Aros/Developer/Docs/Libraries/I...
- bluedino 13y agoI 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
- ithinkso 13y agoBig 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).