Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

I know that C++ is based on C, but what are the differences?

  1. Apr 25, 2007 #1
    I know that C++ is based on C, but I recently checked out the book Practical C, thinking it was a C book but now I am unsure and can hardly tell the difference between the two.

    Can someone help clarify what the differences are? which do you recommend that I learn to use as a tool when I get into college?
     
  2. jcsd
  3. Apr 25, 2007 #2
    operator overload, inheritance, polymorphism, class...check in the TOC or index for these words.
     
  4. Apr 25, 2007 #3

    jtbell

    User Avatar

    Staff: Mentor

    C++ started out as basically C with extra features (most notably the 'class') to support object-oriented programming. So most of the basic constructs are the same, or very similar, in both languages: arithmetic expressions (calculations), boolean expressions (comparisons, etc.), if-statements, loops, ordinary functions, etc.

    Where the two languages differ is basically that (a) C++ has features that directly support object-oriented programming, whereas C does not, and (b) C++ has a more extensive standard library than C does, for performing common tasks.

    At the very beginning level, the biggest difference is in the way you normally do input and output. For example, C does output using the printf() function and its relatives, whereas C++ usually uses iostreams (e.g. 'cout' for standard output), which are easier to use for simple input and output. However, C++ also allows you to use C-style output via printf(), for backwards compatibility with C.

    Going a bit further, both C and C++ let you group data together using something called an "array", which works pretty much the same way in both languages. C++ also has something called a "vector" which can do things that an array cannot and is much more convenient to use than an array in many situations.

    Similarly, in both C and C++ you can store text as an array of characters, but C++ also has a 'string' data type that is specifically designed for that purpose, which is more convenient and less prone to programmer errors.

    Many C++ books, especially older ones, look a lot like C books, because they're basically C books that were re-tooled into C++ books. The language is C++, but they do things in a C-style way, using the older features inherited from C rather than the newer ones that were created for C++.

    For an example of a "modern" approach to C++, try Koenig and Moo's "Accelerated C++".

    At the college level, I think you'll find that most places use Java for their introductory programming courses now, because of its ties to the Web and because it has an even more extensive standard library than C++ does. The Java standard library includes graphics, networking, etc., which the C++ standard library does not. You can still do all that stuff in C++ or C, but you have to use libraries that are tailored for your operating system: Unix versus Windows versus MacOS, whereas in Java it's the same everywhere (at least in principle).
     
  5. Apr 26, 2007 #4

    mezarashi

    User Avatar
    Homework Helper

    The difference between the two is very subtle. By C, I hope you refer to the 1990 ANSI C standard. The unfortunate thing is that most casual learners will never be able to tell the difference. I personally think that C is far less flexible than C++, but in a way that makes it safer. If you really need to know the difference, try compiling a program you've created and successfully compiled in C++ using a strict C compiler like GCC. If you're using MSVC, then rename the source file *.c instead of *.cpp. You'll be surprised at all the new errors that pop up.
     
  6. Apr 26, 2007 #5

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    It's a double-edged sword. The inflexibility of C makes it harder to shoot yourself in the foot -- but it also forces your code to be more cumbersome, meaning you have many more chances to shoot yourself in the foot, and makes your code harder to read and maintain.
     
  7. Apr 26, 2007 #6
    I completely disagree with the notion that C far less flexible than C++.

    There is nothing you cannot do in C that you can do in C++ except for using STLs.
     
  8. Apr 26, 2007 #7

    mezarashi

    User Avatar
    Homework Helper

    By inflexible I don't mean incapable. There seems to be formal rigid method of writing code which is actually enforced by the compiler. Just an example off the top of my head, in C90, this is not allowed (but is okay to a C++ compiler):

    Code (Text):

    for(int i=0;i<50;i++)
     
    But I agree with Hurkyl that C++ is a much more enjoyable language to read and write. It is simply our own responsibility to make sure we adhere to strict coding rule standards, since the compiler does not enforce it for us.
     
  9. Apr 26, 2007 #8

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Honestly, I would have cited this as a failing of C90 (and thankfully fixed in C99). I can see some merit in some of C's restrictions, even if I dislike them (such as no function overloading)... but C90's harsh limitation on where you can declare a variable is my single greatest dislike of that standard: it is incredibly disruptive to both the writing and the reading process. :frown:
     
  10. Apr 26, 2007 #9
    do most compilers accept both or just one or the other? can someone recommend me a Windowns Vista safe compiler/IDE?
     
  11. Apr 26, 2007 #10

    KTC

    User Avatar

  12. Apr 26, 2007 #11
    If you're new to programming, I'd say learn the basic procedural and object-oriented programming paradigms in Python. Python is a much cleaner language for learning how to think like a programmer than C or C++.

    If you have to choose between C & C++ or you're just curious, I'd say C++ hands down. It would be very difficult to learn object oriented programming using C. You can always go to C from C++ if needed.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: I know that C++ is based on C, but what are the differences?
  1. Base conversion in C (Replies: 8)

Loading...