InformIT has a series of articles about C++, including a long introduction to C++ for new programmers and a Templates and Inheritance interaction article.
InformIT has a series of articles about C++, including a long introduction to C++ for new programmers and a Templates and Inheritance interaction article.
Cool! I guess noone else will post a comment here because people are only interested in flame wars/distro wars/OS wars.
Most of those who comment about *NIX like OSes don’t wrote 1(read as one) line of code in their life but they still post ugly comments. So, people, here is something worth reading!
Enjoy!
Yeah, this is a pretty good article, and it shows the beautyness of C++ as well
I can’t imagine how those C people can live without very useful stuff like templates..
This seems like a rather good series. However, it may not be very suitable for complete newbies. It seems to have rather much academic theory and not enough hands-on examples and explanations.
The article of templates&inheritance is interesting, but make me wonder if it’s reasonable to use those techniques.
They violate the KISS principle, and their inner workings aren’t easy to grab by intermediate C++ programmers, making the code somewhat obfuscated.
I’m proficient in C++, and have been enthusiastic about it quite a lot of years (around 6), but in the last two I’ve been forced to use plain C for various reasons: in work because it’s the lowest common denominator of the prorammers pool (actually, I was the only C++ programmer here), and in home I’m programming now for microcontrollers, where C is more appropiate.
Apart for the traumatic experience of losing a lot of flexibility in the language in the downgrade from C++ to C (I first learned C, then C++), I’ve come to love the fact that C is a very small and predictible language.
Now i’m considering to begin using ObjC.
THE QUESTION: Anyone have come this path too and tried ObjC? How was the experience?
PS.:
I’m fascinated by programming languages, and have done my homework; I’ve used interpreted and compiled ones, low level and high level…
That is (in order of appearance in my life) Basic, Clipper, x86 asm, C, REXX, Tcl/Tk, C++, Modula-2, Prolog, Python, Lambda-calculus, guile/scheme.
Nevertheless, the ones I use currently are C (obviously), Tcl/Tk (because of it’s portability) and Python O:-).
It’s nice to see an article that shows some of the powerful features of C++ (generic programming, STL, exceptions, operator overloading, etc) without treating C++ as “C with classes.” Not a strlen() or c-style array to be found anywhere
I’ve tried Objective C. It is very tedious for the first couple of days or hours depending on how you program, because it is very much different from CC++Java in how it looks on the screen.
As far as I know you cannot access data elements inside of an instance of a class with Ojective C and it forces you to use accessor methods a-la Java. This is the type of thing programmers either really love or really hate. Some people like being constrained to one programming model because it makes decisions for them (I prefer this) and other people hate this sort of rigid behavoir. I found Objective C to be the compiled analogue of Java because you can use NSAutoreleasePool to act a lot like garbage collection…I think there might even be java style garbage collection.
In programming I encountered some major differences between doing things for openStep and Cocoa, but all in all I don’t think it is too hard to write programmers that run on both.
I like objective C a lot, although it really isn’t too much better or worse than C++ or Java. They’re really almost the same, and to me at least the nuances almost do not matter. I write mostly compiler software and games which do not need to run at high speeds.
I think you should learn it just for fun, personally. You can download a pdf from developer.apple.com and read their introduction to it. I also recommend Building Cocoa Applications from Orielly. It seemed to me to be the most useful of the three books they put out on the subject.
My only gripe is that I cannot easily (as far as I know, I would LOVE to be corrected) port Objective C programs to Windows. Anybody with such information please e-mail me!
Thanks for your answer. I’ll give ObjC a try, that’s for sure.
As far as I’ve looked, the syntax isn’t any more weird than the one of Tcl/tk, so it doesn’t disgust me O:-).
I don’t have an OS X machine available to me; I intend to try out GnuStep. Do you think thet “Building Cocoa Applications” would still apply? (would you recommend the book, that is) Or is it too much focused in OS X implementation of ObjC?
Thanks again 🙂
I’ve been programming C++ for many years.. and recently I started looking at different ways to get things solved.
I especialy falled in love with ruby. I don’t know exactly what makes me love it.. but it just feels right 🙂
The fast parts of the code I still do in C++ and
the flexible parts I do in ruby.
I have written a tutorial on how to embed ruby into c++.
http://metaeditor.sf.net/embed/
Suggestions/Comments is very welcome 🙂
>> They violate the KISS principle, and their inner workings aren’t easy to grab by intermediate C++ programmers, making the code somewhat obfuscated.
Well, call it obfuscated.. fact is that it’s easier to grab then let’s say the spaggethi-C code of Gtk
(for me at least)
@Holofix :
no, you could access your instance variables directly … but in fact, as it’s possible to declare method with the same name as your instance variable, personnally, I only use methods as accessors.
Frankly, the programmation with Objective-C is much different that what you actually do in C++ (I’m a former C++ programmer); of course, you could do the same way, but it’s a pity to not use the dynamism nature of Objective-C … Things like categories or poseAs: , forwarding, etc. (and I don’t even speak of distributed objets) are a really good thing.
About compiling Objective-C on windows, why don’t you try the GNUstep window’s port ? there is even a gui installer (it doesn’t include the gui classes for the moment, as they are considered as alpha).It’s available here : ftp://ftp.gnustep.org/pub/gnustep/windows/Install-GNUstep-Developm…
@Dario
The BEST Cocoa book available is «Cocoa Programming for Mac OS X» from Aaron Hillegass, ISBN 0-201-72683-1 . And this book could be applicable to OpenStep or GNUstep (Aaron Hillegass is a former NeXT’s employee). That said, the O’reilly book isn’t that bad … (there is also one O’reilly book not very interessting about mac os x) .
Anyway, you could look on some documentations and tutorials about GNUstep and Objective-C on this page : http://www.roard.com/docs/ .
two presentation articles to GNUstep programming :
http://www.sophos.ca/~ludovic/article/article.html (published in the Linux Journal May edition)
http://www.roard.com/docs/lmf1.article/ (published in the Linux Magazine France February edition)
i think the best source for learning c++ is bruce eckel’s book “thinking in c++”, available free on the Internet. (oh, and the best java book is also from him, “thinking in java”). the link should be http://www.bruceeckel.com or something like that. –; ) this series are also nice, but i agree that there is not enough examples.
Some for loops using STL in the article “iterators”
for (vector<int>::iterator p = v.begin(); p!=v.end(); p++)
for (vector<double>::iterator p = v.begin(); p<v.end(); p++)
while both seem to be used for vectors when trying to learn STL the other day I found out that the p<v.end() doesn’t seem to work for lists.
This is because lists have no real order, I think.
One of the many traps you can find when learning programming off of example code.
I still find C++ confusing and ugly. To be honest, I much prefer Delphi/Object Pascal, it seems to have many of the same features of C++ (excluding generics and a preprocessor, but they are of dubious value anyway) with a much cleaner and easier to understand syntax.
Somebody made a comment about the spaghetti code of GTK, that’s hardly fair, GTK is object oriented. If anything I find reading GObject C easier than C++, as the latter seems to invent new syntax just for the hell of it, at least with C there is a small working set of rules from which you can extrapolate the others.
There are all sorts of complex rules about STL iterators. The iterators you get out of a vector conform to what is called the “RandomAccessIterator” model. This means they support increment/decrement, pointer arithmatic, ordered comparison, etc. The iterators you get out of a list are “BidirectionalIterators” and all you can do with them is compare for equality, increment, and decrement. Unless you absolutely need the functionality, you should write your code to be as generic as possible. ie. If you just need to test for the end of the loop, use i != vec.end(), because that will work on all iterators that support comparison for equality. This is actually a general principle of generic programming. Don’t depend on the behavior of a specific type unless you absolutely have to. The STL is a good example of this. Pretty much every construct in the STL just assumes that the object is has a default constructor (one that takes no arguements) and has a sane copy constructor. Everything else is totally irrelevent to whether you can use STL constructs on a given type.
I know about openStep for windows. I should’ve been more clear. I would like to distribute a product I am working on for Windows and Macintosh and code both in objective C. I could not mandate the use of openStep. I was more looking for an Objective C framework over MFC similar, but maybe not the same to all the NSWindow etc. stuff. ( I want even very stupid people to be able to use this program, and I’d wager there are even some smart one who would have trouble installing openStep on windows).
@Dario, I would not recommend the book I mentioned if you are not using OS X.
@Nicholas: How can you access instance variables from outside of a class?
If I have a variable in the class called x,
Do I just do [myObject x] as if it were a method?
Do I do myObject.x ?
Afaik, I’ve never even seen ObjC code with private or public or anything similar.
from my, limited, knowlage of ObjC you can access objects variables using the object.x syntax.
The ObjC syntax also seems much nicer than C++ as it does not have the rather arbitary quirks like forcing a diferent syntax for objets and objects via pointers object.x Vs. pointer->x even if it does still have headers which always seem like extra unneeded effort and showing the internals of how the compiler works when I simply don’t care how it works so long as it produces good code.
well, it’s not OPENSTEP, it’s GNUstep … anyway, I think it is possible to provide an already compiled package, so users won’t have to compile/install everything. The problem is the current alpha state of the -gui under windows (I don’t have windows, and I just tested it quickly).
To reference an instance variable, you should declare your variable as public with the @public directive :
@interface MyClass : NSObject
{
// By default it’s protected
@public
int x;
}
– (void) print;
@end
@implementation MyClass
– (void) print {
NSLog (@”number : %d”, x);
}
@end
then in your program :
MyClass* instance = [MyClass new];
instance->x = 42;
[instance print]; // will print “number : 42”
But you should note that it will only works if you have statically typed your class (like here, MyClass*); with a simple “id” type, it won’t work (compiler failure) as the compiler can’t know what possible variables could be accessibles…
For ivars, you could use @protected and @private directives too. It only apply to your ivars, not your methods. For private methods, it’s far better to simply add a category in the implementation file (as the method will be totally hidden from the user of the class).
Hope that help. You could read an ObjC manual such as http://www.toodarkpark.net/computers/objc/objctoc.html
(others ressources referenced also on http://www.gnustep.org website)
>> p++
Also, you should always (in those for-loops you typed there) pre-increment variables instead of post-incrementing them (ie. ++p, not p++).
The latter will slow things down.
>> Somebody made a comment about the spaghetti code of GTK, that’s hardly fair, GTK is object oriented.
That was me who said that.
But I wasn’t talking about the object oriented nature of Gtk, it was more about C libraries in general (like libPNG and its lib_png_long_not_so_good_looking_function_names).
It’s more the way how everything is pasted into one big file, how everything is done using the old C-style (duh) way of doing error checking.. it just doesn’t look attractive.
And C++ features can really simplify your code.
For example, try comparing a point structure with another point structure in C: ‘((point1.x == point2.x) && (point1.y == point2.y))’ yikes!
While in C++ you could just do ‘(point1 == point2)’ using an overloaded == operator.
That does not only greatly improve the readability of your code, it’s also less work to type which means you’ll be more productive
Templates are also a good example.
In C you’ll have to redefine your classes for every type or use other weird and hackish ways, but with C++ you can just create one template and use that multiple times for different types of variables, it’s amazing Mike! (pun intended
>> If anything I find reading GObject C easier than C++, as the latter seems to invent new syntax just for the hell of it, at least with C there is a small working set of rules from which you can extrapolate the others.
But you can’t ignore the fact that you’ll have to type (and read) a lot more than with C++ to create a simple object with some member variables/properties.
And with C++ there’s no extra dependency for the object stuff.
btw. I’m not trying to flame anything/one, I’ve also been against using C++ 5 years ago
Ghod.. I really hated that language, and now I’m in love with it
C++ template is good but only at source code level, it helps programmers to write less code. However, template introduces overhead and decreases the performance. I think C has better performance.
That was me who said that.
But I wasn’t talking about the object oriented nature of Gtk, it was more about C libraries in general (like libPNG and its lib_png_long_not_so_good_looking_function_names).
Well as for interCaps vs GTK-style/czech vs slovak or however you refer to them, basically naming schemes are a matter of personal taste, I prefer interCaps style but that’s just what I was brought up on and find aesthetically pleasing. Others have pointed out to me that words_written_like_that are easier to read quickly, even if they do look a bit ugly.
It’s more the way how everything is pasted into one big file, how everything is done using the old C-style (duh) way of doing error checking.. it just doesn’t look attractive.
For error checking you can either have functions return a result code and use pointers to return data, or use exceptions I guess. They both have their uses, but I’d point out that parts of Mono are being de-exceptionified because for core code exceptions are too slow (i think).
While in C++ you could just do ‘(point1 == point2)’ using an overloaded == operator.
Yes, operator overloading is OK, but I tend to find that if you aren’t the author of the code it can be confusing (in a general sense, not so much for C++). The semantics of it are essentially hidden in the code – is comparing a string object in that way case sensitive? Only the author of the object knows.
It also means you need a different syntax to determine if the two expressions evaluation to the same instance, or are semantically similar. Like a lot of C++ features, it can be easy to abuse.
Templates are also a good example.
In C you’ll have to redefine your classes for every type or use other weird and hackish ways, but with C++ you can just create one template and use that multiple times for different types of variables, it’s amazing Mike! (pun intended
I was talking to a C++ using friend of mine the other day, he used templates for a while but doesn’t define them anymore – he said they make code a lot harder to debug for starters. I have to admit, I need to learn more about generics, I’m still not 100% sure what they give you that intelligent use of polymorphism doesn’t – Delphi has no templates and I never found myself wishing for such things, but then maybe you only want them once you’ve used them.
But you can’t ignore the fact that you’ll have to type (and read) a lot more than with C++ to create a simple object with some member variables/properties.
Yeah, defining GObject is a pain in the ass. I have never written my own GObjects from scratch, just altered other peoples code – I was talking from the viewpoint of reading code rather than writing it.
I’ll probably learn it sooner or later, maybe I’ll even like it. I’d much rather skip straight to using Ruby or Python though.
…because this discussion is much more interessting than: Windows sucks vs. Linux sucks articles by all those (f)lamers.
I second that!!!
I would also like to say that it seems to me that Programmers seem to be a lot more civil when talking about their diffrences than general OS people….just an observation.
No, templates are no different from straight C++ code. The only overhead that templates incur is in file size.
That said, I can see no real benefit to template meta-programming, and I don’t advocate its use at all.
I have experience with both. C++ for toolkit development and libraries, and C for low level stuff like device drivers and embedded code.
i am reasonably proficient with C++, and i have built an number of template classes that also use inheritance. i still have problems reading stl code as stl is a lot of things, but not very readable.
the reasons i like C best:
-i can know the complete C standard by heart, and there is no code that i can’t read or debug. knowing C by heart is possible even if i don’t use it for some time. knowing the full C++ standard is only possible if you do it fulltime, and even then…
-i can do anything in C that C++ doesn’t like. this is nice especially if you do low level stuff.
-C is simple. C++ is not. if you doubt this, compare the number of pages for ‘the C programming language’ by kernighan, and ‘the C++ programming language’ by stroustroub.
kind regards,
Int.
Those of you who don’t think templates are useful:
Do you not find the C++ Standard Library useful? About 90% of it is templates.
Without templates, it would not be possible to have a container class that is either variable-size or typesafe.
What about algorithms? How do you sort a list of phone book entries? How about sorting a list of computer parts? Same thing? Too bad, because you get to write the algorithm twice if you don’t have templates.
That is, unless you’re using a dynamically-typed language like Smalltalk or Python, in which case you pay in runtime overhead and (arguably) in typesafety.
I find that one reason that the STL is so hard to read, is because most implementations are derived from the SGI version. The SGI version goes to _great _lengths _to _keep _from _M_polluting _the _global _namespace _with _symbols _so _it _appends _an _underscore _to _pretty _M_uch _everything _because _names _that _begin _with _an _underscore _are _reserved _for _the _compiler _and _runtime. The sucky indentation and crazy _M_ identifier for member variables doesn’t make things much easier, as well as all the hacks to make it portable to shitty C++ compilers (ahem VC 6.x…) don’t help things much. In contrast, Boost, which uses templates more extensively than the STL does, is very readable and clean.
Templates in general are a very powerful mechanism. The most important thing is that they allow a lot of genericity while still offering static type safety, and without the overhead of a dynamically typed language. For example, take std::copy() The prototype looks this:
template <typename InputIterator, typename OutputIterator>
std::copy(InputIterator b, InputIterator e, OutputIterator o);
Now, std::copy will not only work on pointers, but anything that acts like a pointer on the syntax level. That means I can use std::copy to copy from a raw C buffer to a std::vector, even though the two have very little to do with each other. In Java, any types you pass to the copy routine would have to be derived from the same base class. This imposes a huge performance penalty (the interface must be through non-inlinable virtual functions, the v-table pointer doubles the size of small iterators like straight pointers, etc), and also requires planning ahead of time to link the two constructs (vector and copy). For something that sits at the lowest levels of a program (the standard library) these traits are unacceptable.