views:

222

answers:

5

What does C++ add to C?

+1  A: 

Not? I would say it's not worth if you performance is not an issue for you. (Follow the double negatives.)

Jon Reid
So, why C++ instead of C if performance is an issue ?
Yktula
I would add a small memory foot print requirement too.
NawaMan
@Yktula, The answer is the O, O and P.
NawaMan
What OOP-related features are used in idiomatic C++?
Yktula
I assumed you wanted a higher level language.
Jon Reid
+1  A: 

Because despite academic efforts such as Singularity, there's not a single mainstream OS where drivers can be written in a high-level language.

Note that anything that can be done in C++ can also be done in C, but some things are a lot easier in C++.

Ben Voigt
*What* is easier in C++ ?
Yktula
@Yktula: polymorphism. Bam!
Randolpho
Or memory management (see RAII). Or building collections of things (with C++ templates, they're type safe, in C you need code duplication or casting everywhere). Encapsulation. Initialization of state.
Ben Voigt
+2  A: 

Like all sophisticated and powerful things there is a price to be paid to succeed in C++.

  • You have to be incredibly careful with memory management.
  • Multi-paradigm capability means you have to be really good at design to avoid making a mess.
  • Extreme performance requires careful planning and selection of features used.
  • The ability to circumvent most every language policy requires monumental self discipline.

So if you're sloppy with memory, poor at design, don't need fast programs, or have no self discipline, then please don't learn C++. There is always Java or C#.

Amardeep
*"You have to be incredibly careful with memory management"* - no, you just have to use simple idioms that allow you to mostly not worry about it.
Georg Fritzsche
I'd say only the 2nd and perhaps 4th points apply only to C++ instead of both C and C++. Can you expound on the 4th?
Yktula
C lacks built-in OOP and generic programming so I don't see it as really in the running with the other HLL's you've listed. As far as #4, you can cast away almost anything and work around pretty much any access qualifier because the language is powerful enough to blow your own head off.
Amardeep
+2  A: 

meta programming? templates?

like with C you get performance, but the code looks horrible. with the high level languages you get nice code but there is less flexibility to make the fastest possible code.

with c++ you can do both? you can freely make anything as fast as it could be made in C, but native object orientation, and templates/operator overloading ect makes it so you can write fairly nice looking code too. indeed, you can make it so it is neat and fast.

I have never really found it more of a pain to write stuff in c++ than in a higher level language. the trick is having good libraries.

matt
"like with C you get performance, but the code looks horrible." Since C is a simpler language, as long as there's a degree of abstraction, code can look cleaner in C than C++.
Yktula
don't buy this at all. you can write bad code in any language.
asveikau
@Yktula I agree actually, I don't really like using templates simply cause I don't like how the syntax looks! :D (who needs 4 different types of braces?! sorry is that a right bit shift or are you closing to angle braces?!) I suppose by 'horrible' I meant 'a lot' when referring to C.
matt
A: 

My two cents:

  • Although I don't program in Python, I would have to say that Python is probably the best programming language for getting real work done. It's an elegant language, and it has an enormous collection of libraries for doing various things. However, my experience as a user has shown me over and over again that Python is slow (take yum, for example).
  • I do know Haskell pretty well, and I have to say that it's a friggin' awesome language. Better yet, it is compiled, and its speed is competitive with Java and C++ (though you have to put forth extra effort to get this speed in some cases). However, libraries for things like database access don't always match the elegance of Haskell's base libraries (I'm probably way wrong about this), and they're harder to install on Ubuntu. In my opinion, that's why it's more challenging to get real work done in Haskell than in Python.
  • Ruby's good for web applications. Other than that, it's slow (though I speculate jRuby or something might be faster).

C++ is far from elegant, and in many cases, elegance is frowned apon. Anyone ever told you to use static_cast instead of C-style casting? Anyone ever told you not to use namespace std;? C++ has a lot of features, but doesn't tend to have many important language features (such as closures, which are formally proven to be the best thing since sliced bread).

Why do people use C++, then? Well, it's performance-focused, making it a good choice when you need speed. It has classes, namespaces, and templates, so it's a good choice when you want better code organization, but still need to use "C" for some reason. Also, it has the Boost library, which I've heard is really good for getting work done.

Joey Adams
C++ has closures as of C++0x.
Ben Voigt
In your examples its not elegance that is frowned upon, its about maintainability / maintaining sanity. Also what the best language is absolutely depends on the problem you are trying to solve - there is no single *best* language.
Georg Fritzsche