CP14_Cpp - Short introduction to C+ C+ short reference for...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 C++ short reference for introductory computational physics Short introduction to C++ Structure of a program Variables, Data Types, and Constants Operators Basic Input/Output Control Structures Functions Arrays Input/Output with files Pointers Classes Reference books …and thousands more!!! Good practice Have a good reference book for the version of C++ you are using. Refer to this book frequently to be sure you are aware of the rich collection of C++ features and you are using these features correctly. Programming tips Some books* have very practical advice on # Good programming practices # Common programming errors # Performance tips # Software engineering observations # Testing and debugging tips * C++ how to program , Deitel & Deitel have hundreds of valuable tips. Part 1 Structure of a program
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 // Simple program //comment #include <iostream.h> //header int main() //main (function) { int x, y; //declarations x = 2; y = x + 2; cout <<" x = "<< x <<" x + 2 = "<< y << endl; return 0; } x = 2 x + 2 = 4 statements more complex structure involves programmer-defined functions, control statements, classes, communication with files, … Free-format language C++ is a free-format language like many other languages. The compiler ignores ALL spaces, tabs, and new-line characters (also called “white spaces”) The compiler recognizes “white spaces” only inside a string. Using white spaces allows to better visualize a program structure (e.g. extra indentation inside if statements or for loops) . Common structure of a program 1. Comments 2. Header files 3. Declare variables 4. Declare constants 5. Read initial data 6. Open files 7. CALCULATIONS (include calling other functions) 8. Write results 9. Closing 10. Stop Steps 5 – 9 may call other modules Part 2 Variables, Data Types, and Constants Variables, Data Types and Constants Identifiers (names of variables) Fundamental data types Declaration of variables Global and local variables Initialization of variables Constants Variables Variable is a location in the computer’s memory where a value can be stored for use by a program.
Background image of page 2
3 Identifiers – Names of variables A variable name is any valid identifier . An identifier is a series of characters consisting of letters , digits , and uderscore ( _ ) that does not begin with a digit. C++ is case sensitive – uppercase and lowercase letters are different. Examples: abc velocity_i Force_12 Identifiers: reserved key words These keywords must not be used as identifiers! C and C++ keywords auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while Identifiers: reserved key words II C++ only keywords asm bool catch class const_cast delete dynamic_cast explicit false friend inline mutable namespace new operator private protected public reinterpret_cast static_cast
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/29/2009 for the course PHYSICS 811 taught by Professor Godunov during the Fall '09 term at Old Dominion.

Page1 / 16

CP14_Cpp - Short introduction to C+ C+ short reference for...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online