Quote:
Originally Posted by Super TWiT
I am sure many of you have heard about Linus Torvald's Rant on C++. At first, I thought he was crazy, but a lot of people agree with him that I've seen after looking at various comments on websites that journal tech news. I am looking to learn a language and am currently swayed towards C++, but I DO respect torvalds and it leads me to wonder... Many small programs are written in c. I have heard that c++ really isn't different than c as far as efficiency (I HATE bloated applications). I don't want to start a flamewar, but am looking for a balanced disscussion of c vs c++. What are the main differences?
|
I was at one point faced with the same question as you.. "What is the difference between C and C++?"
Well, C++ is C with some extra features (C++ is C = C + 1). You can write programs that will compile under g++ that are written exclusively with C libraries.
The difference between C and C++ in this way is that C is -
#include <stdio.h>
#include <string.h>
whereas C++ is
#include <cstdio>
#include <cstring>
#include <c - then - other header file minus .h extension>
Also you have "namespace"s, which allow you to access member functions of a class without the requirement of a scope resolution operator.
So, without "using namespace std;" in your source file, to print something with C++'s cout function, you would have to type -
std::cout << "stuff" << endl;
whereas with "using namespace std;"
you only have to type cout << "stuff" << endl;
On top of that you have inline functions, a cleaner way of passing values by reference, and a system of reading/writing input that is somewhat different than C. By which I mean the "string" class, which I have yet to prefer over the standard null terminated string. (my expertise is very much still a work in progress.)
inline functions are similar to macro definitions in that when you use a macro name as a function call, the code represented by the macro name is written in place of the function call. Inline functions do the same thing assuming that they can perform successfully, otherwise they behave just like a regular function. (the purpose is to avoid the function calls during run time that would be better served as just being code in a program, without the overhead of the function call.)
The "most" defining feature of C++ is the reversal, renaming, and expansion of the structure. By reversal I mean, in
struct demonstration_structure{
int everything_is_public_in_a_structure_except;
private:
int things_that_come_after_private;
};
whereas in a CLASS
class demonstration_class{
int everything_is_private_in_a_class;
public:
int except_everything_that_comes_after_public:
};
I would suggest learning C first, because just about all the "Beginning C++" books I've seen mostly cover C, but without the attention to the detail that a C book would give (for example, the book 'The C Programming Language' contains 90% of the features in "Beginning C++", which is my college book, with better examples and better explanations).
On top of that, C++ is C with some new stuff thrown in. In this respect, C is C++'s "base class", and C++ is the "derivative class".
The base class contains all of C. The derivative class contains everything the base class is + some more.
NOTES on the Linus letter - I imagine he isn't entirely incorrect, though I can consider the C vs C++ debate to be nothing unlike the glock vs 1911 debate in that both platforms work as intended, but some people can't handle one, the other, or both for some reason. From that point, people build emotions around their decision, and all meaningful conversation is lost from that point forward.