Introduction

Terminology:


C++ Programs

C++ Program Basics:


Symbols

Special Symbols:

Special Symbol Examples:
+     ? 
-     ,
*     <=
/     !=
.     ==
;     >=  
Word Symbols: Word Symbol Examples:
int
float
double
char
void
return  

Identifiers

Using Identifiers:

Legal Identifier Examples:
first
conversion
payRate
Illegal Identifier Examples:
employee Salary (cannot use space)
Hello! (cannot use special characters, like exclamation mark)
one+two (cannot use special characters, like + character)
2nd (cannot begin with digit)

A Caution: reserved words, "keywords," and predefined identifiers...

Here is a list of all the reserved words in Standard C++, and a few predefined identifiers for the sake of comparison.

There is a distinction between reserved words and predefined identifiers, which are sometimes collectively referred to as keywords. Nevertheless, be aware that the terminology is nonstandard. As an illustration, some authors use keyword in the same sense that others use reserved word.

C++ Reserved Words

The reserved words of C++ may be conveniently placed into several groups. In the first group, we put those that were also present in the C programming language and have been carried over into C++. There are 32 such reserved words:
auto   const     double  float  int       short   struct   unsigned
break  continue  else    for    long      signed  switch   void
case   default   enum    goto   register  sizeof  typedef  volatile
char   do        extern  if     return    static  union    while
There are another 30 reserved words that were not in C, are therefore new to C++:
asm         dynamic_cast  namespace  reinterpret_cast  try
bool        explicit      new        static_cast       typeid
catch       false         operator   template          typename
class       friend        private    this              using
const_cast  inline        public     throw             virtual
delete      mutable       protected  true              wchar_t
The following 11 C++ reserved words are not essential when the standard ASCII character set is being used, but they have been added to provide more readable alternatives for some of the C++ operators, and also to facilitate programming with character sets that lack characters needed by C++.
and      bitand   compl   not_eq   or_eq   xor_eq
and_eq   bitor    not     or       xor

Note that your particular compiler may not be completely up-to-date, which means that some (and possibly many) of the reserved words in the preceding two groups may not yet be implemented.

Some Predefined Identifiers

Beginning C++ programmers are sometimes confused by the difference between the two terms reserved word and predefined identifier, and some potential for confusion.

One of the difficulties is that some keywords that one might "expect" to be reserved words are not. The keyword main is a prime example, and others include things like the endl manipulator and other keywords from the vast collection of C++ libraries.

For example, you could declare a variable called main inside your main function, initialize it, and then print out its value (but ONLY do that to verify that you can!). On the other hand, you could not do this with a variable named else. The difference is that else is a reserved word, while main is "only" a predefined identifier.

Here is a short list of some predefined identifiers:

cin   endl     INT_MIN   iomanip    main      npos  std
cout  include  INT_MAX   iostream   MAX_RAND  NULL  string
References: http://www.cppreference.com/keywords/index.html
http://msdn.microsoft.com/en-us/library/2e6a4at9(VS.80).aspx

Data Types

Using Data Types:

Simple Data Types (three categories):
  1. Integral: integers (numbers without a decimal)
  2. Floating-point: decimal numbers
  3. Enumeration type: user-defined data type
Integral Data Types (further classified):
  1. char
  2. short
  3. int
  4. long
  5. bool
  6. unsigned char
  7. unsigned short
  8. unsigned int
  9. unsigned long
int Data Type:
  1. Numbers with no decimal point
  2. Positive integers do not require + sign in front of them (but, can include +)
  3. No commas are used within integer
  4. ***Commas used for separating items in a list***
int Data Type Examples:
-6728
0
78
bool Data Type:
  1. Two values: true and false, called logical (or Boolean) values
  2. An expression that evaluates to true or false called logical (Boolean) expression
  3. In C++, bool, true, and false are reserved words
  4. Older compilers do not include bool data type
char Data Type:
  1. Smallest integral data type
  2. Can hold numeric values -128 to 127
  3. Used to represent characters: letters, digits, and special characters
  4. char data type can represent each character on keyboard
  5. char data values represented within single quotation marks (e.g., 'r')
  6. Blank is represented as ' ' (space between single quotes)
char Data Type Examples:
 'A', 'a', '0', '*', '+', '$', '&' 
Floating-Point Data Types:
  1. Represent numbers with decimal points (real numbers)
  2. C++ uses form of scientific notation called floating-point notation
  3. In C++, letter E (or e) represents the exponent
Floating-Point Data Types (further classified):
  1. float
  2. double
  3. long double
Floating-Point Data Type Examples:
Real Number   C++ Floating-Point Notation
75.924        7.592400E1
0.18          1.800000E-1
0.0000453     4.530000E-5
-1.482        -1.482000E0
7800.0        7.800000E3
***Some compilers, default constant float values to double, and provide a warning when assigning constant float values to float data type variables. You may want to declare float data values as double to eliminate warnings.***

string Type:

Arithmetic Operators and Operator Precedence

Using Arithmetic Operators and Operator Precedence:

C++ Mathematical Operators (in order of precedence):
1) *   multiplication
1) /   division
1) %   remainder (modulus operator)
2) +   addition
2) -   subtraction
***Operators having same level of precedence are evaluated from left to right.***

Expressions

Using Expressions:

Mixed Expression Examples:
2 + 3.5 = 5.5
6  /  4 + 3.9 = 4.9
5.4  *  2 - 13.6 + 18  /  2 = 6.200000000000001
Type Conversion (Casting): Type Casting Examples:
static_cast<float>(5) = 5.0
int(5.5) = 5 //C-style typecasting
int('A') = 65 //C-style typecasting
***Remember: char data type yields an integral value. Review (ASCII table).***

Input, Memory, and Data

Using Input:

Allocating Memory (Constants and Variables):
Four characteristics of variables:
  1. Name
  2. Type
  3. Size
  4. Value
Syntax for declaring variables to allocate memory:
dataType identifier;

Examples:
int myVar; //declares one int variable
float myFloat, myFloat2, myFloat3; //declares three float variables
Syntax for declaring named constants to allocate memory (uses keyword const and must assign value):
const dataType identifier = value;

Example:
const double PI = 3.14;
Storing Data into Variables (declaring and initializing variables): Assignment Statement Examples:
variable = expression;

myVar = 10; //assigns value of 10 to myVar

int i; //allocates memory for variable i
i = i + 2; //evaluates i (right side), adds two to it, and assigns new value to memory location i (left side)
Initialization Statement Examples:
int variable = expression;

int myVar = 10; //allocates memory for variable myVar, and assigns value of 10 to myVar

//declares two double variables (myDouble and myDouble3), initializes myDouble2 with value of 25.5
double myDouble, myDouble2=25.5, myDouble3;
Input (Read) Statement: Input (Read) Statement Examples:
cin >> variableName >> variableName. . .;

cin >> miles; //computer gets value from standard input device, and places value in memory cell named miles

cin >> feet >> inches; //input multiple values into multiple memory locations with single statement  
Problems Using Mixed Values:

Increment and Decrement Operators

Using Increment and Decrement Operators:

Prefix and Postfix Operator Differences:

Output

Using Output:

Escape Sequence Examples:
  \n	//Newline: cursor moves to beginning of next line
  \t	//Tab: cursor moves to next tab stop
  \b	//Backspace: cursor moves one space left
  \r	//Return:	cursor moves to beginning of current line (not next line)
  \\	//Backslash: backslash printed
  \'	//Single quotation: single quotation mark printed
  \"	//Double quotation: double quotation mark printed
Preprocessor Directives: Using cin/cout and namespace: Commonly used header files:
<iostream>  //input/output 
<cmath>  //math functions
<string>  //string functions
<iomanip>  //formatting manipulations for input/output
Using string Data Type:

Creating a C++ Program

Putting It Together:

C++ Program Form and Style: Simple Program: Summary: Sample Program Code

Compiling and Running a Program