C++ For Science and Engineering Lecture 5

C++ For Science and Engineering
Lecture 5
John Chrispell
Tulane University
Wednesday September 1, 2010
A note on member functions
The cout.put() function incountered last lecture is the first
example of an object oriented programming concept of member
functions.
c o u t . p u t ( ch ) ;
// D i s p l a y i n g t h e c h a r ch .
c o u t . p u t ( ’ ! ’ ) ; // D i s p l a y i n g t h e ( ! ) c h a r a c h t e r .
Member functions belong to classes and describe how to
manipulate class data. More on this in the future as well.
John Chrispell,
Wednesday September 1, 2010
slide 3/35
C++ Escape Sequence Codes
Character Name
Newline
Horizontal tab
Vertical tab
Backspace
Carriage return
Alert
Backslash
Question Mark
Single quote
Double quote
John Chrispell,
Wednesday September 1, 2010
ASCI Symbole
NL(LF)
HT
VT
BS
CR
BEL
\
?
’
”
C++ Code
\n
\t
\v
\b
\r
\a
\\
\?
\0
"
slide 5/35
bondini.cpp
#i n c l u d e <i o s t r e a m >
i n t main ( ) {
u s i n g namespace s t d ;
c o u t << ” \ a O p e r a t i o n \” HyperHype \” i s now a c t i v a t e d ! \ n” ;
c o u t << ” E n t e r y o u r a g e n t code :
\b\b\b\b\b\b\b\b” ;
long code ;
c i n >> code ;
c o u t << ” \aYou e n t e r e d ” << code << ” . . . \ n” ;
c o u t << ” \ aCode v e r i f i e d ! P r o c e e d w i t h P l a n Z3 ! \ n” ;
return 0;
}
// U se s e s c a p e c h a r a c t e r s
John Chrispell,
Wednesday September 1, 2010
slide 7/35
Other Data types
The bool type named after English mathematician George Boole
who developed a mathematical notation for the laws of logic.
This gives us the Boolean variable or one that can take on only
the values of true and false.
bool i s r e a d y = t r u e ;
and the literals true = 1 and false = 0 can be converted to type
int by promotion.
i n t ans = true ;
bool s t a r t = −100;
bool s t o p = 0 ;
John Chrispell,
Wednesday September 1, 2010
// a n s a s s i g n e d v a l u e 1
// s t a r t a s s i g n e d t r u e
// s t o p a s s i g n e d f a l s e
slide 9/35
The const Qualifier
Previouly we have defined symbolic constants using the
preprocessor.
#d e f i n e MONTHS 12 // T h i s i s t h e C way o f d o i n g t h i n g s
C++ has a different way to handel symbolic constants.
const i n t MONTHS = 1 2 ; // a c h i e v e s t h e same c o n s t a n t .
The key word const is termed a qualifier because it qualifies the
meaning of the declaration.
It is common practice to use all capitals when defining constants
const t y p e NAME = v a l u e ;
Note you must specify a value when you declare the constant.
Using const works with elaborate types.
John Chrispell,
Wednesday September 1, 2010
slide 11/35
Floating Point Numbers
C++ has two ways of writing floating point numbers (Numbers
with fractional parts).
Note: 34.123 and 3412.3 are the same except for the scale.
The scaling factor serves to move the decimal point. Thus,
floating point.
Standard decimal point notation.
8.0 the decimal point ensures that the number is
E notation. (Scientific notation useful for very big and very
small numbers.)
John Chrispell,
Wednesday September 1, 2010
slide 13/35
E Notation
A quick review of scientific notation.
2 . 5 2 e+9 /∗ V a l i d ∗/
2 . 5 2 E9 /∗ V a l i d ∗/
John Chrispell,
Wednesday September 1, 2010
2 . 5 2 e+9 /∗ Not V a l i d ∗/
slide 15/35
Floating-Point Types
C and C++ both support three floating-point types:
f l o a t , double , long double
Each type allows for different number of significant figures, and
minimum allowable range of exponents.
It depends on the compiler how many bits are used for each type.
(There may be no difference.) Standard requirments are that float
is at least 32 bits, double is at least 48 bits, and long double is at
least as big as long double.
They must all support a range of exponents that is at least -37 to
37. Exact sizes are given in the cfloat header file.
John Chrispell,
Wednesday September 1, 2010
slide 17/35
floatnum.cpp
We can use the setf() method from the ostream class to force
output to stay in fixed-point notation. Normally trailing zeros are
dropped, and programs may switch to scientific notation.
#i n c l u d e <i o s t r e a m >
i n t main ( ) {
u s i n g namespace s t d ;
cout . s e t f ( i o s b a s e : : fixed , i o s b a s e : : f l o a t f i e l d ) ;
/∗ The c a l l above f o r c e s f i x e d −p o i n t n o t a t i o n ∗/
f l o a t tub = 10.0 / 3 . 0 ;
// good t o a b o u t 6 p l a c e s
double mint = 1 0 . 0 / 3 . 0 ;
// good t o a b o u t 15 p l a c e s
const f l o a t m i l l i o n = 1 . 0 e6 ;
cout
cout
cout
cout
<<
<<
<<
<<
” t u b = ” << t u b ;
” , a m i l l i o n t u b s = ” << m i l l i o n ∗ t u b ;
” , \ nand t e n m i l l i o n t u b s = ” ;
10 ∗ m i l l i o n ∗ t u b << e n d l ;
c o u t << ” mint = ” << mint << ” and a m i l l i o n m i n t s = ” ;
c o u t << m i l l i o n ∗ mint << e n d l ;
return 0;
}
John Chrispell,
Wednesday September 1, 2010
slide 19/35
What is the defalult?
By default when you use a floating-point constant in a program it
is a double.
You can change to float or long double in the following manner:
1.234 f
1 . 2 3 e20F
3 . 2 2 1 E28
2.2L
John Chrispell,
//
//
//
//
a
a
a
a
f l o a t constant
f l o a t constant
double constant
long double
Wednesday September 1, 2010
slide 21/35
More on Floating-Point Numbers and fltadd.cpp
Floating point numbers can represent values in between
integers.
Floating point numbers can represent a greater range of
number values.
Floating point operations are slower than integer operations.
You can lose precision.
// f l t a d d . cpp −− p r e c i s i o n p r o b l e m s w i t h f l o a t
#i n c l u d e <i o s t r e a m >
i n t main ( ) {
u s i n g namespace s t d ;
f l o a t a = 2 . 3 4 E+22 f ;
float b = a + 1.0 f ;
c o u t << ” a = ” << a << e n d l ;
c o u t << ”b − a = ” << b − a << e n d l ;
return 0;
}
We expect mathematically to get a value of 1. Problem float has
only represents the first 6 or 7 digits in a number.
John Chrispell,
Wednesday September 1, 2010
slide 23/35
More on Floating-Point Numbers and fltadd.cpp
Floating point numbers can represent values in between
integers.
Floating point numbers can represent a greater range of
number values.
Floating point operations are slower than integer operations.
You can lose precision.
// f l t a d d . cpp −− p r e c i s i o n p r o b l e m s w i t h f l o a t
#i n c l u d e <i o s t r e a m >
i n t main ( ) {
u s i n g namespace s t d ;
f l o a t a = 2 . 3 4 E+22 f ;
float b = a + 1.0 f ;
c o u t << ” a = ” << a << e n d l ;
c o u t << ”b − a = ” << b − a << e n d l ;
return 0;
}
We expect mathematically to get a value of 1. Problem float has
only represents the first 6 or 7 digits in a number.
John Chrispell,
Wednesday September 1, 2010
slide 25/35
Arithmetic Operators
C++ has five basic arithmetic operators:
1
2
3
4
5
The (+) operator adds its operands.
The (-) operator subtracts the second operand form the first.
The (*) operator multiplies its operands.
The (/) operator divides its first operand by the second. If
both operands are integer the relult is the integer portion of
the quotent with the remainder discarded.
The (%) operator finds the modulus of its first operand with
respect to the second. (i.e. 19 % 6 is 1).
Note that C++ uses operator precedence and Associativity. Thus,
multiplicat, division and taking of modulus are done before
addition and subtraction.
i n t v a l u e = 20∗5+24∗6; // what g e t s done f i r s t ?
John Chrispell,
Wednesday September 1, 2010
slide 27/35
divide.cpp
#i n c l u d e <i o s t r e a m >
i n t main ( ) {
u s i n g namespace s t d ;
cout . s e t f ( i o s b a s e : : fixed , i o s b a s e : : f l o a t f i e l d ) ;
c o u t << ” I n t e g e r d i v i s i o n : 9/5 = ”
<< 9 / 5 << e n d l ;
c o u t << ” F l o a t i n g −p o i n t d i v i s i o n : 9 . 0 / 5 . 0 = ” ;
c o u t << 9 . 0 / 5 . 0 << e n d l ;
c o u t << ” Mixed d i v i s i o n : 9 . 0 / 5 = ”
<< 9 . 0 / 5 << e n d l ;
c o u t << ” d o u b l e c o n s t a n t s : 1 e7 / 9 . 0 = ” ;
c o u t << 1 . e7 / 9 . 0 << e n d l ;
c o u t << ” f l o a t c o n s t a n t s : 1 e 7 f / 9 . 0 f = ” ;
c o u t << 1 . e 7 f / 9 . 0 f << e n d l ;
return 0;
}
Note the division operator is overloaded here. In important C++
concept.
John Chrispell,
Wednesday September 1, 2010
slide 29/35
Type Conversions
C++ will do many type conversions for you automatically. This is
usually nice; however, it can be baffeling if it happens unexpectedly
C++ converts value swhen you assign a value of one
arithmetic type to a variable of another aritmatic type.
double v a l u e = 5 ;
C++ converts values when you combine mixed types in
expressions.
v a l u e = ( 5 . 0 e12 ) ∗ 2 . 0 ∗ 4 ;
C++ converts values when you pass arguments to functions.
value = sqrt (16);
Note conversions can happen on assignment (not just in
declaration).
John Chrispell,
Wednesday September 1, 2010
slide 31/35
assign.cpp
#i n c l u d e <i o s t r e a m >
i n t main ( ) {
u s i n g namespace s t d ;
cout . s e t f ( i o s b a s e : : fixed , i o s b a s e : : f l o a t f i e l d ) ;
float tree = 3;
// i n t c o n v e r t e d t o f l o a t
i n t g u e s s = 3 . 9 8 3 2 ; // f l o a t c o n v e r t e d t o i n t
i n t d e b t = 7 . 2 E12 ; // r e s u l t n o t d e f i n e d i n C++
c o u t << ” t r e e = ” << t r e e << e n d l ;
c o u t << ” g u e s s = ” << g u e s s << e n d l ;
c o u t << ” d e b t = ” << d e b t << e n d l ;
c o u t << i n t ( ’Q ’ ) ;
// d i s p l a y s t h e i n t e g e r code f o r ’Q ’
return 0;
}
Note that g++ Warns us of potential pitfalls.
John Chrispell,
Wednesday September 1, 2010
slide 33/35
Type Casting
We can explicitly enforce types using casting mechanisms as well.
int value = 7;
( long ) v a l u e // w i l l r e t u r n a l o n g c o n v e r s i o n o f v a l u e
long ( v a l u e ) // w i l l r e t u r n a l o n g c o n v e r s i o n o f v a l u e
( typeName ) v a l u e
// c o n v e r t s v a l u e t o typeName t y p e
typeName ( v a l u e )
// c o n v e r t s v a l u e t o typeName t y p e
C
C++
s t a t i c c a s t <typeName> ( v a l u e )
// c o n v e r t s v a l u e t o typeName t y p e
/∗ T h i s l a s t one i s t h e most r e s t r i c t i v e
∗ o f when i t can be u s e d
John Chrispell,
Wednesday September 1, 2010
∗/
slide 35/35