Going C++

Started by
25 comments, last by owl 19 years, 3 months ago
I've been programming in C for a while now, but I'd like to know C++ as well. I've seen they are VERY similar languages, but this new "objects" thingy is actually hard to understand, so...do you know of any tutorials you would recommend? I just need an edge, some basic concepts behind the idea of object-oriented programming and the new syntax behind classes and that sort of things... So, where should I look? Thanks!
Advertisement
Here you go, a brilliant document that explained the main differences to me. Credits to the author!

http://www.4p8.com/eric.brasseur/cppcen.html

Always remember this: C is not a subset of C++. Rather, C++ is a totally different language with its own standard. You'll especially notice this when a C program doesnt compile with the g++ compiler because the .h files are not exactly the same in both languages. In fact, it's better not to switch between languages at all and just go for either C or C++. Recently I stepped over from C to C++ and I find some C++ features to be very handy, like exceptions and classes.

Good luck and have fun coding,
Bas
Holy...uhm...cow! This EXCEPTION thing is driving me nuts!
Thanks for the link!
With the risk of getting massive flames, why do yuo want to switch to C++? I am a C programmer for more than 2 years, and I didn't find anything useful (FOR ME) in C++.
Quote:Original post by Raduprv
With the risk of getting massive flames, why do yuo want to switch to C++? I am a C programmer for more than 2 years, and I didn't find anything useful (FOR ME) in C++.

Well, there are some features found in C++ that really makes it a better C. You don't have to use the object oriented features at all, just stay procedural if you prefer. But things like namespaces, operator overloading and templates are very useful features, even if you want to stay away from OOP. So it's definitely worth learning at least these parts of C++, because they will make your C code cleaner, more elegant and easier to maintain.
The only procedural C++ feature that I find useful is the operator overloading, but I don't find it THAT useful. In fact, sometimes it can make the code even more confusing, especially for someone who is not familiar with your program.
Namespaces, well... I use long global variables, and their name is pretty descriptive so I don't really need namespaces.
Oh, and you can also put values for variables in headers. That'snice too.

Of course, there is nothing wrong with using those features, or even OOP stuff, but if you are happy with C as it is (I personally am), I don't think you should move to C++ just because it's 'trendy'.
Quote:Original post by basananas
Here you go, a brilliant document that explained the main differences to me. Credits to the author!

http://www.4p8.com/eric.brasseur/cppcen.html

well, the number 6 in that thingy is wrong...

for (int i = 0; i < 10; ++i) {  cout << i << endl;  // i is in scope}cout << i << endl;// ISO standard says i is out of scope// msvc doesn't care [it was a bug in visual c++ 6, it doesn't warn in .net 2003 but can probably be configured to do so]// the version of gcc I tried complained, mentioned the iso standard


also, templates, even though a pain, make abiguity with function overloading completely go away

the ambiguity would be something like having:
double sin(double);
float sin(float);

so, if called with a float, which one will get called? a float can be up-cast to a double. You'd think the compilier would be smart enough to keep it a float [it isn't], but what about an int? which should it be cast to?...

the implementation of template functions doesn't have to be type independent, you can write a different template function for float and one for double...


virtual functions are useful too, if a pain. If you felt like it you could make a 3d engine where all objects have their own virtual draw method, and the engine doesn't need to care about their type [terrain, nurbs, catmul clark subdivision surface, vertex buffer]...
[performance wise it might not be that great of an idea]
Just going from C to C++ you will immediately benefit from templates and modules. In C you also have to deal with modules but it's a bit of a pain to set up because you have to impose the structure on it whereas it's done for you in C++ thru objects. Then the next step up is OOP or polymorphism and some other stuff. The main idea behind this is to tell the object what to do and not to pull data or its internal representation out of the object for anyone to see. Whether or not this will work for you is up to you and your project. Some things fit into OOP and some don't. But object based programming applies to both C and C++ and it's what most of us will eventually end up using if not already in a larger program.
Thinking in C++ by Bruce Eckel. His target audience is actually C programmers migrating to C++. It's a pretty wordy book though if you're only looking for a quick reference.
Quote:Original post by sit
well, the number 6 in that thingy is wrong...


I've tried it myself and I have to conclude that you are right! I always used "for (int i..." like the scope was only within the for loop itself, but that document claims that you can access i even after the loop has ended. It's a nasty mistake in what is actually a great document, and I've emailed the writer about it.

Well found by the way!

Bas

This topic is closed to new replies.

Advertisement