Elements of a simple C++ program
- Documentation : Comment using //
- // : for rest of line
- /* start of any length comment and end */
- Your name
- Class Period
- Due date of assignment
- Program description
- Variable dictionary of all variables declared and used by this module
- Description of interface to other modules
- Inclusion of libraries -- #include <iostream.h>
- Name of "main" function : main()
- Precede by int
- Precede by void
- Use brackets around code
- { indicates beginning of code segment
- } indicates end of code segment
- Print or display text : cout << "message";
- Terminate statement with ;
- Use << with cout
- (Use >> with cin)
- Tabs : \t
- Carriage return : \n
- Return value to environment : return 0;
Programming Style
- Document!!
Program descriptions
Use of variables
- Use whitespace
blank spaces around operators (+, =, ...)
blank lines "around" sections of code
More to come about this in next chapter...
Program Format
- #include directive
- using namespace
- main
- code in the scope of main
- end of main
Variable declaration
- Variables must be declared before they can be used.
- Type followed by name of variable (identifier)
Types are reserved words such as int
- Identifiers
- Consist of letters, digits and _
- Begin with letter or _ (not good idea)
- Case sensitive (a1 and A1 are different)
- Cannot be a keyword or reserved word
- No limit to length. For compatibility use 31 characters
- Use meaningful names
- All caps indicates a constant
- Beginning with _ indicates a system defined variable
- Declarations
- Integer declaration
int length, width;
char shirt_size
- Placement of variable declaration : almost anywhere
- For now, place them immediately after {
- Results in storage allocation from RAM
Types can be:
- Integer is 4 bytes -2,147,483,647 to +2,147,483,647. int
- Short integer is 2 bytes -32,767 to 32,767. short int or short
- Long integer is usually 4 bytes. long int or long
- Floating point is 4 bytes. 10-38 to 1038.
float
- Double is 8 bytes. 10-308 to 10308.
double
- Long double is 10 bytes. 10-4932 to 104932.
long double
- Character is 1 byte, 256 ASCII values. char
- Boolean is 1 byte. true or false.
bool
- C++ string Class
- #include <string>
- string myTeam;
- myTeam = "The Cleveland Browns";
DEEP DIVE on types
Types are extremely important in non-scripting languages such as C++ and Java
Characters and Strings
- 5 is an integer, '5' is a character, and "5" is a string
- Characters (i.e., can only hold a single character
- Strings can hold 1 or more characters
- The end of line character looks like 2 characters to us, but is interpreted
as one '\n'.
- The tab character is '\t'.
- There are other special backslash characters, but the tab and newline are
used frequently. (See Table 2-2 of your text)
- Use single quotes around a single letter
- Single letters: 'A', '1', '-'
- Single letters include escape characters: '\n' and '\t'
- The null string is represented as "" but really has a hidden character
in it known as the end of string
- To use strings, you must use the include directive and the string header
- #include <string>
- \ is an escape character and can be used to get s ' \ "
- \' \\ \"
- Casting can be done between characters and numerics
- char c=68.5 assigns the character at ascii code 68 to c - the letter 'D'
- int i ='D' assigns the integer 68 to i
- int j = 2 + 'a' adds 2 to 97 which is the ascii charcter code for 'a'. Thus 99 is assigned to j
- Such implicit type casting is not recommended as it is difficult to read
and figure out. If you use it, DOCUMENT IT!
Numbers
- 5L is a long integer which currently holds the same values as an integer
- 5LL is a very long integer and gives you a larger range
- float and double permit decimal numbers
- There is a mathematical difference between 5 and 5.0
- Computers have a physical limit on the number of decimal places
- Scientific notation are number such as 3.76E2 which is 3.7 time 10 squared
or 3.7 X 100 or 370
Assignment statement
- General format: Variable = Expression
- perimeter = length + length + width + width;
- Semantics
- evaluate the right hand side (rhs)
- use operator precedence
- use type rules for operators
- possibly typecast result to the lhs type
- place result in memory for variable
- Potential error
- Incorrect types for operators
- Division by 0
- Typecasting results in loss of precision
- Not enough room in memory (type mismatch)
Initializations
- You can initialize a variable when you declare it
- If not initialized, whatever is it RAM at that location is used
- int i=0;
- int i=1, j=0;
- double x=3.14;
Named Constants
- A permanent data value that cannot be changed
- General format: const type identifier = value
;
- Example:
- const double PI = 3.14159;
- Identifier is usually in all CAPS