Many people have said that C++ is a completely different language than C, but Bjarne himself has said that C++ is a language that is extended from C hence that is where the ++ comes from. So why does everybody keep saying that C and C++ are completely different languages? In what way is C different from C++ other than the extended features in C++?
|
|
|||||||
|
|
At the very beginning, mid 1980's, C++ was almost a proper superset of C. That is how it all started. And additionally, the technical differences between C and C++ have made that the typical idioms used in those languages and what is considered 'good practice' has diverged even more. |
|||||||
|
|
Stroustrup himself answers that in his FAQ:
It's support for object-oriented programming and generic programming that make C++ "completely different" to C. You can almost write pure C and then compile it with a C++ compiler (as long as you take care of the stricter type checking). But then you're still writing C - you're not writing C++. If you're writing C++, then you're making use of it's object-oriented and template features and that's nothing like what you would see in C. |
||||
|
|
|
Put simply, what is considered idiomatic in C is definitely not idiomatic in C++. C and C++ are very different languages in practice, because of the way people use them. C aims at minimalism, where C++ is a very complex language, with a lot of features. There are also some practical differences: C can be easily called from pretty much any language, and often defines the ABI of a platform, whereas C++ is quite hard to use from other libraries. Most languages have a FFI or interface in C, even languages implemented in C++ (java, for example). |
|||||||||
|
|
Apart from the obvious fact that C++ supports object-oriented programming, I think you have your answer here: http://en.wikipedia.org/wiki/Compatibility_of_C_and_C++ That article contains code examples showing stuff that is ok in C but not in C++. For instance:
Porting a C program to C++ is often straightforward and consists mostly of fixing compilation errors (adding casts, new keywords etc). |
|||||||
|
|
The difference is that in C you think procedurally and in C++ you think in an object orientated way. The languages are quite similar but the approach is very different. |
|||||||
|
|
C++ adds not only new features, but new concepts and new idioms to C. Even though C++ and C are closely related, the fact remains that in order to write effectively in a language, you must think in the style of that language. Even the best C code cannot take advantage of the different strengths and idioms of C++, and so is more likely than not actually rather bad C++ code. |
|||
|
|
|
The "extended features", you make it sound like in C++ they added like, variadic macros or something and that's it. The "extended features" in C++ are a complete overhaul of the language and totally supersede the best C practices because the new C++ features are so much better than the original C features that the original C features are completely and totally redundant in the vast majority cases. Suggesting that C++ merely extends C is suggesting that a modern battle tank extends a butterknife for the purposes of waging war. |
|||||
|
|
While C++ can be a super set of C in syntactic terms - i.e. any construct of C program can be compiled by C++ compiler. However, you almost never write a C++ programs the way you would have done with C program. The list can be endless or may be someone should just do more research to put it as exhaustive reports. However, i am putting few pointers that make the key differences. The point of the current post is that C++ has following features that a good C++ programmers must use as programming best practices even though C equivalent are possible to compile. How should it be done in C++ over C
Features I would still not be fan of in C++ 1. Templates - While i don't use heavy templates in many codes - it can turn out to be very powerful for libraries. There is almost no equivalent of it in C. But on a normal day - specially if you are doing mathematically missing.
Things that i like about C and miss in C++
Dipan. |
||||
|
|
