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

C++ documentation

  1. Jun 18, 2008 #1
    Hi,

    I have started programming with java and now I am trying to learn C++ by myself. What a real problem for me is that I don't know which methods I am going to use for a specific problem. Is there any documentation or API that I can use for C++.
    For instance this;

    #include <iostream>
    using namespace std;
    int main()
    {
    cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
    cin.get();
    }

    How did we know that we should use iostream and cin, cout stuff.
     
    Last edited: Jun 18, 2008
  2. jcsd
  3. Jun 18, 2008 #2
    You figure out which functions (they really are called functions in C++, not methods) to use principally through experience and reading. The C++ standard library and standard template library are extremely well documented. For instance, C++ Reference is pretty helpful site.

    Alternatively, you could learn about this kind of thing from a book. A decent way to start would be to pick up an introductory C++ book like Deitel's C++: How to Program, which contains a good account of the standard library as well as a discussion of the language more generally.

    Of course, if you're really serious about understanding the standard library, the canonical book is The C++ Standard Library: A Tutorial and Reference. It's a bit pricey (as is the Deitel book) but it's something every C++ programmer will benefit from reading.
     
  4. Jun 19, 2008 #3
    Thank you Shoehorn!
    I hope I can reach the point that I want in C++ with your help.:)
     
  5. Jun 20, 2008 #4

    jtbell

    User Avatar

    Staff: Mentor

    The Deitel book is popular, but it was first written back in the 1990s, before the current C++ standard and standard library were established. I don't remember which edition I looked at most recently, but it was definitely several years after the C++ standard, and it struck me as being rather old-fashioned with respect to the modern features of the standard library. For example, it taught text manipulation first using plain old character arrays and pointers, and "tacked on" a later chapter about the standard library's "string" data type which is much easier to use correctly IMO. Similarly it favored C-style arrays in general, with the modern container data types such as vectors left until the end of the book, with a very superficial treatment.

    For a modern approach to C++ that starts out using the standard library as much as possible from the beginning, you might consider Koenig and Moo's Accelerated C++, which is a rather slim volume and not super expensive.
     
  6. Jun 20, 2008 #5
    By the way, I wonder the similarities between C and C++. If I learn C++, will I be able to code with C? Are they similar?
     
  7. Jun 20, 2008 #6
    These are indeed valid criticisms. He's got rather an idiosyncratic style and many of his techniques in teaching C++ seem to favour approaches that would be more at home in C. He's also tends to be slightly too verbose when discussing examples.

    Nonetheless, it's a clear, well presented book and is worth a read in my opinion.

    Yup. I should probably have mentioned this in preference to Deitel. Accelerated C++ really is a great book from which to learn C++.

    C is a subset of C++. Generally speaking, this means that any well written C code should compile under any good C++ compiler. The converse, however, is not true.

    More precisely, C++ is easier to learn and easier to write than C. In fact, unless you have a very specific reason for wanting to learn C (like, say, a burning desire to program microcontrollers), I'd go so far as to say that you're better off not learning C before learning C++. Unfortunately, several methods and techniques that are commonly used by C programmers will get you into a whole lot of trouble if you attempt to use them in C++.
     
  8. Jun 21, 2008 #7

    jtbell

    User Avatar

    Staff: Mentor

    I suspect that "C++ How to Program" was derived from his earlier "C How to Program."

    To elaborate on this: For some things (two examples are terminal and file input/output, and text manipulation), C++ has two ways of doing them: a "C style" way which is inherited from C, and a "C++ style" way which is new. Bjarne Stroustrup (the original creator of C++) included the "C style" methods mainly for backward compatibility with C. His idea was that people could thereby easily migrate their existing C code to C++, while using the "C++ style" methods in new code.

    In my opinion, the "C++ style" ways usually are easier to learn, and to use in many situations. Not all... in particular, creating nicely-formatted output using C++ iostream member functions can be very tedious, as opposed to C's concise format descriptors.

    But I'd never use C-style character arrays and char* pointers for text, instead of C++'s 'string' data type, or C-style arrays instead of C++'s vectors, etc., except perhaps if I were in a situation where every last bit of efficiency was absolutely essential.
     
  9. Jun 21, 2008 #8

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Except where you need to talk to other libraries. One problem with C++ brilliant idea of leaving the CString class as 'an exercise for the student' for the first 10years of the standard is that every gui toolkit, library and OS call has their own string type and generally the only common way between them is char*.
     
  10. Jun 21, 2008 #9
    This is one example (among many others) of why it's a good idea to use the Boost libraries. They really are superb.
     
  11. Jun 22, 2008 #10

    jtbell

    User Avatar

    Staff: Mentor

    <nitpick>

    There was no C++ standard until 1998, and that standard included string, vector, etc.

    </nitpick>

    OK, one can argue that there was a "de facto standard" defined by the earlier editions of Stroustrup's "The C++ Programming Language." There was another book, with "Annotated" in the title, but I can't remember the rest of it.
     
  12. Jun 22, 2008 #11

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    There wasn't an 'ISO' C++ standard until 1998 - but the C++ programming language came out in 1985 and the C++ARM in 1990. They could have at least suggested a structure for the string lib.
    Even the string class in the std lib isn't a great design, so many people use either the STL or boost ones instead, making it even worse.

    Windows is the worst offender with you needing char*, MFC CString, BSTR and the same again for unicode and others in the same program.
     
  13. Jun 22, 2008 #12

    jtbell

    User Avatar

    Staff: Mentor

    Didn't std::string come from the STL?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C++ documentation
Loading...