C++ Notes: Summary - Basic Elements

Basics

Comments

// This comment terminates at end of line.
/* Comment (possibly many lines) terminates at */

Reserved Words

asm auto bool break case catch char class const const_cast continue default delete do double dynamic_cast else enum explicit export extern false float for friend goto if inline int long mutable namespace new operator private protected public register reinterpret_cast return short signed sizeof static static_cast struct switch template this throw true try typedef typeid typename union unsigned using virtual void volatile wchar_t while

Operators and Puctuation

() {} [] + - * / % ++ -- < <= == != >= > && || ! << >> ?: ; , . = += -= ...
& | ^ ~ :: -> ->* .*
   

Identifiers

  • Start identifiers with an alphabetic character (a-z or A-Z), and continue with alphabetic, numeric (0-9), or '_' (underscore) characters.
  • Second words in a name should start with an uppercase letter.
  • Do not use keywords (see below).
  • Constants should be all uppercase (X_AXIS, Math.PI, ...).
Simple and String Types

Integer Types

The integer types: char, short, int, long, and bool are binary integers. All except bool are signed (char is usually signed), but can be declared unsigned. Below are some typical characteristics of the basic types. The standard allows compilers to have different implementations.
Type BytesRange (signed) Range (unsigned)Literals
bool 10..1   false, true
char 1-128..+127 0..255 'a'
short2-32,768..+32,767 0..65,535 none
int 4-2,147,483,648..+2,147,483,6470..4,294,967,29523, 0xAF
long 4-2,147,483,648..+2,147,483,6470..4,294,967,29523L, 0xAFL
Operators: arithmetic, comparison, bitwise, assignment.

Floating-point Types

The floating-point types, float and double, are usually stored in IEE-754 format. Calculations may produce NaN (Not a Number); or positive or negative infinity.
Type BytesRange Accuracy Literals
float 4 -3.4E38..+3.4E38 6-7 digits 3.14F 6.02e23F
double8 -1.7E308..+1.7E30814-15 digits3.14 6.02e23
long double12??-???? digits 
Operators: arithmetic, comparison, assignment.
   

Characters

Characters are stored in one byte (char) or two bytes (wchar_t). char is an integer type.
Literals
  • 'A' // one character in single quotes.
  • Double quotes ("") are used to enclose strings which may have zero or more characters, eg, "Hello".
  • Hexadecimal '0xnn' where x is a hexadecimal digit. Eg '0x41'
  • Octal '\nnn' where n is an octal digit.
  • Escape combinations: '\n' newline, '\\' backslash, '\'' single quote, '\"' double quote, '\r' carriage return, '\t' tab, '\b' backspace, '\f' form feed.

C-strings

C-strings are arrays of chars with a terminating zero ('\0'). Enclose C-string literals in double quotes. C-strings are type char[] or equivalently char *.
   char* message = "Time to go home";
   char message[] = "Time to go home";
Assignment and concatenation operators are overloaded to allow use with string variables.

string type

This object type requires the <string> header.
   string s;
   s = s + " more";
Declarations

Simple variable declarations

Write the type, a variable name, an optional initial value, and a comment. It's possible to write a comma-separated list of variable names after the type or to omit the comment, but it's not a good style.
   int  t;               // temp in Celsius
   char separator = ','; // name separator
   unsigned short h;     // height in pixels
   

Constant declarations

Use the keyword const in front of a declaration which has an initial value. Constant names should be in uppercase characters. If the name has multiple words, they should be separated by underscore ("_").

   const int MAX_SIZE = 20; // Max number of keys.

Enumeration Types

A sequence of integer constants can be defined with enum.
enum Direction (NORTH, EAST, SOUTH, WEST);
// NORTH is 0, EAST is 1, ...
Direction myWay;