Summary of Specific C++ Knowledge You Are Expected to Learn Quickly
This document contains a summary of most of the C++ features you are expected to learn quickly, given that you are familiar with Java. It may be that you are already reasonably comfortable with C++, but even if this is the case there are likely some things you have forgotten or never knew, so it might be dangerous to be too complacent. For brevity, the material here is mostly descriptive, but you should also study the additionally available separate documents containing many examples of C++ code illustrating the topics and ideas described here, including proper formatting. If something is mentioned here that you don't understand, and for which you cannot find an illustrative example in the supplied sample code, ask your instructor about it.
For the sake of completeness, all current C++ standard libraries are listed here, as well as (nearly) all C++ reserved words, but you are not expected to be familiar with the complete list of either.
cassert cctype cerrno cfloat ciso646 climits clocale cmath csetjmp csignal cstdarg cstddef cstdio cstdlib cstring ctime cwchar cwctype
// 10 header files related to "streams" fstream iomanip ios iosfwd iostream istream ostream sstream streambuf strstream // 4 "language support" header files exception new stdexcept typeinfo // 6 "odds and ends" header files bitset complex limits locale string valarray
algorithm deque functional iterator list map memory numeric queue set stack utility vector
#include
compiler directive, not even for the legacy
header files from the Standard C Library.
You should try to avoid mixing use of the
old-style and new-style header files.
Where possible, use the new-style header files,
and since the contents of these header files
are in the "standard namespace", you must
insert, following your #include
directives, the statement:
using namespace std;
You are expected to be thoroughly familiar with this "cycle". The actual steps involved in this process depend, of course, on the nature of your programming environment, and one or more of the steps might even be "hidden". You are expected to be reasonably familiar with how it goes in the Microsoft Visual C++ world.
Remember, as an aside, that the compile step also involves a preliminary preprocessor step that is generally "invisible" to the programmer.
There are two kinds of comments in C++, and you should have some idea of when to use each kind:
// C++ comments like this for single-line or end-of-line remarks. /* C-style comments like this are more suitable for comments that extend over multiple lines, or that must be inserted in the middle of code. Remember that C-style comments cannot be nested. */
The most commonly used of these simple
data types are shown in a monospace, bold
font like this
. Remember that
the number of bytes occupied by a value
of each of these data types in the computer's
memory is a system-dependent feature of
each C++ implementation.
char
'A', 'B', 'c', 'd', '?', '/', '\n', '\t', ...
short
, int
, long
... –3, -2, -1, 0, 1, 2, 3 ...
float
, double
,
long double
23.789, -2.1E+3, 4e-1
bool
bool
data
type are true
and false
.
enum
enum ColorType {RED, WHITE, BLUE};
Some notes on simple data types:
char
,
short
, int
and long
are all referred
to as integral types
in C++. The only odd thing about this is
that char
is usually (in other programming
languages) regarded as a type separate
and apart from any integral type. This close
association in C++ can be either convenient
or troublesome, depending on how
careful you are (when converting
between types, for example).
struct
, class
,
one of the two string
types available (all of which are mentioned below)
char
whose data contents must be
terminated by the character '\0'
string
class
struct
struct
should be used whenever
you have the choice of a
struct
or a class
and
the structure has data members but no function
members. We shall subscribe to the latter school
of thought.
public
protected
private
static
members
friend
functions and
overloaded operator functions (especially for I/O)
cin >> n >> x >> c >> name;You must also know how each of the following works (and differs from cin >>):
cin.get(c); // Read a character cin.get(cString, n+1); // Read a string cin.getline(cString, n+1); // Read a line of text cin.ignore(80, '\n'); // Ignore remaining input on the line getline(cin, cppString); // Read a line of text into cppString
endl
would terminate the output line, in the
sense that the next value displayed will appear on
a new line of output:
cout << n << x << c << name << "and some text" << endl;
endl
setw
setprecision
In the following table the operators are grouped by conceptual category:
+ - * / % ++ -- (typecast)
= += -= *= /= %=
< <= > >= == !=
! && ||
. -> [] * (pointer dereference)
:: sizeof () (function call) new delete ?: &
The same operators shown above are repeated below, but this time grouped by precedence, highest to lowest, with those operators on the same line, or on immediately adjacent lines, having the same precedence:
:: . -> [] ()(function call) ++ -- unary + and - * (i.e., pointer dereference) (typecast) ! & new delete new[] delete[] sizeof * / % + - < <= > >= == != && || ? : = += -= *= /= %=
Some operator notes:
++
or --
anywhere but in a stand-alone-statement.
&&
and ||
.
=
when
you mean ==
, and watch
out especially for this in if-statements.
You need to understand the difference between an expression and a statement.
You also need to understand how the data type of the result is determined when an expression containing values of different data types is being evaluated.
You need to understand the
bool
data type
and how boolean values (constants, variables
or expressions) are used to test conditions
in both decision-making constructs and looping
constructs. You must understand the use of
the six relational operators and the three
boolean operators to form both simple and
compound boolean expressions and know how
to evaluate those expressions. You must also
understand how short-circuit evaluation
of certain boolean expressions works.
You need to understand that in the C programming language the values 0 and 1 (or, more generally, 0 and anything not 0) were used as "true" and "false" and that for reasons of backward compatibility this still holds in C++.
You need to understand how to use the various forms of the if-statement, including the if..else-statement and nested if-statements, as well as the switch-statement. You should know when a nested if-statement can be replaced by a switch-statement and when it cannot. You must be able to choose the most appropriate of the various possibilities for performing any given test. In particular, you must (for example) be able to write code to
Even more important, you must be able to recognize which of the above situations (or other similar ones) you are in at any given time, before you begin to write the corresponding code.
You need to understand how to use the three C++ looping constructs
as well as the reasons for choosing one rather than another to perform a particular loop. This implies an understanding of the following terms and concepts:
You must also understand that if a loop is controlled by a compound boolean expression, then it is often the case that several tests need to be made after the loop has terminated to see why it ended (so that appropriate action may then be taken). In particular, you must be able to write loops to
Even more important, you must be able to recognize which of the above situations (or other similar ones) is most relevant to the problem you are trying to solve, before you begin to write the corresponding code.
You need to understand how each of the following statements alters the flow of control within a loop:
break; // Control transfers to the statement immediately following // the loop within which the break statement occurs continue; // Control transfers immediately to the beginning of the // next iteration of the loop within which the continue // statement occurs
You must be able to recognize and code properly those situations which call for either a nested decision-making structure or a nested loop structure or, for that matter, some combination of two or more of any of the structures for decision-making and looping.
You need to understand and appreciate that functions help make your programs modular, readable, and maintainable. A function encapsulates a "logical chunk" of related code that accomplishes a particular goal for the program. Each function should perform one simple task, such as computing and returning a value, or displaying a menu.
A function is declared to say
that it exists somewhere and it is
going to be used. A function is
defined to show how it does
what it does, and to reserve memory for
the code that actually does it.
A function is called
(or invoked) to actually do
what it does. A function declaration
may be a full function definition or just
a function prototype. Function prototypes are
placed before the main
function, and
function definitions after main
, at
least in a single-file program.
Functions may be classified into the following two categories:
Functions may also be classified into the following two categories:
void
A void function is generally the best choice for "performing a task" that does not require any value to be "returned", though void functions may well return values to their caller via reference parameters. Use a value-returning function if the function's main or only job is to obtain or compute a single value of some kind and return that value to the caller.
A function definition always has a header and a body, and may or may not have a parameter list. The corresponding function prototype may be obtained by taking the function header and adding a semicolon at the end. You can even leave out the parameters in the function prototype (but not their types).
If present, the parameter list of a
function consists of one or more
type_name parameter_name
pairs separated by commas within the
parentheses following the function name. The
parameter names are optional in the
prototype but necessary in the header of the
definition. The parameters in a function's
parameter list form its
interface with the rest
of the program, i.e., its way of receiving
information from, and sending information back
to, the rest of the program when that
function is called. Parameters
thus fall into three
conceptual categories,
according to purpose:
Parameters may also be classified into the following two categories:
C++ offers the following two parameter-passing mechanisms:
&
)
following the typename in a function
prototype or header is the sign of a reference
parameter, except in the case of an
array, which is always passed by reference.
You should understand that value parameters are used for "safety", reference parameters are used for "efficiency", and constant reference parameters are used for both when the thing being passed is large data structure.
You should be familiar with the following functions from the C++ Standard Library. In each case the name of the library is given first in angle brackets, followed by the names of the functions from that library.
<cctype> isalnum isalpha isdigit islower isupper tolower toupper <cmath> exp log pow sqrt fabs <cstdlib> abs atoi exit rand srand system <cstring> strcat strcmp strcpy strlen
Also, although you are not expected to
know offhand any of the values of the system-
dependent constants defined in the <cfloat>
and <climits>
libraries, you should at least
be aware of the existence of these libraries
and their contents (in a general way).
You should know when and how to overload a function, as well as how to use default parameters with a function, and be able to decide whether one or the other should be used in a given situation.
You should have a basic understanding of what a template function is, as well as when and how to define and use one.
You should be familiar with and understand the following pointer concepts:
new
and delete
with simple variables and dynamic arrays
NULL
pointer
this
pointer
new
and
delete
.
main
function, or variables declared to be static
.
auto
) variable, an
extern
variable, and a static
variable.
These represent three different storage
classes in C++.
{ }
For
example, the body of a main
function (or any
other function) is a block, and loop
bodies are often blocks of code.
#include
or #define
before compilation starts.
const
in each of the following contexts:
static
applied to a local
variable inside a function.
typedef char LineType[81];
62 == 076 == 0x3E
)
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
asm bool catch class const_cast delete dynamic_cast explicit false friend inline mutable namespace new operator private public protected reinterpret_cast static_cast template this throw true try typeid typename using virtual wchar_t
The following is a far-from-exhaustive list of typical C++ programming errors. You should be aware of all of them, as well as how each error is reported or tends to show up on your system, and what you should try to do to avoid that error in the first place.
#include
one or more required header files
using
statements for the
namespaces used
;
)
=
when you
need ==
in a condition
float
or
double
quantities for equality
&
or |
when you
need &&
or ||
switch
struct
or
a class
const
that should be constant
NULL
pointer