Professional Documents
Culture Documents
519
520 APPENDIX A. THE ASCII CHARACTER CODES
Operators
Table B.1 lists the available C++ operators. This table also gives the sections where
the operators are first introduced since this should allow you to easily look up what an
operator does. (If there is no section number, the operator is not used in this book.)
All of the operators within a group in this table have the same precedence. A group of
operators higher up in the table has a higher precedence than a group further down the
table. All binary operators, except for the assignment operators, are left associative.
The assignment operators and unary operators are right associative. Note that it is
not true that all operators in the same group have the same associativity.
Within an expression, operators with the higher precedence are evaluated first. If
the resulting expression contains operators with the same precedence, then these are
evaluated right to left, or left to right, according to their associativity. For instance,
in the expression:
6 + 3 * 4 / 3 + 2
the * and / operators have the same highest precedence. Since they are not unary
or assignment operators, they also have the same left to right associativity. Therefore
3 * 4 is evaluated first, giving 4 for the expression 3 * 4 / 3. The whole expression
then reduces to:
6 + 4 + 2
521
522 APPENDIX B. OPERATORS
Operator Section
·· .. scope resolution 8.5, 14.3
·· .. global scope resolution 5.4
member selection 8.3
-> member selection 8.9
[] subscripting 6.2
() function call 5.1
++ postfix increment 3.1.5
-- postfix decrement 3.1.5
typeid type identification
typeid run-time type identification
dynamic_cast<>() run-time conversion 3.3.2
static_cast<> () compile-time conversion 3.3.2
reinterpret_cast<>() unchecked conversion 3.3.2
const_cast<> () const conversion 3.3.2
sizeof size of object 3.4.1
sizeof size of type 3.4.1
++ prefix increment 3.1.5
-- prefix decrement 3.1.5
- unary minus 3.1.6
() C-style cast Appendix C
! logical negation 4.2
& address-of 6.1.1
* dereference 6.1.2
new allocate 7.6.1
new [] allocate 7.6.1
delete deallocate 7.6.2
delete [] deallocate 7.6.2
- one's complement 11.1.1
->* member selection 8.10
· * member selection 8.10
* multiplication 3.1.2
/ division 3.1.3
% mod ulo (remainder) 3.1.4
« left shift 11.1.5
» right shift 11.1.5
> greater than 4.1
>= greater than or equal to 4.1
< less than 4.1
<= less than or equal to 4.1
Operator Section
+ addition 3.1
- subtraction 3.1
-- equal 4.3
!= not equal 4.3
& bitwise AND 11.1.2
- bitwise XOR 11.1.3
I bitwise OR 11.1.4
&& logical AND 4.2
II logical OR 4.2
= assignment 3.1
+= add and assign 3.4.3
-= subtract and assign 3.4.3
*= multiply and assign 3.4.3
/= divide and assign 3.4.3
%= modulo and assign 3.4.3
«= left shift and assign 11.1.6
»= right shift and assign 11.1.6
&= AND and assign 11.1.6
-= exclusive OR and assign 11.1.6
1= inclusive OR and assign 11.1.6
?: conditional operator 4.11
throw throw exception 15.2
, comma 4.9
• The inline specifier can be used to suggest that the compiler directly substitutes
the function body:
lIf you do need information specifically on C, rather than C++, then consult [4], [5] and [6].
525
526 APPENDIX C. DIFFERENCES BETWEEN C AND C++
int length;
length = 1000;
vector v(length);
dynamic_cast<>() static_cast<> 0
const_cast<> 0 reinterpret_cast<>()
• There is a distinct Boolean type, bool, together with the defined keywords true
and false.
II This is a comment.
• Structures are widely used in C and are like very restricted classes that can only
have public data members:
struct complex {
double re, im;
};
struct complex z;
z.re 11.11;
z.im = -2.13;
Structures have not been introduced since classes give a uniform notation.
• Input and output can be carried out by library functions, such as scanf 0 and
printf O. The C++ iostream library provides a safer, more convenient and
object-oriented technique.
indicates that the function has an unknown number of arguments. Since any type
checking is lost it is best to avoid defining such functions if it is at all possible.
• Whereas in C++ ' x' has the type char, in C it has the type int. As a con-
sequence, sizeof('x') is equal to sizeof(char) in C++ and sizeof(int) in
C.
• Whereas in C++ an empty argument list in a function declaration means that
there are no arguments (as does void), in C it indicates unknown arguments.
Although interpreting an empty argument list in this way is now obsolescent in
C, you may still come across it.
528 APPENDIX C. DIFFERENCES BETWEEN C AND C++
• A C++ structure, struct, is a synonym for a class in which all of the members
are public by default. Since a C++ structure can have function members, it is
a significant extension of the C construct.
which prevents name mangling for this particular function. There is an alternative
form of the linkage directive for more than one function:
extern "C" {
double old_C_functionCdouble x);
int ancient_C_functionCchar *pt);
}
Bibliography
[2] Grady Booch, Robert Martin and James Newkirk. Object-Oriented Analysis and
Design with Applications. Addison Wesley Longman, 2000.
This book is about design, rather than language, and is not specifically about
numerical applications. It is recommended reading if you are designing large ap-
plications using object-oriented techniques.
[3] Margaret A. Ellis and Bjarne Stroustrup. The Annotated C++ Reference Manual.
Addison Wesley Longman, 1990.
This is very much a reference manual rather than a tutorial. It is a good, well
structured, source of information on the details of why various features of the
language are defined as they are. It is certainly not an easy book for the beginner
nor is there any discussion of the Standard Library. Note that it pre-dates the
approval of the ANSI Standard.
[4] Al Kelly and Ira Pohl. A Book On C. Addison Wesley Longman, 1998.
This is an excellent introduction to C and is recommended if you need to use those
aspects of C++ that are included to provide backward compatibility with C.
[6] Andrew Koenig. C Traps and Pitfalls. Addison Wesley Longman, 1998.
A very readable description of some of the features of C that can trap the pro-
grammer. Most of the examples are also relevant to C++; those that are not serve
to demonstrate what you are missing.
529
530 BIBLIOGRAPHY
[8] Robert Robson. Using the STL: the C++ standard template library. Springer-
Verlag, New York, 1999.
A very good description of the STL, but without any emphasis on numerical
applications.
[9] James T. Smith. C++ Toolkit for Scientists and Engineers, Second Edition.
Springer-Verlag, New York, 1999.
A description of useful application packages rather than the language itself. The
applications include scalar equations, vectors, polynomials and matrices.
[10] Bjarne Stroustrup. The C++ Programming Language, Third Edition. Addison-
Wesley, Reading, Massachusetts, 1997.
This is the definitive text book on C++ by the designer of the language. There
is no emphasis on numerical applications and it is certainly not for beginners.
However, the book does contain lots of useful insights, and the chapters on design
and development are particularly valuable.
The following references provide the background for many of the applications that
appear in this book. With one exception, they are not directly relevant to C++ and
some of them are quite specialized:
[12] R.B.J.T. Allenby and E.J. Redfern. Introduction to Number Theory with Comput-
ing. Edward Arnold, 1989.
This is a delightful introduction to number theory and a useful background to
some of our programming examples.
[14] William H. Press, Brian P. Flannery, Saul A. Teukolsky and William T. Vetterling.
Numerical Recipes in C, The Art of Scientific Computing. Cambridge University
Press, 1993.
This is a very readable source of information on numerical techniques.
! see logical negation operator < < see inserter; insertion operator; left
!= see inequality operator shift operator
# see preprocessor operator < <= see left shift and assign operator
## see preprocessor operator <= see less than or equal operator
% see modulus operator < > see template
%= see assignment operator = see assignment operator
& see address operator; bitwise AND > see greater than operator
operator; reference declarator >= see greater than or equal operator
&& see logical AND operator > > see extraction operator; extractor;
&= see AND and assign operator right shift operator
operator > >= see right shift and assign operator
" see string '1: see conditional expression operator
o see function call operator; parentheses [ 1 see square brackets; subscripting
* see dereferencing operator; indirection operator
operator; multiplication \ see backslash; escape sequence
operator \" see double quote
*- see multiply and assign operator \' see single quote
+ see addition operator; unary operator, \? see question mark
\ \ see escape sequence, backslash
plus
\a see alert
++ see increment operator
\ b see backspace
+= see add and assign operator \f see form feed
, see comma operator
\n see new line
- see subtraction operator; unary
operator, minus
\r see carriage return
\ t see horizontal tab
- - see decrement operator \ v see vertical tab
- = see subtract and assign operator , see bitwise exclusive OR operator
- > see class member access operator -= see exclusive OR and assign operator
- >* see pointer-to-member operator {} see block; braces; class declaration;
· see class member access operator enum declaration; function
·* see pointer-to-member operator body; initialization list
· .. see ellipsis see bitwise inclusive OR operator
/ see division operator 1= see inclusive OR and assign operator
/* */ see comment II see logical OR operator
/ / see comment - see bitwise complement operator
/ = see divide and assign operator
: see class access specifier; field A
declaration; statement label a. out 2
:: see scope resolution operator abortO 436
::* see pointer-to-member declarator Abramowitz, Milton 530
; see separator; statement terminator abstract class 340-346
< see less than operator constructor for, 353
531
532 INDEX
o
assignments, 39
inheritance, 375-400
derivation list, 376 object 198
multiplication -oriented, xv, 6, 92
integer, 23 code, 233
operator, 33, 522 initialization
floating point, 33 auto, 99
multiply and assign operator 39, 523 static, 100
mutable 12 oct 501
octal constant 14, 15
N of stream class 502
name one's complement operator 308, 522
clashes, 401-403 one-dimensional array 127-129
mangling, 234, 528 open mode options 504
namespace 401-419 operands
accessing member, 404-405 order of evaluation of, 71-72
alias, 406-407 type conversion, 260
creating, 403-406 operator 16
nested,406 addition, 33
std, 411-412 address-of, 120
unnamed, 412-413 alternative representation of, 13
using declaration, 409-411 assignment, 39-40
using directive, 407-409 associativity, 521
NaN 33,326 binary, 26
far 12 bitwise, 307-310
nested comments 10 AND,308
alternative to, 76 assignment, 310
<new> 475 complement, 308
new 12, 171-179, 522, 526, 527 exclusive OR, 308
exception handling, and, 437 inclusive OR, 309
new line 15, 519 class member access, 199, 215
Newkirk, James 529 comma, 69
Newton-Raphson 81, 196 conditional expression, 71
noboolalpha 501 const, 252
normalized number 326 decrement, 25-26, 33
noshowbase 501 postfix, 25
noshowpoint 501 prefix, 25
noshowpos 501 delete, 262, 526, 527
noskipws 501 dereferencing, 120-121
not 13 division, 33
Not a Number 33, 326 equality, 48-50
not equal operator 48-50, 523 extraction, 24
not_eq 13 function, 262-269
nouni tbuf 501 explicit call, 262
nouppercase 501 greater than, 45
NULL 127 or equal, 45
null 519 increment, 25-26, 33
character, 15 postfix, 25
pointer, 126, 173 prefix, 25
statement, 70 indirection, 120-121
540 INDEX
directive, 407-409 w
<utility> 476 wchar_t 12
while 12, 60-61
V white space 10, 11
<valarray> 479 writeO 494
<vector > 477 ws 501
vector < bool> 482- 486
vertical tab 15, 519 x
Vetterling, William T. 530 xor 13
virtual 12 xor_eq 13
base class, 381-385, 388
function, 336-340 Z
overriding of, 338 zero 126
void 12, 89 divide by, 23, 33
void* 135
volatile 12