Question

Is it true that there is no need to learn C because C++ contains everything?

I am taking a class in C++ programming and the professor told us that there is no need to learn C because C++ contains everything in C plus object-oriented features. However, some others have told me that this is not necessarily true. Can anyone shed some light on this?

 45  16958  45
1 Jan 1970

Solution

 88

Overview:

It is almost true that C++ is a superset of C, and your professor is correct in that there is no need to learn C separately.

C++ adds the whole object oriented aspect, generic programming aspect, as well as having less strict rules (like variables needing to be declared at the top of each function). C++ does change the definition of some terms in C such as structs, although still in a superset way.

Examples of why it is not a strict superset:

This Wikipedia article has a couple good examples of such a differences:

One commonly encountered difference is that C allows implicit conversion from void* to other pointer types, but C++ does not. So, the following is valid C code:

int *i = malloc(sizeof(int) * 5);  

... but to make it work in both C and C++ one would need to use an explicit cast:

int *i = (int *) malloc(sizeof(int) * 5)

Another common portability issue is that C++ defines many new keywords, such as new and class, that may be used as identifiers (e.g. variable names) in a C program.

This wikipedia article has further differences as well:

C++ compilers prohibit goto from crossing an initialization, as in the following C99 code:

 void fn(void)
 {
  goto flack;
  int i = 1;
 flack:
   ;
 }

What should you learn first?

You should learn C++ first, not because learning C first will hurt you, not because you will have to unlearn anything (you won't), but because there is no benefit in learning C first. You will eventually learn just about everything about C anyway because it is more or less contained in C++.

2008-09-28

Solution

 14

While it's true that C++ was designed to maintain a large degree of compatibility with C and a subset of what you learn in C++ will apply to C the mindset is completely different. Programming C++ with Boost or STL is a very different experience than programming in C.

There was a term of art called using C++ as a better C. This meant using some C++ language features and tools to make C programming easier (e.g., declaring the index variable of a for loop within the for statement). But now, modern C++ development seems very different from C other than a great deal of the syntax and in those cases the C legacy often seems to be a burden rather than a benefit.

2008-09-28