Thursday, November 14, 2013

C++ Class 2: A Simple Program

A Simple Program:

Printing a Line of Text

  • Comments
    • Document Programs
    • Improve program readability
    • Ignored by compiler
    • Single-line comment
      • Begin with //
    • Multiple-line comment
      • Everything between /* and */ is ignored
  • Preprocessor directives
    • Processed by preprocessor before compiling
    • Begin with #
      • #include
      • #define

C++ Preprocessor

  • C++ Compilers automatically invoke a preprocessor that takes care of #include statements and some other special directives.
  • You don't need to do anything special to run the preprocessor - it happens automatically.

The Preprocessor

  • Lines that start with the character '#' are special instructions to a preprocessor.
  • The preprocessor can replace the line with something else:
    • include: replaced with contents of a file
  • Other directives tell the preprocessor to look for patterns in the program and do some fancy processing.

Preprocessing

  1. C++ Program
  2. C++ Preprocessor
  3. Temporary file (C++ program)
  4. C++ Compiler
  5. Executable Program
Code
Click on image to enlarge it

Output of above program:

Output
Click on image to enlarge it
The program was written in Microsoft Visual C++ 6.0
You can use any compiler which you find easy to use but I suggest you to use Microsoft Visual C++  6.0 or Borland in the beginning if you want to be a good programmer.

  • Output stream object
    • cout
    • "Connected" to screen
    • <<
      • Stream insertion operator
      • Value to right (right operand) inserted into output stream
  • Namespace
    • using namespace std; specifies that we are using objects from "namespace" std
  • Escape character and escape sequences
    • \ is called the "Escape Character"
    • Indicates "special" character output
Some Escape Characters:

Some Escape Characters
Click on image to enlarge it
(To be continued)

Widgets

 

Copyright @ 2014 CPP Fuzz.