C++ Summary
First Steps:

  • Compiler
    • The C++ compiler is invoked by "CC"
    • "CC" will also compile C programs, so you can type
      CC hw.c
      and get an "a.out" as usual.
      CC Word_count.c get_word.c btree.c
      will compile Assignment 6.
  • Comments.
    • C++ supports single line comments.
      pp++; // this comment goes to the end of the line
      but does not go over to the next. If you need multi-line comments you can still use the /* ... */ from C.
  • Just in time declarations.
    • C++ requires declarations just as C does, but declarations can occur anywhere in the code, not just at the top.
      for( int ii=0; ii < argc; ii++)
      is a perfectly legal C++ for loop provided "argc" has already been declared.
    • The declaration is in effect inside the smallest { . . . } pair containing the declaration.
  • Output to the screen.
    • There is a new IO method in C++. You need a line
      #include < iostream.h >
      which is used instead of
      #include < stdio.h >
    • Printing of the standard data types.
      • To print "hello world" on a line by itself we now say
        cout << "hello world\n";
        • To print the decimal integer contained in the variable "x" on a line by itself, we now say
          cout << x <<"\n";
    • Mix and Match.
      • You can use "printf" just as before but it takes a bit of set up. You need to include both
        < iostream.h > and < stdio.h >
        and then call the function
        ios::sync_with_stdio();
        This is the C++ standard but the CC compiler seems to manage without explicitly calling sync_with_stdio.
      • You can also use the C library function "sprintf" from Appendix B. This function does the "prontf" thing but instead of printing the result, it puts it in a C-string. You can then ship the C-string to "cout".
  • Function and operator overloading.
    • Functions can be overloaded, which means that different functions can have the same name. For example, in a geometry program we might have rectangles, circles and pentagons. We might want to write functions to compute the areas of an instance of each of these figures. If rect* points to a rectangle, circ* to a circle and pent* to a pentagon then in C++ we can have three functions, all called Area, with three declarations
      double Area(rect*);
      double Area(circ*);
      double Area(pent*);
    • With a bit more work, operators can be overloaded as well so we can define a complex number data type and if c1 and c2 are complex numbers, write c1=c1+c2 and have the expected thing happen.


Math 211 homepage.
C++ Classes.
C++ Overloading.
C++ Derivation.