C++ Language: Version 2.2 (ANSI/ISO Standard Version) Last Updated 10/14/99
C++ Language: Version 2.2 (ANSI/ISO Standard Version) Last Updated 10/14/99
This document provides neither a complete nor rigorous description of the C++ language. It
does, however, describe the features of the language that are most useful to engineers and
scientists. These frequently used aspects of the language are described below:
Program Structure
Any text on a line after a // symbol is ignored (used for comments). Long (multiline
comments can be placed between /* and */ symbols.
Functions must be declared before use but can be defined after use. Modern C/C++ style is
to put all function definitions after main() with all declarations before main() as prototypes.
An alternate style eliminates the need for separate function declarations by placing all
definitions before main() and their first use. Function definitions can not be nested.
Identifiers (names) are case sensitive and can be of any length but typically only the first 31
characters are significant. They must start with a letter (including _) and may contain letters
and numbers. Objects names are generally all lower case. Class names generally start with an
upper case letter. Constants are generally in all upper case.
Variables can be declared anywhere before they are used use. I usually collect all
declarations at the top of each function so that they are easy to find. Some C++ programmers
declare variables just before they are used. At any rate comments should be included with all
non-trivial variable declarations describing significance, use and units (if any).
Use square brackets to indicate arrays. Arrays are declared with the number of storage
locations indicated, but array element references start at zero. Modern C++ compilers
support the string and vector container classes (declared in the and include files,
respectively). These container classes provide significant advantages over the use of arrays.
Built in Types -
Type Modifiers -
unsigned Doesn’t use sign bit (assumes int if base type is omitted).
long May have twice as many bytes as base type (assumes int if base type is omitted).
short May have half as many bytes as base type (assumes int if base type is omitted).
const Constant (values can’t be changed during execution).
Common Operators
Assignment: =
Increment and decrement: ++ (pre or post fix) and -- (pre or post fix)
Arithmetic: +, -, *, / and % (integer remainder)
Relational: == (equality), != (inequality), <, >, <= and >=
Boolean: && (and), || (or) and ! (not)
Bitwise: & (and), | (or), ^ (xor), ~ (not), << (shift left) and >> (shift right)
Control Constructs
Statements end with semicolons, i.e. ;’s. A block is a statement or two or more statements
enclosed in braces, i.e. { and }. A block can be used anywhere a statement can be used.
Statements and blocks can be spread across multiple lines.
switch (expression)
{ case value1: [block1 [break;]]
...
case valuen: [blockn [break;]]
default: [blockn+1 [break;]]
}
Standard Libraries
Many commonly used features of C and C++ are defined in the standard libraries. There is
massive overlap between the C libraries (declared in include files with names
like <libname.h> and C++ libraries (declared in include files with names like <libname>). The
C++ libraries generally contain the same functions as the corresponding C libraries but with
these functions placed in the std namespace. As a result, the following line generally should
be placed immediately following the inclusion of the C++ libraries:
using namespace std;
The following table lists the new C++ names, the old C/C++ names and some commonly
used functions of the most popular standard libraries.
String operators include: [], =, >>, <<, +, ==, !=, <, <=, > and >=.
Vector elements are numbered starting at 0. Iterators can be created by adding element
numbers to the result of the .begin() member function. Constructor example: vector<double>
a(MAX_SIZE, 0.0);
Steam I/O
Integer constants default to the smallest integer type that can hold their value. Otherwise, the
following suffixes can be used (alone or together):
u Unsigned
l or L Long
Floating point constants default to type double. Otherwise, the following suffixes can be
used:
f Float
l or L Long double
Reserved Words
These words can not be used for programmer defined symbols (names).
asm
auto
break
case
catch
char
class
const
continue
default
delete
do
double
else
enum
extern
float
for
friend
goto
if
inlineint
long
new
operator
private
protected
public
register
return
short
signed
sizeof
static
struct
switch
template
this
throw
try
typedef
union
unsigned
virtual
void
volatile
wchar_t
while
This table lists all the C++ operators in order of non-increasing precedence. An expression
involving operators of equal precedence is evaluated according to the associativity of the
operators.
<, <=, >, >= Less than, less than or equal, greater than, greater than or left to right
equal
C++ References
Horstmann, C. S. Computing Concepts with C++ Essentials, 2nd ed. John Wiley & Sons,
1999.
Barclay, K. A. and B. J. Gordon. C++ Problem Solving and Programming. Prentice Hall,
1994.
Horstmann, C. S. Mastering Object-Oriented Design in C++. John Wiley & Sons, 1995.
This document provides neither a complete nor rigorous description of the C++ language. It
does, however, describe the features of the language that are most useful to engineers and
scientists. These frequently used aspects of the language are described below:
Program Structure
Any text on a line after a // symbol is ignored (used for comments). Long (multiline
comments can be placed between /* and */ symbols.
Functions must be declared before use but can be defined after use. Modern C/C++ style is
to put all function definitions after main() with all declarations before main() as prototypes.
An alternate style eliminates the need for separate function declarations by placing all
definitions before main() and their first use. Function definitions can not be nested.
Identifiers (names) are case sensitive and can be of any length but typically only the first 31
characters are significant. They must start with a letter (including _) and may contain letters
and numbers. Objects names are generally all lower case. Class names generally start with an
upper case letter. Constants are generally in all upper case.
Variables can be declared anywhere before they are used use. I usually collect all
declarations at the top of each function so that they are easy to find. Some C++ programmers
declare variables just before they are used. At any rate comments should be included with all
non-trivial variable declarations describing significance, use and units (if any).
Use square brackets to indicate arrays. Arrays are declared with the number of storage
locations indicated, but array element references start at zero. Modern C++ compilers
support the string and vector container classes (declared in the and include files,
respectively). These container classes provide significant advantages over the use of arrays.
Built in Types -
Type Modifiers -
unsigned Doesn’t use sign bit (assumes int if base type is omitted).
long May have twice as many bytes as base type (assumes int if base type is omitted).
short May have half as many bytes as base type (assumes int if base type is omitted).
const Constant (values can’t be changed during execution).
Common Operators
Assignment: =
Increment and decrement: ++ (pre or post fix) and -- (pre or post fix)
Arithmetic: +, -, *, / and % (integer remainder)
Relational: == (equality), != (inequality), <, >, <= and >=
Boolean: && (and), || (or) and ! (not)
Bitwise: & (and), | (or), ^ (xor), ~ (not), << (shift left) and >> (shift right)
Control Constructs
Statements end with semicolons, i.e. ;’s. A block is a statement or two or more statements
enclosed in braces, i.e. { and }. A block can be used anywhere a statement can be used.
Statements and blocks can be spread across multiple lines.
switch (expression)
{ case value1: [block1 [break;]]
...
case valuen: [blockn [break;]]
default: [blockn+1 [break;]]
}
Standard Libraries
Many commonly used features of C and C++ are defined in the standard libraries. There is
massive overlap between the C libraries (declared in include files with names
like <libname.h> and C++ libraries (declared in include files with names like <libname>). The
C++ libraries generally contain the same functions as the corresponding C libraries but with
these functions placed in the std namespace. As a result, the following line generally should
be placed immediately following the inclusion of the C++ libraries:
using namespace std;
The following table lists the new C++ names, the old C/C++ names and some commonly
used functions of the most popular standard libraries.
string(int size) Constructor. Argument size is optional but recommended. Note lower
case s.
int .length() Returns the length of the string.
string .substr(int start, Returns the substring starting at location start of length size.
int size)
string& .insert(int n, Inserts a copy of s into string starting at position n. The rest of the
string s) original string is shifted right.
string& .erase(int from, Removes characters from position from to through position to from the
int to) string. Moves the rest of the string to the left. Returns the modified
string.
int .find(string ss) Returns the starting position of the first occurrence of substring ss.
getline(istream is, Places next line from is into s. The string extractor (>>) only gets
string s) "words". Not actually a member function.
String operators include: [], =, >>, <<, +, ==, !=, <, <=, > and >=.
Vector elements are numbered starting at 0. Iterators can be created by adding element
numbers to the result of the .begin() member function. Constructor example: vector<double>
a(MAX_SIZE, 0.0);
Steam I/O
The inclusion of the const modifier indicates that the function does not modify the object on
which it operates. This restriction is enforced by the compiler.
Integer constants default to the smallest integer type that can hold their value. Otherwise, the
following suffixes can be used (alone or together):
u Unsigned
l or L Long
Floating point constants default to type double. Otherwise, the following suffixes can be
used:
f Float
l or L Long double
Reserved Words
These words can not be used for programmer defined symbols (names).
asm
auto
break
case
catch
char
class
const
continue
default
delete
do
double
else
enum
extern
float
for
friend
goto
if
inlineint
long
new
operator
private
protected
public
register
return
short
signed
sizeof
static
struct
switch
template
this
throw
try
typedef
union
unsigned
virtual
void
volatile
wchar_t
while
This table lists all the C++ operators in order of non-increasing precedence. An expression
involving operators of equal precedence is evaluated according to the associativity of the
operators.
<, <=, >, >= Less than, less than or equal, greater than, greater than or left to right
equal
C++ References
Horstmann, C. S. Computing Concepts with C++ Essentials, 2nd ed. John Wiley & Sons,
1999.
Barclay, K. A. and B. J. Gordon. C++ Problem Solving and Programming. Prentice Hall,
1994.
Horstmann, C. S. Mastering Object-Oriented Design in C++. John Wiley & Sons, 1995.
This document provides neither a complete nor rigorous description of the C++ language. It
does, however, describe the features of the language that are most useful to engineers and
scientists. These frequently used aspects of the language are described below:
Program Structure
Any text on a line after a // symbol is ignored (used for comments). Long (multiline
comments can be placed between /* and */ symbols.
Functions must be declared before use but can be defined after use. Modern C/C++ style is
to put all function definitions after main() with all declarations before main() as prototypes.
An alternate style eliminates the need for separate function declarations by placing all
definitions before main() and their first use. Function definitions can not be nested.
Identifiers (names) are case sensitive and can be of any length but typically only the first 31
characters are significant. They must start with a letter (including _) and may contain letters
and numbers. Objects names are generally all lower case. Class names generally start with an
upper case letter. Constants are generally in all upper case.
Variables can be declared anywhere before they are used use. I usually collect all
declarations at the top of each function so that they are easy to find. Some C++ programmers
declare variables just before they are used. At any rate comments should be included with all
non-trivial variable declarations describing significance, use and units (if any).
Use square brackets to indicate arrays. Arrays are declared with the number of storage
locations indicated, but array element references start at zero. Modern C++ compilers
support the string and vector container classes (declared in the and include files,
respectively). These container classes provide significant advantages over the use of arrays.
Built in Types -
Type Modifiers -
unsigned Doesn’t use sign bit (assumes int if base type is omitted).
long May have twice as many bytes as base type (assumes int if base type is omitted).
short May have half as many bytes as base type (assumes int if base type is omitted).
const Constant (values can’t be changed during execution).
Common Operators
Assignment: =
Increment and decrement: ++ (pre or post fix) and -- (pre or post fix)
Arithmetic: +, -, *, / and % (integer remainder)
Relational: == (equality), != (inequality), <, >, <= and >=
Boolean: && (and), || (or) and ! (not)
Bitwise: & (and), | (or), ^ (xor), ~ (not), << (shift left) and >> (shift right)
Control Constructs
Statements end with semicolons, i.e. ;’s. A block is a statement or two or more statements
enclosed in braces, i.e. { and }. A block can be used anywhere a statement can be used.
Statements and blocks can be spread across multiple lines.
switch (expression)
{ case value1: [block1 [break;]]
...
case valuen: [blockn [break;]]
default: [blockn+1 [break;]]
}
Standard Libraries
Many commonly used features of C and C++ are defined in the standard libraries. There is
massive overlap between the C libraries (declared in include files with names
like <libname.h> and C++ libraries (declared in include files with names like <libname>). The
C++ libraries generally contain the same functions as the corresponding C libraries but with
these functions placed in the std namespace. As a result, the following line generally should
be placed immediately following the inclusion of the C++ libraries:
using namespace std;
The following table lists the new C++ names, the old C/C++ names and some commonly
used functions of the most popular standard libraries.
string(int size) Constructor. Argument size is optional but recommended. Note lower
case s.
int .length() Returns the length of the string.
string .substr(int start, Returns the substring starting at location start of length size.
int size)
string& .insert(int n, Inserts a copy of s into string starting at position n. The rest of the
string s) original string is shifted right.
string& .erase(int from, Removes characters from position from to through position to from the
int to) string. Moves the rest of the string to the left. Returns the modified
string.
int .find(string ss) Returns the starting position of the first occurrence of substring ss.
getline(istream is, Places next line from is into s. The string extractor (>>) only gets
string s) "words". Not actually a member function.
String operators include: [], =, >>, <<, +, ==, !=, <, <=, > and >=.
Vector elements are numbered starting at 0. Iterators can be created by adding element
numbers to the result of the .begin() member function. Constructor example: vector<double>
a(MAX_SIZE, 0.0);
Steam I/O
The inclusion of the const modifier indicates that the function does not modify the object on
which it operates. This restriction is enforced by the compiler.
Integer constants default to the smallest integer type that can hold their value. Otherwise, the
following suffixes can be used (alone or together):
u Unsigned
l or L Long
Floating point constants default to type double. Otherwise, the following suffixes can be
used:
f Float
l or L Long double
Reserved Words
These words can not be used for programmer defined symbols (names).
asm
auto
break
case
catch
char
class
const
continue
default
delete
do
double
else
enum
extern
float
for
friend
goto
if
inlineint
long
new
operator
private
protected
public
register
return
short
signed
sizeof
static
struct
switch
template
this
throw
try
typedef
union
unsigned
virtual
void
volatile
wchar_t
while
This table lists all the C++ operators in order of non-increasing precedence. An expression
involving operators of equal precedence is evaluated according to the associativity of the
operators.
<, <=, >, >= Less than, less than or equal, greater than, greater than or left to right
equal
C++ References
Horstmann, C. S. Computing Concepts with C++ Essentials, 2nd ed. John Wiley & Sons,
1999.
Barclay, K. A. and B. J. Gordon. C++ Problem Solving and Programming. Prentice Hall,
1994.
Horstmann, C. S. Mastering Object-Oriented Design in C++. John Wiley & Sons, 1995.
This document provides neither a complete nor rigorous description of the C++ language. It
does, however, describe the features of the language that are most useful to engineers and
scientists. These frequently used aspects of the language are described below:
Program Structure
Any text on a line after a // symbol is ignored (used for comments). Long (multiline
comments can be placed between /* and */ symbols.
Functions must be declared before use but can be defined after use. Modern C/C++ style is
to put all function definitions after main() with all declarations before main() as prototypes.
An alternate style eliminates the need for separate function declarations by placing all
definitions before main() and their first use. Function definitions can not be nested.
Identifiers (names) are case sensitive and can be of any length but typically only the first 31
characters are significant. They must start with a letter (including _) and may contain letters
and numbers. Objects names are generally all lower case. Class names generally start with an
upper case letter. Constants are generally in all upper case.
Variables can be declared anywhere before they are used use. I usually collect all
declarations at the top of each function so that they are easy to find. Some C++ programmers
declare variables just before they are used. At any rate comments should be included with all
non-trivial variable declarations describing significance, use and units (if any).
Use square brackets to indicate arrays. Arrays are declared with the number of storage
locations indicated, but array element references start at zero. Modern C++ compilers
support the string and vector container classes (declared in the and include files,
respectively). These container classes provide significant advantages over the use of arrays.
Built in Types -
Type Modifiers -
unsigned Doesn’t use sign bit (assumes int if base type is omitted).
long May have twice as many bytes as base type (assumes int if base type is omitted).
short May have half as many bytes as base type (assumes int if base type is omitted).
const Constant (values can’t be changed during execution).
Common Operators
Assignment: =
Increment and decrement: ++ (pre or post fix) and -- (pre or post fix)
Arithmetic: +, -, *, / and % (integer remainder)
Relational: == (equality), != (inequality), <, >, <= and >=
Boolean: && (and), || (or) and ! (not)
Bitwise: & (and), | (or), ^ (xor), ~ (not), << (shift left) and >> (shift right)
Control Constructs
Statements end with semicolons, i.e. ;’s. A block is a statement or two or more statements
enclosed in braces, i.e. { and }. A block can be used anywhere a statement can be used.
Statements and blocks can be spread across multiple lines.
switch (expression)
{ case value1: [block1 [break;]]
...
case valuen: [blockn [break;]]
default: [blockn+1 [break;]]
}
Standard Libraries
Many commonly used features of C and C++ are defined in the standard libraries. There is
massive overlap between the C libraries (declared in include files with names
like <libname.h> and C++ libraries (declared in include files with names like <libname>). The
C++ libraries generally contain the same functions as the corresponding C libraries but with
these functions placed in the std namespace. As a result, the following line generally should
be placed immediately following the inclusion of the C++ libraries:
using namespace std;
The following table lists the new C++ names, the old C/C++ names and some commonly
used functions of the most popular standard libraries.
string(int size) Constructor. Argument size is optional but recommended. Note lower
case s.
int .length() Returns the length of the string.
string .substr(int start, Returns the substring starting at location start of length size.
int size)
string& .insert(int n, Inserts a copy of s into string starting at position n. The rest of the
string s) original string is shifted right.
string& .erase(int from, Removes characters from position from to through position to from the
int to) string. Moves the rest of the string to the left. Returns the modified
string.
int .find(string ss) Returns the starting position of the first occurrence of substring ss.
getline(istream is, Places next line from is into s. The string extractor (>>) only gets
string s) "words". Not actually a member function.
String operators include: [], =, >>, <<, +, ==, !=, <, <=, > and >=.
Vector elements are numbered starting at 0. Iterators can be created by adding element
numbers to the result of the .begin() member function. Constructor example: vector<double>
a(MAX_SIZE, 0.0);
Steam I/O
The inclusion of the const modifier indicates that the function does not modify the object on
which it operates. This restriction is enforced by the compiler.
Integer constants default to the smallest integer type that can hold their value. Otherwise, the
following suffixes can be used (alone or together):
u Unsigned
l or L Long
Floating point constants default to type double. Otherwise, the following suffixes can be
used:
f Float
l or L Long double
Reserved Words
These words can not be used for programmer defined symbols (names).
asm
auto
break
case
catch
char
class
const
continue
default
delete
do
double
else
enum
extern
float
for
friend
goto
if
inlineint
long
new
operator
private
protected
public
register
return
short
signed
sizeof
static
struct
switch
template
this
throw
try
typedef
union
unsigned
virtual
void
volatile
wchar_t
while
This table lists all the C++ operators in order of non-increasing precedence. An expression
involving operators of equal precedence is evaluated according to the associativity of the
operators.
<, <=, >, >= Less than, less than or equal, greater than, greater than or left to right
equal
C++ References
Horstmann, C. S. Computing Concepts with C++ Essentials, 2nd ed. John Wiley & Sons,
1999.
Barclay, K. A. and B. J. Gordon. C++ Problem Solving and Programming. Prentice Hall,
1994.
Horstmann, C. S. Mastering Object-Oriented Design in C++. John Wiley & Sons, 1995.