Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS 342: Object-Oriented Software Development Lab
A Tour Through C++
David L. Levine
Christopher D. Gill
Department of Computer Science
Washington University, St. Louis
levine,cdgill@cs.wustl.edu
http://classes.cec.wustl.edu/cs342/
CS 342: OO Software Development Lab OO Programming with C++
C++ Tour Topics
 C++ Overview and Design
Goals
 Function Prototypes
 C++ Classes
 C++ Objects
 Inheritance Preview
 Dynamic Binding Preview
 Overloading
 Type-Safe Linkage
 Inline Functions
 Dynamic Memory Management
 Const Type Qualifier
 Stream I/O
 Boolean Type
 References
 Default Parameters
 Declaration Statements
 Static Initialization
Copyright c
1997-2000 Dept. of Computer Science, Washington University 1
CS 342: OO Software Development Lab OO Programming with C++
C++ Overview
 C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the
early 80’s
– The original cfront translated C++ into C for portability
 However, this was difficult to debug and potentially inefficient
– Many native host machine compilers now exist
 e.g., Borland, DEC, GNU, HP, IBM, Microsoft, Sun, Symantec,
etc.
 C++ is a mostly upwardly compatible extension of C that provides:
1. Stronger typechecking
2. Support for data abstraction
3. Support for object-oriented programming
4. Support for generic programming
Copyright c
1997-2000 Dept. of Computer Science, Washington University 2
CS 342: OO Software Development Lab OO Programming with C++
C++ Design Goals
 As with C, run-time efficiency is important
– Unlike, e.g., Ada and Java, complicated run-time libraries have not
traditionally been required for C++
 Note, that there is no language-specific support for concurrency,
persistence, garbage collection, or distribution in C++
 Compatibility with C libraries and UNIX tools is emphasized, e.g.,
– Object code reuse
 The storage layout of structures is compatible with C
 Support for X-windows, standard ANSI C library, UNIX system
calls via the extern "C" feature
– Object code analysis tools, such as nm and size
– C++ works with the make recompilation utility
Copyright c
1997-2000 Dept. of Computer Science, Washington University 3
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
C + + D e s i g n G o a l s ( c o n t ’ d )
 “ A s c l o s e t o C a s p o s s i b l e , b u t n o c l o s e r ”
– i.e., C + + i s n o t a p r o p e r s u p e r s e t o f C , s o t h a t
b a c k w a r d s c o m p a t i b i l i t y i s n o t e n t i r e l y
m a i n t a i n e d
 T y p i c a l l y n o t a p r o b l e m i n p r a c t i c e . . .
 N o t e , c e r t a i n C + + d e s i g n g o a l s c o n fl i c t w i t h
m o d e r n t e c h n i q u e s f o r :
1 . Compiler optimization
– e.g., p o i n t e r s t o a r b i t r a r y m e m o r y l o c a t i o n s
c o m p l i c a t e r e g i s t e r a l l o c a t i o n a n d g a r b a g e
c o l l e c t i o n
2 . Software engineering
– e.g., t h e c l a s s i n t e r f a c e e x p o s e s i t s p r i v a t e
d a t a l a y o u t t o a l l c l i e n t s u n l e s s c e r t a i n
p a t t e r n s / i d i o m s a r e u s e d
– e.g., s e p a r a t e c o m p i l a t i o n c o m p l i c a t e s
inlining d u e t o d i f fi c u l t y o f i n t e r p r o c e d u r a l
a n a l y s i s
– D y n a m i c m e m o r y m a n a g e m e n t i s
e r r o r - p r o n e
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
M
ajorC++
F
eatures
a
nd
Enhancem
ents
1.
C++
supports
O
O
prog
ra
m
m
ing
fe
atures

e.g
.
,
abstra
ctclasses
,inheritance
,a
nd
dynam
ic
binding
2.
C++
supports
data
abstra
ction
fe
atures

e.g
.
,
classes
a
nd
n
a
m
e
spaces
3.
C++
supports
generic
prog
ra
m
m
ing

e.g
.
,para
m
eterized
types
4.
C++
supports
sophisticated
e
rro
rhandling

e.g
.
,
e
xception
handling
5.
C++
supports
identifying
a
n
object’s
type
at
ru
ntim
e

e.g
.
,R
u
n
-Tim
e
Type
Identification(RTTI)
Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
5
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
O t h e r U s e f u l F e a t u r e s a n d
E n h a n c e m e n t s
 C + + e n f o r c e s t y p e c h e c k i n g v i a function
prototypes
 P r o v i d e s t y p e - s a f e l i n k a g e
 P r o v i d e s i n l i n e f u n c t i o n e x p a n s i o n
 B u i l t - i n d y n a m i c m e m o r y m a n a g e m e n t v i a n e w
a n d d e l e t e o p e r a t o r s
 D e f a u l t v a l u e s f o r f u n c t i o n p a r a m e t e r s
 R e f e r e n c e s p r o v i d e call-by-reference p a r a m e t e r
p a s s i n g
 D e c l a r e c o n s t a n t s t h a t c a n b e u s e d t o d e fi n e
s t a t i c a r r a y b o u n d s w i t h t h e c o n s t t y p e q u a l i fi e r
 P r o v i d e s a n a m e s p a c e c o n t r o l m e c h a n i s m f o r
r e s t r i c t i n g t h e s c o p e o f c l a s s e s , f u n c t i o n s , a n d
g l o b a l o b j e c t s
 T h e n a m e o f a s t r u c t , c l a s s , e n u m , o r
u n i o n i s a t y p e n a m e
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
S o m e w h a t U s e f u l F e a t u r e s a n d
E n h a n c e m e n t s
 O p e r a t o r a n d f u n c t i o n o v e r l o a d i n g
 N e w b o o l b o o l e a n t y p e
 A l l o w s s e v e r a l d i f f e r e n t c o m m e n t i n g s t y l e s
 A n e w s e t o f “ f u n c t i o n c a l l ” - s t y l e c a s t n o t a t i o n s
 V a r i a b l e d e c l a r a t i o n s c a n o c c u r a n y w h e r e
s t a t e m e n t s c a n a p p e a r w i t h i n a b l o c k
 A l l o w s u s e r - d e fi n e d c o n v e r s i o n o p e r a t o r s
 S t a t i c d a t a i n i t i a l i z e r s m a y b e a r b i t r a r y
e x p r e s s i o n s
 N e w t y p e - s e c u r e e x t e n s i b l e I / O i n t e r f a c e c a l l e d
streams a n d iostreams
 N e w m u t a b l e t y p e q u a l i fi e r
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
CS 342: OO Software Development Lab OO Programming with C++
Language Features Not Part of C++
1. Concurrency
 See Concurrent C by Nehrain Gehani and the Actor++ model by
Lavender and Kafura
2. Persistence
 See Object Store, Versant, Objectivity, as well as the Exodus
system and E programming language
3. Garbage Collection
 See papers in USENIX C++ 1994
4. Distribution
 See CORBA and DCOM
Copyright c
1997-2000 Dept. of Computer Science, Washington University 8
CS 342: OO Software Development Lab OO Programming with C++
Strategies for Learning C++
 Focus on concepts and programming techniques
– Don’t get lost in language features
 Learn C++ to become a better programmer
– More effective at designing and implementing
– Design Patterns
 Recognize that C++ supports many different programming styles
 Learn C++ gradually
– You don’t have to know every detail of C++ to write a good C++
program
Copyright c
1997-2000 Dept. of Computer Science, Washington University 9
CS 342: OO Software Development Lab OO Programming with C++
Overview of Remaining Tutorial
 To illustrate C++, we use a detailed case study that examines
several alteratives methods of implementing a Stack
– We begin with C and evolve up to various C++ implementations
 Next, we examine many of the key C++ features in more detail
– We’ll cover these features in even more detail over the course of
the class
Copyright c
1997-2000 Dept. of Computer Science, Washington University 10
CS 342: OO Software Development Lab OO Programming with C++
Stack Example
 Consider the following “bare-bones” Stack implementation:
typedef int T;
/* const int MAX_STACK = 100; */
#define MAX_STACK 100
T stack[MAX_STACK];
int top = 0;
T item;
stack[top++] = item; // push
...
item = stack[--top]; // pop
 Obviously, this is not very abstract...
Copyright c
1997-2000 Dept. of Computer Science, Washington University 11
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
D a t a H i d i n g I m p l e m e n t a t i o n i n C
 D e fi n e t h e i n t e r f a c e t o a S t a c k o f i n t e g e r s i n C :
/ * F i l e s t a c k . h * /
/ * T y p e o f S t a c k e l e m e n t . * /
t y p e d e f i n t T ;
/ * S t a c k i n t e r f a c e . * /
i n t c r e a t e ( i n t s i z e ) ;
i n t d e s t r o y ( V O I D ) ;
v o i d p u s h ( T n e w _ i t e m ) ;
v o i d p o p ( T * o l d _ t o p ) ;
v o i d t o p ( T * c u r _ t o p ) ;
i n t i s _ e m p t y ( v o i d ) ;
i n t i s _ f u l l ( v o i d ) ;
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
D a t a H i d i n g I m p l e m e n t a t i o n i n C
( c o n t ’ d )
 / * F i l e s t a c k . c * /
# i n c l u d e < s t d l i b . h >
# i n c l u d e " s t a c k . h "
/ * H i d d e n w i t h i n t h i s f i l e . * /
s t a t i c i n t t o p _ , s i z e _ ;
s t a t i c T * s t a c k _ ;
i n t c r e a t e ( i n t s i z e ) {
t o p _ = 0 ; s i z e _ = s i z e ;
s t a c k _ = m a l l o c ( s i z e * s i z e o f ( T ) ) ;
r e t u r n s t a c k _ = = 0 ? - 1 : 0 ;
}
v o i d d e s t r o y ( v o i d ) { f r e e ( ( v o i d * ) s t a c k _ ) ; }
v o i d p u s h ( T i t e m ) { s t a c k _ [ t o p _ + + ] = i t e m ; }
v o i d p o p ( T * i t e m ) { * i t e m = s t a c k _ [ - - t o p _ ] ; }
v o i d t o p ( T * i t e m ) { * i t e m = s t a c k _ [ t o p _ - 1 ] ; }
i n t i s _ e m p t y ( v o i d ) { r e t u r n t o p _ = = 0 ; }
i n t i s _ f u l l ( v o i d ) { r e t u r n t o p _ = = s i z e _ ; }
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
D
ata
Hiding
Im
plem
entation
in
C
(co
nt’d)

Use
ca
se
#include
"
s
t
a
ck.h"
v
oid
foo
(void)
{
T
i
;
p
u
sh
(10);
/*
Oops,
forgot
t
o
c
all
c
r
e
a
t
e
!
*/
p
u
sh
(20);
p
o
p
(&i);
destroy
();
}

M
ain
problem
s:
1.
The
p
rog
ra
m
m
e
r
m
u
st
call
c
r
e
a
t
e
first
a
nd
destroy
last!
–
Could
u
se
first-tim
e-in
flag...
2.
There
is
o
nly
one
stack
a
nd
o
nly
one
type
of
stack
3.
N
am
e
space
pollution...
4.
N
on-reentra
nt
Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
14
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
D a t a A b s t r a c t i o n I m p l e m e n t a t i o n i n C
 A n A D T S t a c k i n t e r f a c e i n C :
/ * F i l e s t a c k . h * /
/ * T y p e o f S t a c k e l e m e n t . * /
t y p e d e f i n t T ;
/ * T y p e d e f i n i t i o n f o r S t a c k A D T . * /
t y p e d e f s t r u c t {
s i z e _ t t o p _ , s i z e _ ;
T * s t a c k _ ;
} S t a c k ;
/ * S t a c k i n t e r f a c e . * /
i n t S t a c k _ c r e a t e ( S t a c k * s , s i z e _ t s i z e ) ;
v o i d S t a c k _ d e s t r o y ( S t a c k * s ) ;
v o i d S t a c k _ p u s h ( S t a c k * s , T i t e m ) ;
v o i d S t a c k _ p o p ( S t a c k * , T * i t e m ) ;
/ * M u s t c a l l b e f o r e p o p ’ i n g * /
i n t S t a c k _ i s _ e m p t y ( S t a c k * ) ;
/ * M u s t c a l l b e f o r e p u s h ’ i n g * /
i n t S t a c k _ i s _ f u l l ( S t a c k * ) ;
/ * . . . * /
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
D a t a A b s t r a c t i o n I m p l e m e n t a t i o n i n C
( c o n t ’ d )
 A n A D T S t a c k i m p l e m e n t a t i o n i n C :
/ * F i l e s t a c k . c * /
# i n c l u d e " s t a c k . h "
i n t S t a c k _ c r e a t e ( S t a c k * s , s i z e _ t s i z e ) {
s - > t o p _ = 0 ; s - > s i z e _ = s i z e ;
s - > s t a c k _ = m a l l o c ( s i z e * s i z e o f ( T ) ) ;
r e t u r n s - > s t a c k _ = = 0 ? - 1 : 0 ;
}
v o i d S t a c k _ d e s t r o y ( S t a c k * s ) {
f r e e ( ( V O I D * ) s - > s t a c k _ ) ;
s - > t o p _ = 0 ; s - > s i z e _ = 0 ; s - > s t a c k _ = 0 ;
}
v o i d S t a c k _ p u s h ( S t a c k * s , T i t e m ) {
s - > s t a c k _ [ s - > t o p _ + + ] = i t e m ;
}
v o i d S t a c k _ p o p ( S t a c k * s , T * i t e m ) {
* i t e m = s - > s t a c k _ [ - - s - > t o p _ _ ] ;
}
i n t S t a c k _ i s _ e m p t y ( S t a c k * s ) {
R E T U R N s - > t o p _ = = 0 ;
}
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
D a t a A b s t r a c t i o n I m p l e m e n t a t i o n i n C
( c o n t ’ d )
 U s e c a s e
v o i d f o o ( v o i d )
{
/ * M u l t i p l e s t a c k s ! * /
S t a c k s 1 , s 2 , s 3 ;
T i t e m ;
/ * P o p ’ d e m p t y s t a c k * /
S t a c k _ p o p ( & s 2 , & i t e m ) ;
/ * F o r g o t t o c a l l S t a c k _ c r e a t e ! * /
S t a c k _ p u s h ( & s 3 , 1 0 ) ;
/ * D e s t r o y u n i n i t i a l i z e d s t a c k ! * /
S t a c k _ d e s t r o y ( & s 1 ) ;
}
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
M
ain
problem
s
w
ith
D
ata
Abstraction
in
C
1.
N
o
guara
nteed
initialization/term
ination
2.
Stillo
nly
o
n
e
type
ofstack
supported
3.
To
o
m
u
ch
o
ve
rhead
due
to
function
calls
4.
N
o
generalized
e
rro
rhandling...
5.
The
C
co
m
pilerdoes
n
ote
nfo
rce
info
rm
ation
hiding
e.g
.
,
s1.top_
=
s2.stack_[0];
/*
Violate
abstraction
*/
s2.size_
=
s3.top_;
/*
Violate
abstraction
*/
Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
18
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
D a t a A b s t r a c t i o n I m p l e m e n t a t i o n i n
C + +
 Question: h o w t o w e g e t e n c a p s u l a t i o n and
m o r e t h a n o n e s t a c k ?
 Answer: d e fi n e a S t a c k A D T u s i n g C + + :
/ / F i l e S t a c k . h
t y p e d e f i n t T ;
c l a s s S t a c k {
p u b l i c :
S t a c k ( s i z e _ t s i z e ) ;
S t a c k ( c o n s t S t a c k & s ) ;
v o i d S t a c k ( c o n s t S t a c k & s ) ;
˜ S t a c k ( v o i d ) ;
v o i d p u s h ( c o n s t T & i t e m ) ;
v o i d p o p ( T & i t e m ) ;
i n t i s _ e m p t y ( v o i d ) c o n s t ;
i n t i s _ f u l l ( v o i d ) c o n s t ;
/ / . . .
p r i v a t e :
s i z e _ t t o p _ , s i z e _ ;
T * s t a c k _ ;
} ;
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
D a t a A b s t r a c t i o n I m p l e m e n t a t i o n i n
C + + ( c o n t ’ d )
 M a n a g e r o p e r a t i o n s
S t a c k : : S t a c k ( s i z e _ t s i z e )
: t o p _ ( 0 ) , s i z e _ ( s i z e ) ,
s t a c k _ ( n e w T [ s i z e ] ) { }
S t a c k : : S t a c k ( c o n s t S t a c k & s ) :
: t o p _ ( s . t o p _ ) , s i z e _ ( s . s i z e _ ) ,
s t a c k _ ( n e w T [ s . s i z e _ ] ) { }
v o i d S t a c k : : o p e r a t o r = ( c o n s t S t a c k & s ) :
i f ( t h i s = = & s ) r e t u r n ;
d e l e t e [ ] t h i s - > s t a c k _ ;
t h i s - > s t a c k _ = n e w T [ s . s i z e _ ] ;
t h i s - > t o p _ = s . t o p _ ; t h i s - > s i z e _ = s . s i z e _ ;
f o r ( s i z e _ t i = 0 ; i < s . s i z e _ ; i + + )
t h i s - > s t a c k _ [ i ] = s . s t a c k _ [ i ] ;
}
S t a c k : : ˜ S t a c k ( v o i d ) {
d e l e t e [ ] t h i s - > s t a c k _ ;
}
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
D a t a A b s t r a c t i o n I m p l e m e n t a t i o n i n
C + + ( c o n t ’ d )
 A c c e s s o r a n d w o r k e r o p e r a t i o n s
i n t S t a c k : : i s _ e m p t y ( v o i d ) c o n s t {
r e t u r n t h i s - > t o p _ = = 0 ;
}
i n t S t a c k : : i s _ f u l l ( V O I D ) c o n s t {
r e t u r n t h i s - > t o p _ = = t h i s - > s i z e _ ;
}
v o i d S t a c k : : p u s h ( c o n s t T & i t e m ) {
t h i s - > s t a c k _ [ t h i s - > t o p _ + + ] = i t e m ;
}
v o i d S t a c k : : p o p ( T & i t e m ) {
i t e m = t h i s - > s t a c k _ [ - - t h i s - > t o p _ ] ;
}
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
D a t a A b s t r a c t i o n I m p l e m e n t a t i o n i n
C + + ( c o n t ’ d )
 U s e c a s e
# i n c l u d e " S t a c k . h "
v o i d f o o ( v o i d ) {
S t a c k s 1 ( 1 ) , s 2 ( 1 0 0 ) ;
T i t e m ;
i f ( ! s 1 . i s _ f u l l ( ) )
s 1 . p u s h ( 4 7 3 ) ;
i f ( ! s 2 . i s _ f u l l ( ) )
s 2 . p u s h ( 2 1 1 2 ) ;
i f ( ! s 2 . i s _ e m p t y ( ) )
s 2 . p o p ( i t e m ) ;
/ / A c c e s s v i o l a t i o n c a u g h t
/ / a t c o m p i l e - t i m e !
s 2 . t o p _ = 1 0 ;
/ / T e r m i n a t i o n h a n d l e d a u t o m a t i c a l l y
/ / v i a d e s t r u c t o r .
}
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
B
enefits
ofC
+
+
D
ata
Abstraction
Im
plem
enation
1.
D
ata
hiding
a
nd
data
abstra
ction,e.g
.
,
Stack
s1
(200);
s1.top_
=
10
//
E
r
r
o
r
flagged
by
c
o
m
piler!
2.
The
ability
to
declare
m
ultiple
stack
objects
Stack
s1
(10),
s2
(20),
s3
(30);
3.
A
utom
atic
initialization
a
nd
term
ination
{
//
Constructor
a
u
t
o
m
a
tically
c
alled.
Stack
s1
(1000);
//
.
.
.
//
D
e
s
t
r
u
c
t
o
r
a
u
t
o
m
a
tically
c
alled
}
Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
23
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
D
ra
w
backs
w
ith
C++
D
ata
Abstraction
Im
plem
entation:
1.
Errorhandling
is
obtru
sive

Use
e
xception
handling
to
solve
this
2.
The
e
xa
m
ple
is
lim
ited
to
a
single
type
ofstack
elem
ent(INT
in
this
ca
se)

W
e
ca
n
u
se
C++
“para
m
eterized
types”to
re
m
o
ve
this
lim
itation
3.
Function
callo
ve
rhead

W
e
ca
n
u
se
C++
inline
fu
n
ctions
to
re
m
o
ve
this
o
ve
rhead
Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
24
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
E x c e p t i o n H a n d l i n g I m p l e m e n t a t i o n i n
C + + ( c o n t ’ d )
 C + + e x c e p t i o n h a n d l i n g s e p a r a t e s e r r o r
h a n d l i n g f r o m n o r m a l p r o c e s s i n g
/ / F i l e S t a c k . h
t y p e d e f i n t T ;
c l a s s S t a c k {
p u b l i c :
c l a s s U n d e r f l o w { / * . . . * / } ;
c l a s s O v e r f l o w { / * . . . * / } ;
S t a c k ( s i z e _ t s i z e ) ;
˜ S t a c k ( v o i d ) ;
v o i d p u s h ( c o n s t T & i t e m )
t h r o w ( O v e r f l o w ) ;
v o i d p o p ( T & i t e m )
t h r o w ( U n d e r f l o w ) ;
/ / . . .
p r i v a t e :
s i z e _ t t o p _ , s i z e _ ;
T * s t a c k _ ;
} ;
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
Exception
H
andling
Im
plem
entation
in
C++(co
nt’d)

Stack.C
v
oid
Stack::push
(const
T
&item)
throw
(Stack::Overflow)
{
if
(this->is_full
())
throw
Stack::Overflow
();
this->stack_[this->top_++]
=
item;
}v
oid
Stack::pop
(T
&item)
throw
(Stack::Underflow)
{
if
(this->is_empty
())
throw
Stack::Underflow
();
item
=
this->stack_[--this->top_];
}
Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
26
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
E x c e p t i o n H a n d l i n g I m p l e m e n t a t i o n i n
C + + ( c o n t ’ d )
 U s e c a s e
# i n c l u d e " S t a c k . h "
v o i d f o o ( v o i d ) {
S t a c k s 1 ( 1 ) , s 2 ( 1 0 0 ) ;
t r y {
T i t e m ;
s 1 . p u s h ( 4 7 3 ) ;
/ / E x c e p t i o n , p u s h ’ d f u l l s t a c k !
s 1 . p u s h ( 4 2 ) ;
/ / E x c e p t i o n , p o p ’ d e m p t y s t a c k !
s 2 . p o p ( i t e m ) ;
s 2 . t o p _ = 1 0 ; / / A c c e s s v i o l a t i o n c a u g h t !
} c a t c h ( S t a c k : : U n d e r f l o w ) {
/ / H a n d l e u n d e r f l o w . . .
} c a t c h ( S t a c k : : O v e r f l o w ) {
/ / H a n d l e o v e r f l o w . . .
} c a t c h ( . . . ) {
/ / C a t c h a n y t h i n g e l s e . . .
t h r o w ;
}
/ / T e r m i n a t i o n i s h a n d l e d a u t o m a t i c a l l y .
}
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
T e m p l a t e I m p l e m e n t a t i o n i n C + +
 A p a r a m e t e r i z e d t y p e S t a c k c l a s s i n t e r f a c e
u s i n g C + +
/ / t y p e d e f i n t T ;
t e m p l a t e < c l a s s T >
c l a s s S t a c k {
p u b l i c :
S t a c k ( s i z e _ t s i z e ) ;
˜ S t a c k ( v o i d )
v o i d p u s h ( c o n s t T & i t e m ) ;
v o i d p o p ( T & i t e m ) ;
i n t i s _ e m p t y ( v o i d ) ;
i n t i s _ f u l l ( v o i d ) ;
/ / . . .
p r i v a t e :
s i z e _ t t o p _ , s i z e _ ;
T * s t a c k _ ;
} ;
 T o s i m p l i f y t h e f o l l o w i n g e x a m p l e s w e ’ l l o m i t
e x c e p t i o n h a n d l i n g . . .
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
T e m p l a t e I m p l e m e n t a t i o n i n C + +
( c o n t ’ d )
 A p a r a m e t e r i z e d t y p e S t a c k c l a s s
i m p l e m e n t a t i o n u s i n g C + +
t e m p l a t e < c l a s s T > i n l i n e
S t a c k < T > : : S t a c k ( s i z e _ t s i z e )
: t o p _ ( 0 ) , s i z e _ ( s i z e ) ,
s t a c k _ ( n e w T [ s i z e ] ) { }
t e m p l a t e < c l a s s T > i n l i n e
S t a c k < T > : : ˜ S t a c k ( v o i d ) {
d e l e t e [ ] t h i s - > s t a c k _ ;
}
t e m p l a t e < c l a s s T > i n l i n e v o i d
S t a c k < T > : : p u s h ( c o n s t T & i t e m ) {
t h i s - > s t a c k _ [ t h i s - > t o p _ + + ] = i t e m ;
}
t e m p l a t e < c l a s s T > i n l i n e v o i d
S t a c k < T > : : p o p ( T & i t e m ) {
i t e m = t h i s - > s t a c k _ [ - - t h i s - > t o p _ ] ;
}
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
Te
m
plate
Im
plem
entation
in
C++(co
nt’d)

N
ote
the
m
inor
changes
to
the
code
to
a
cco
m
m
odate
para
m
eterized
types
#include
"Stack.h"
v
oid
foo
(void)
{
Stack
s1
(1000);
Stack
s2;
Stack<
Stack
<
A
c
tivation_Record>
*
>
s3;
s1.push
(-291);
s2.top_
=
3.1416;
//
A
c
c
e
s
s
violation
c
a
ught!
s3.push
(new
Stack);
Stack
<
A
c
tivation_Record>
*
s
a
r
;
s3.pop
(sar);
delete
s
a
r
;
//
T
e
r
mination
is
handled
a
u
t
o
m
a
tically
}
Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
30
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
T e m p l a t e I m p l e m e n t a t i o n i n C + +
( c o n t ’ d )
 A n o t h e r p a r a m e t e r i z e d t y p e S t a c k c l a s s
t e m p l a t e < c l a s s T , s i z e _ t s i z e >
c l a s s S t a c k {
p u b l i c :
S t a c k ( v o i d ) ;
˜ S t a c k ( v o i d )
v o i d p u s h ( C O N S T T & i t e m ) ;
v o i d p o p ( T & i t e m ) ;
/ / . . .
p r i v a t e :
s i z e _ t t o p _ , s i z e _ ;
T s t a c k _ [ s i z e ] ;
} ;
 N o t e , t h e r e i s n o l o n g e r a n y n e e d f o r d y n a m i c
m e m o r y , e.g.,
S t a c k < i n t , 2 0 0 > s 1 ;
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
O
bject-Oriented
Im
plem
entation
in
C++

Problem
s
w
ith
pre
vious
e
xa
m
ples:
–
Changes
to
the
im
plem
entation
willrequire
re
co
m
pilation
a
nd
relinking
of
clients
–
Extensions
w
illrequire
a
cce
ss
to
so
u
rce

Solutions
–
Com
bine
inheritance
with
dynam
ic
binding
to
com
pletely
decouple
interfa
ce
from
im
plem
entation
a
nd
binding
tim
e
–
This
requires
the
u
se
ofC
+
+
abstractbase
classes
Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
32
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
O
bject-Oriented
Im
plem
entation
in
C++(co
nt’d)

D
efining
a
n
abstra
ctbase
class
in
C++
t
e
m
plate
<
class
T
>
class
Stack
{p
ublic:
virtual
v
oid
p
u
sh
(const
T
&item)
=
0
;
virtual
v
oid
p
o
p
(T
&item)
=
0
;
virtual
int
is_empty
(void)
c
o
n
s
t
=
0
;
virtual
int
is_full
(void)
c
o
n
s
t
=
0
;
//
T
e
m
plate
m
e
thod
v
oid
t
o
p
(T
&item)
{
this->pop
(item);
this->push
(item);
}
};

By
u
sing
“pure
virtualm
ethods
,”
w
e
ca
n
guara
ntee
thatthe
co
m
piler
w
o
n
’t
allo
w
in
stantiation!
Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
33
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
O b j e c t - O r i e n t e d I m p l e m e n t a t i o n i n C + +
( c o n t ’ d )
 U s e i n t e r f a c e i n h e r i t a n c e t o c r e a t e a s p e c i a l i z e d
v e r s i o n o f a “ b o u n d e d ” s t a c k :
# i n c l u d e " S t a c k . h "
# i n c l u d e " A r r a y . h "
t e m p l a t e < c l a s s T >
c l a s s B _ S t a c k : p u b l i c S t a c k < T >
{
p u b l i c :
e n u m { D E F A U L T _ S I Z E = 1 0 0 } ;
B _ S t a c k ( s i z e _ t s i z e = D E F A U L T _ S I Z E ) ;
v i r t u a l v o i d p u s h ( c o n s t T & i t e m ) ;
v i r t u a l v o i d p o p ( T & i t e m ) ;
v i r t u a l i n t i s _ e m p t y ( v o i d ) c o n s t ;
v i r t u a l i n t i s _ f u l l ( v o i d ) c o n s t ;
/ / . . .
p r i v a t e :
A r r a y < T > s t a c k _ ; / / u s e r - d e f i n e d
s i z e _ t t o p _ ; / / b u i l t - i n
} ;
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
O b j e c t - O r i e n t e d I m p l e m e n t a t i o n i n C + +
( c o n t ’ d )
 c l a s s B S t a c k i m p l e m e n t a t i o n
t e m p l a t e < c l a s s T >
B _ S t a c k < T > : : B _ S t a c k ( s i z e _ t s i z e )
: t o p _ ( 0 ) , s t a c k _ ( s i z e ) {
}
t e m p l a t e < c l a s s T > v o i d
B _ S t a c k < T > : : p u s h ( c o n s t T & i t e m ) {
t h i s - > s t a c k _ . s e t ( t h i s - > t o p _ + + , i t e m ) ;
}
t e m p l a t e < c l a s s T > v o i d
B _ S t a c k < T > : : p o p ( T & i t e m ) {
t h i s - > s t a c k _ . g e t ( - - t h i s - > t o p _ , i t e m ) ;
}
t e m p l a t e < c l a s s T > i n t
B _ S t a c k < T > : : i s _ f u l l ( v o i d ) c o n s t {
r e t u r n t h i s - > t o p _ > = t h i s - > s t a c k _ . s i z e ( ) ;
}
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
O b j e c t - O r i e n t e d I m p l e m e n t a t i o n i n C + +
( c o n t ’ d )
 L i k e w i s e , i n t e r f a c e i n h e r i t a n c e c a n c r e a t e a
t o t a l l y d i f f e r e n t “ u n b o u n d e d ” i m p l e m e n t a t i o n :
/ / F o r w a r d d e c l a r a t i o n .
t e m p l a t e < c l a s s T > c l a s s N o d e ;
t e m p l a t e < c l a s s T >
c l a s s U B _ S t a c k : p u b l i c S t a c k < T >
{
p u b l i c :
e n u m { D E F A U L T _ S I Z E = 1 0 0 } ;
U B _ S t a c k ( s i z e _ t h i n t = D E F A U L T _ S I Z E ) ;
˜ U B _ S t a c k ( v o i d ) ;
v i r t u a l v o i d p u s h ( c o n s t T & n e w _ i t e m ) ;
v i r t u a l v o i d p o p ( T & t o p _ i t e m ) ;
v i r t u a l i n t i s _ e m p t y ( v o i d ) c o n s t {
r e t u r n t h i s - > h e a d _ = = 0 ;
}
v i r t u a l i n t i s _ f u l l ( v o i d ) c o n s t { r e t u r n 0 ; }
/ / . . .
p r i v a t e :
/ / H e a d o f l i n k e d l i s t o f N o d e < T > ’ s .
/ / N o t e t h e u s e o f t h e C h e s h i r e c a t . . .
N o d e < T > * h e a d _ ;
} ;
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
O
bject-Oriented
Im
plem
entation
in
C++(co
nt’d)

class
N
ode
im
plem
entation
t
e
m
plate
<
class
T
>
class
N
ode
{
friend
t
e
m
plate
<
class
T
>
class
U
B
_Stack;
p
ublic:
N
ode
(T
i,
N
ode
*
n
=
0)
:
item_
(i),
n
e
x
t
_
(n)
{}
p
rivate:
T
item_;
N
ode
*
n
e
x
t
_
;
};

N
ote
thatthe
u
se
ofthe
“Cheshire
cat”idiom
allo
w
s
the
libra
ry
w
riter
to
co
m
pletely
hide
the
representation
ofclass
U
B
Stack
.
.
.
Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
37
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
O b j e c t - O r i e n t e d I m p l e m e n t a t i o n i n C + +
( c o n t ’ d )
 C l a s s U B S t a c k i m p l e m e n t a t i o n :
t e m p l a t e < c l a s s T >
U B _ S t a c k < T > : : U B _ S t a c k ( s i z e _ t h i n t ) : h e a d _ ( 0 ) { }
t e m p l a t e < c l a s s T > v o i d
U B _ S t a c k < T > : : p u s h ( c o n s t T & i t e m ) {
N o d e < T > * t = n e w N o d e < T > ( i t e m , t h i s - > h e a d _ ) ;
a s s e r t ( t ! = 0 ) ;
t h i s - > h e a d _ = t ;
}
t e m p l a t e < c l a s s T > v o i d
U B _ S t a c k < T > : : p o p ( T & t o p _ i t e m ) {
t o p _ i t e m = t h i s - > h e a d _ - > i t e m _ ;
N o d e < T > * t = t h i s - > h e a d _ ;
t h i s - > h e a d _ = t h i s - > h e a d _ - > n e x t _ ;
d e l e t e t ;
}
t e m p l a t e < c l a s s T >
U B _ S t a c k < T > : : ˜ U B _ S t a c k ( v o i d ) {
/ / d e l e t e a l l N o d e s . . .
f o r ( T t ; t h i s - > h e a d _ ! = 0 ; t h i s - > p o p ( t ) )
c o n t i n u e ;
}
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
O b j e c t - O r i e n t e d I m p l e m e n t a t i o n i n C + +
( c o n t ’ d )
 I t ’ s n o w p o s s i b l e t o w r i t e c o d e t h a t d o e s n ’ t
d e p e n d o n t h e s t a c k i m p l e m e n t a t i o n
 e.g.,
/ / R e q u i r e r e c o m p i l a t i o n i f s u b c l a s s e s c h a n g e
t e m p l a t e < c l a s s T >
S t a c k < T > * m a k e _ s t a c k ( i n t u s e _ B _ S t a c k ) {
/ / F a c t o r y p a t t e r n . . .
i f ( u s e _ B _ S t a c k )
r e t u r n n e w B _ S t a c k < i n t > ;
e l s e
r e t u r n n e w U B _ S t a c k < i n t > ;
}
/ / D o e s n ’ t r e q u i r e r e c o m p i l a t i o n i f
/ / s u b c l a s s e s c h a n g e
v o i d f o o ( S t a c k < i n t > * s t a c k ) {
i n t i ;
s t a c k - > p u s h ( 1 0 0 ) ;
s t a c k - > p o p ( i ) ;
/ / . . .
}
/ / C a l l f o o ( ) .
f o o ( m a k e _ s t a c k < i n t > ( 0 ) ) ;
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
CS 342: OO Software Development Lab OO Programming with C++
Object-Oriented Implementation in C++ (cont’d)
 Moreover, we can make changes at run-time without modifying,
recompiling, or relinking existing code
– i.e., can use “dynamic linking” to select stack representation at
run-time, e.g.,
char stack_symbol[MAXNAMLEN];
char stack_file[MAXNAMLEN];
cin >> stack_file >> stack_symbol;
void *handle = ::dlopen (stack_file);
void *sym = ::dlsym (handle, stack_symbol);
// Note use of RTTI
if (Stack *sp =
dynamic_cast  *> (sym))
foo (sp);
 Note, no need to stop, modify, and restart an executing application!
– Naturally, this requires careful configuration management...
Copyright c
1997-2000 Dept. of Computer Science, Washington University 40
CS 342: OO Software Development Lab OO Programming with C++
Function Prototypes
 C++ supports stronger type checking via function prototypes
– Unlike ANSI-C, C++ requires prototypes for both function
declarations and definitions
– Function prototypes eliminate a class of common C errors
 e.g., mismatched or misnumbered parameter values and return
types
 Prototypes are used for external declarations in header files, e.g.,
extern char *strdup (const char *s);
extern int strcmp (const char *s, const char *t);
file *fopen (const char *filename, const char *type);
extern void print_error_msg_and_die (const char *msg);
Copyright c
1997-2000 Dept. of Computer Science, Washington University 41
CS 342: OO Software Development Lab OO Programming with C++
Function Prototypes (cont’d)
#if defined (__STDC__) || defined (__cplusplus)
extern int freopen (const char *nm, const char *tp, file *s);
extern char *gets (char *);
extern int perror (const char *);
#else /* Original C-style syntax. */
extern int freopen (), perror ();
extern char *gets ();
#endif /* defined (__STDC__) */
int main (int, char *[]) {
char buf[80];
if (freopen ("./foo", "r", stdin) == 0)
perror (freopen), exit (1);
while (gets (buf) != 0)
/* . . . */;
}
Copyright c
1997-2000 Dept. of Computer Science, Washington University 42
CS 342: OO Software Development Lab OO Programming with C++
Function Prototypes (cont’d)
 The preceeding program fails mysteriously if the actual calls are:
/* Extra argument, also out-of-order! */
freopen (stdin, "newfile", 10, ’C’);
/* Omitted arguments. */
freopen ("newfile", "r");
 A “Classic C” compiler would generally not detect erroneous
parameter passing at compile time (though lint would)
– Note, C++ lint utilities are not widely available, but g++ -Wall
and CC +w provide similar typechecking facilities
 Function prototypes are used in both definitions and declarations
Copyright c
1997-2000 Dept. of Computer Science, Washington University 43
CS 342: OO Software Development Lab OO Programming with C++
Function and Operator Overloading
Two or more functions or operators may be given the same name
provided the type signatures are unique
Unique argument types:
double square (double);
Complex &square (Complex &);
Unique number of arguments:
void move (int);
void move (int, int);
A function’s return type is not considered when distinguishing between
overloaded instances
 e.g., the following declarations are ambiguous to the C++ compiler:
extern double operator / (Complex &, Complex &);
extern complex operator / (Complex &, Complex &);
Copyright c
1997-2000 Dept. of Computer Science, Washington University 44
CS 342: OO Software Development Lab OO Programming with C++
C++ Classes
 The class is the basic protection and data abstraction unit in C++
– i.e., rather than “per-object” protection
 The class mechanism facilitates the creation of user-defined
Abstract Data Types (ADTs)
– A class declarator defines a type comprised of data members, as
well as method operators
 Data members may be both built-in and user-defined
– Classes are “cookie cutters” used to define objects
 a.k.a. instances
Copyright c
1997-2000 Dept. of Computer Science, Washington University 45
CS 342: OO Software Development Lab OO Programming with C++
C++ Classes (cont’d)
 For efficiency and C compatibility reasons, C++ has two type
systems
1. One for built-in types, e.g., int, float, char, double, etc.
2. One for user-defined types, e.g., classes, structs, unions,
enums, etc.
 Note that constructors, overloading, inheritance, and dynamic
binding only apply to user-defined types
– This minimizes surprises, but is rather cumbersome to document
and explain . . .
Copyright c
1997-2000 Dept. of Computer Science, Washington University 46
CS 342: OO Software Development Lab OO Programming with C++
C++ Classes (cont’d)
 A class is a type constructor
– e.g., in contrast to an Ada or Java package, or a Modula 2 module
 Note, these are not types, they are encapsulation units
– Until recently, C++ did not have a higher-level modularization
mechanism . . .
 This was a problem for large systems, due to lack of library
management facilities and visibility controls
– Recent versions of the ANSI C++ draft standard include
mechanisms that addresses namespace control and
visibility/scoping, e.g.,
 Name spaces
 Nested classes
Copyright c
1997-2000 Dept. of Computer Science, Washington University 47
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
C++
Classes(cont’d)

G
eneralchara
cteristics
ofC++
classes:
–
Any
n
u
m
ber
ofclass
objects
m
ay
b
e
defined

i.e
.
,objects
,
which
ha
ve
identity
,state
,a
nd
beha
vior
–
Class
objects
m
ay
b
e
dynam
ically
allocated
a
nd
deallocated
–
P
a
ssing
class
objects
,pointers
to
class
objects
,
a
nd
refe
re
n
ce
s
to
class
objects
a
s
para
m
eters
to
fu
n
ctions
a
re
legal
–
V
e
ctors
ofclass
objects
m
ay
b
e
defined

A
class
se
rve
s
the
sa
m
e
purpose
a
s
a
Ja
va
class
,
a
nd
a
sim
ilar
purpose
to
a
C
s
t
r
u
c
t
–
H
o
w
e
ve
r
,itis
e
xtended
to
allo
w
u
se
r-defined
beha
vior
,a
s
w
ell
Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
48
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
C l a s s V e c t o r E x a m p l e
 T h e r e a r e s e v e r a l s i g n i fi c a n t l i m i t a t i o n s w i t h
b u i l t - i n C a n d C + + a r r a y s , e.g.,
– T h e s i z e m u s t b e a c o m p i l e - t i m e c o n s t a n t ,
e.g.,
v o i d f o o ( i n t i ) {
i n t a [ 1 0 0 ] , b [ 1 0 0 ] ; / / O K
i n t c [ i ] ; / / E r r o r !
}
– A r r a y s i z e c a n n o t v a r y a t r u n - t i m e
– L e g a l a r r a y b o u n d s r u n f r o m 0 t o size - 1
– N o r a n g e c h e c k i n g p e r f o r m e d a t r u n - t i m e ,
e.g.,
i n t a [ 1 0 ] , i ;
f o r ( i = 0 ; i < = 1 0 ; i + + )
a [ i ] = 0 ;
– C a n n o t p e r f o r m f u l l a r r a y a s s i g n m e n t s , e.g.,
a = b ; / / E r r o r !
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
Class
V
ectorExam
ple(cont’d)

W
e
ca
n
w
rite
a
C
+
+
class
to
o
ve
rco
m
e
so
m
e
ofthese
lim
itations
,
i.e
.
,
–
co
m
pile-tim
e
co
n
stantsize
–
lack
of
ra
nge
checking

Later
o
n
w
e
’llu
se
otherC++
fe
atures
,su
ch
a
s
inheritance
a
nd
para
m
eterized
types
,to
finish
thejob
, i.e
.
,
–
re
sizing
–
n
o
n
-ze
ro
lo
w
e
rbounds
–
a
rray
a
ssignm
ent
–
type
para
m
eterization
Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
50
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
C l a s s V e c t o r I n t e r f a c e
/ / F i l e V e c t o r . h ( t h i s A D T i s i n c o m p l e t e
/ / w r t i n i t i a l i z a t i o n & a s s i g n m e n t . . . ! )
# i f n d e f _ V E C T O R _ H
# d e f i n e _ V E C T O R _ H
t y p e d e f i n t T ;
c l a s s V e c t o r {
p u b l i c :
V e c t o r ( s i z e _ t l e n = 1 0 0 ) ;
˜ V e c t o r ( v o i d ) ;
s i z e _ t s i z e ( v o i d ) c o n s t ;
b o o l s e t ( s i z e _ t i , T i t e m ) ;
b o o l g e t ( s i z e _ t i , T & i t e m ) c o n s t ;
p r i v a t e :
s i z e _ t s i z e _ ;
T * b u f _ ;
b o o l i n _ r a n g e ( s i z e _ t i ) c o n s t ;
} ;
# e n d i f / * _ V E C T O R _ H * /
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
C l a s s V e c t o r I m p l e m e n t a t i o n
/ / F i l e V e c t o r . c p p .
# i n c l u d e V e c t o r . h
b o o l V e c t o r : : i n _ r a n g e ( s i z e _ t i ) c o n s t
{
r e t u r n i < t h i s - > s i z e ( ) ;
}
b o o l V e c t o r : : s e t ( s i z e _ t i , T i t e m ) {
i f ( t h i s - > i n _ r a n g e ( i ) ) {
t h i s - > b u f _ [ i ] = i t e m ;
r e t u r n t r u e ;
}
e l s e r e t u r n f a l s e ;
}
b o o l V e c t o r : : g e t ( s i z e _ t i , T & i t e m ) c o n s t {
i f ( t h i s - > i n _ r a n g e ( i ) ) {
i t e m = t h i s - > b u f _ [ i ] ;
r e t u r n t r u e ;
}
e l s e r e t u r n f a l s e ;
}
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
Class
V
ectorExam
ple(cont’d)
b
u
f
l
e
n
d
y
n
a
m
i
c
a
l
l
y
a
l
l
o
c
a
t
e
d
m
e
m
o
r
y
o
f
s
i
z
e
l
e
n

The
co
ntrolblock
that
represents
a
n
objectof
class
Ve
cto
r
–
N
ote
,the
co
ntrolblock
m
ay
b
e
allocated
offthe
stack
,the
global
data
segm
ent,o
rthe
heap
–
H
o
w
e
ve
r
,the
buf
field
alw
ays
points
to
m
e
m
o
ry
allocated
offthe
heap
Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
53
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
C l a s s V e c t o r ( A t t e m p t e d ) U s a g e
/ / F i l e t e s t . c p p
# i n c l u d e < l i b c . h >
# i n c l u d e V e c t o r . h
v o i d f o o ( s i z e _ t s i z e ) {
/ / C a l l c o n s t r u c t o r
V e c t o r u s e r _ v e c ( s i z e ) ;
/ / E r r o r , n o d y n a m i c r a n g e
i n t c _ v e c [ s i z e ] ;
c _ v e c [ 0 ] = 0 ;
u s e r _ v e c . s e t ( 0 , 0 ) ;
f o r ( s i z e _ t i = 1 ;
i < u s e r _ v e c . s i z e ( ) ;
i + + ) {
i n t t ;
u s e r _ v e c . g e t ( i - 1 , t ) ;
u s e r _ v e c . s e t ( i , t + 1 ) ;
c _ v e c [ i ] = c _ v e c [ i - 1 ] + 1 ;
}
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
Class
V
ector(Attempted)Usag
e(co
nt’d)
//
E
r
r
o
r
,
private
a
nd
protected
data
inaccessible
size
=
u
s
e
r
_
v
e
c
.
size_
-
1
;
u
s
e
r
_
v
e
c
.buf_[size]
=
100;
//
R
u
n
-
time
e
r
r
o
r
,
index
o
u
t
of
r
a
nge
if
(user_vec.set
(user_vec.size
(),
1000)
=
=
false)
e
r
r
(index
o
u
t
of
r
a
nge);
//
I
ndex
o
u
t
of
r
a
nge
n
o
t
detected
a
t
r
u
n
time!
c
_
v
e
c[size]
=
1000;
//
D
e
s
t
r
u
c
t
o
r
c
alled
when
u
s
e
r
_
v
e
c
leaves
s
c
ope
}Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
55
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
Class
V
ectorExam
ple(cont’d)

N
ote
thatthis
e
xa
m
ple
has
se
ve
ralu
n
n
e
ce
ssa
ry
lim
itations
thata
re
addressed
by
additionalC++
fe
atures
, e.g
.
,
–
s
e
t/get
paradigm
diffe
rs
fro
m
C
’s
b
uilt-in
subscript
n
otation
–
Error
checking
via
return
value
is
so
m
e
whata
wkw
a
rd
–
O
nly
w
o
rks
fo
r
a
ve
ctor
ofint
s
–
Classes
thatinheritfrom
V
e
ctor
m
ay
n
otalw
ays
w
a
ntthe
e
xtra
o
ve
rhead
of
ra
nge
checking
.
.
.

The
follo
w
ing
e
xa
m
ple
illustrates
se
ve
ralm
o
re
adva
n
ced
C++
fe
atures
Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
56
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
C l a s s T e m p l a t e V e c t o r I n t e r f a c e
/ / F i l e V e c t o r . h
/ / t y p e d e f i n t T ;
t e m p l a t e < c l a s s T >
c l a s s V e c t o r {
p u b l i c :
s t r u c t r a n g e _ e r r o r { } ;
V e c t o r ( s i z e _ t l e n = 1 0 0 ) ;
˜ V e c t o r ( v o i d ) ;
s i z e _ t s i z e ( v o i d ) c o n s t ;
T & o p e r a t o r [ ] ( s i z e _ t i )
t h r o w ( r a n g e _ e r r o r ) ;
p r o t e c t e d :
T & e l e m ( s i z e _ t i ) {
r e t u r n t h i s - > b u f _ [ i ] ;
}
p r i v a t e :
s i z e _ t s i z e _ ;
T * b u f _ ;
b o o l i n _ r a n g e ( s i z e _ t i ) ;
} ;
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
C l a s s T e m p l a t e V e c t o r ( A t t e m p t e d )
U s a g e
/ / F i l e t e s t . c p p
# i n c l u d e V e c t o r . h
v o i d f o o ( s i z e _ t s i z e ) {
t r y { / / I l l u s t r a t e s e x c e p t i o n h a n d l i n g . . .
V e c t o r < i n t > u s e r _ v e c ( s i z e ) ;
i n t c _ v e c [ s i z e ] ; / / E r r o r , n o d y n a m i c r a n g e
c _ v e c [ 0 ] = u s e r _ v e c [ 0 ] = 0 ;
f o r ( i n t i = 1 ; i < u s e r _ v e c . s i z e ( ) ; i + + )
{
u s e r _ v e c [ i ] = u s e r _ v e c [ i - 1 ] + 1 ;
c _ v e c [ i ] = c _ v e c [ i - 1 ] + 1 ;
}
/ / E r r o r , p r i v a t e / p r o t e c t e d d a t a i n a c c e s s i b l e
s i z e = u s e r _ v e c . s i z e _ - 1 ;
u s e r _ v e c . b u f _ [ s i z e ] = 1 0 0 ;
u s e r _ v e c . e l e m ( 3 ) = 1 2 0 ;
/ / R u n - t i m e e r r o r !
u s e r _ v e c [ u s e r _ v e c . s i z e ( ) ] = 1 0 0 0 ;
/ / I n d e x r a n g e e r r o r n o t d e t e c t e d a t r u n t i m e !
c _ v e c [ s i z e ] = 1 0 0 0 ;
/ / D e s t r u c t o r c a l l e d s c o p e i s e x i t e d .
}
c a t c h ( V e c t o r < i n t > : : r a n g e _ e r r o r ) { / * . . . * / }
}
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
C++
O
bjects

A
C
+
+
objectis
a
n
in
stance
of
a
class(or
a
ny
otherC++
type
fo
r
that
m
atter
.
.
.)

An
objectca
n
be
instantiated
o
rdisposed
eitherim
plicitly
o
r
e
xplicitly
,depending
o
n
its
life-tim
e

The
life-tim
e
of
a
C
+
+
objectis
either
static
,autom
atic
,
o
rdynam
ic
–
C++
refe
rs
to
this
a
s
the
storage
class
ofa
n
object
Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
59
CS 342: OO Software Development Lab OO Programming with C++
C++ Objects (cont’d)
 Life-time or storage class:
1. Static
– i.e., it lives throughout life-time of process
– static can be used for local, global, or class-specific objects
(note, their scope is different)
2. Automatic
– i.e., it lives only during function invocation, on the run-time stack
3. Dynamic
– i.e., it lives between corresponding calls to operators new and
delete
– Or malloc and free
– Dynamic objects have life-times that extend beyond their
original scope
Copyright c
1997-2000 Dept. of Computer Science, Washington University 60
CS 342: OO Software Development Lab OO Programming with C++
C++ Objects (cont’d)
Uninitialized
Global Data
Initialized
Global Data
Text
Stack
Heap
Automatic
Variables
Dynamic
Variables
Static
Variables
High
Addresses
Low
Addresses
Read-Only
Code and
Data
 Typical layout of memory objects in the process address space
Copyright c
1997-2000 Dept. of Computer Science, Washington University 61
CS 342: OO Software Development Lab OO Programming with C++
C++ Objects (cont’d)
 Most C++ implementations do not support automatic garbage
collection of dynamically allocated objects
– In garbage collection schemes, the run-time system is responsible
for detecting and deallocating unused dynamic memory
– Note, it is very difficult to implement garbage collection correctly
in C++ due to pointers and unions
 Therefore, programmers must explicitly deallocate objects when
they want them to go away
– C++ constructors and destructors are useful for automating
certain types of memory management . . .
Copyright c
1997-2000 Dept. of Computer Science, Washington University 62
CS 342: OO Software Development Lab OO Programming with C++
C++ Objects (cont’d)
 Several workarounds exist, however, e.g.,
– Use Java, Eiffel, or LISP ;-)
– Use inheritance to derive from base class Collectible
 However, this only works then for a subset of classes (i.e.,
doesn’t work for built-in types . . .)
– Use the class-specific new and delete operators to define a
memory management facility using reference counts to reclaim
unused memory
– Adapt Hans Boehm’s conservative garbage collector for C to C++
. . .
 No solution is optimal, however, so storage management is often
performed “by hand” (ugh ;-))
Copyright c
1997-2000 Dept. of Computer Science, Washington University 63
CS 342: OO Software Development Lab OO Programming with C++
Reference Counting
ConferenceCall 935-7538
instantiated by first call to
ConferenceCall::join ("935-7538");
Caller 1 Caller 2 Caller 3
Caller 4
Caller 5
public:
  unsigned int callers_;
private:
  join (const PhoneNumber &);
static ConferenceCall &
int hangUp ();
// . . .
Class ConferenceCall
{
};
 When can we delete
the ConferenceCall
instantiation?
Copyright c
1997-2000 Dept. of Computer Science, Washington University 64
CS 342: OO Software Development Lab OO Programming with C++
C++ Object-Oriented Features
 C++ provides three characteristics generally associated with
object-oriented programming:
– Data Abstraction
 Package a class abstraction so that only the public interface is
visible and the implementation details are hidden from clients
 Allow parameterization based on type
– Single and Multiple Inheritance
 A derived class inherits operations and attributes from one or
more base classes, possibly providing additional operations
and/or attributes
Copyright c
1997-2000 Dept. of Computer Science, Washington University 65
CS 342: OO Software Development Lab OO Programming with C++
C++ Object-Oriented Features (cont’d)
 Dynamic Binding
– The actual type of an object (and thereby its associated
operations) need not be fully known until run-time
– Compare with C++ template feature, which are instantiated at
compile-time
 C++’s object-oriented features encourage designs that
1. Explicitly distinguish general properties of related concepts from
2. Specific details of particular instantiations of these concepts
 e.g., an object-oriented graphical shapes library design using
inheritance and dynamic binding
 This approach facilitates extensibility and reusability
Copyright c
1997-2000 Dept. of Computer Science, Washington University 66
CS 342: OO Software Development Lab OO Programming with C++
C++ Object-Oriented Features (cont’d)
Point
Shape
TriangleCircle
Color
Rectangle
1
1
A
11
 The “OOD challenge” is
to map arbitrarily complex
system architectures into
inheritance hierarchies
Copyright c
1997-2000 Dept. of Computer Science, Washington University 67
CS 342: OO Software Development Lab OO Programming with C++
C++ Object-Oriented Features (cont’d)
 Inheritance and dynamic binding facilitate the construction of
program families and frameworks
– Program families are sets of programs whose common properties
are so extensive that it is advantageous to study the common
properties of the programs before analyzing individual members
– A framework is an integrated set of components that collaborate
to product a reuseable architecture for a family of related
applications
 It also supports the open/closed principle
– i.e., open with respect to extensibility, closed with respect to
stability
Copyright c
1997-2000 Dept. of Computer Science, Washington University 68
CS 342: OO Software Development Lab OO Programming with C++
Inheritance Preview
 A type can inherit or derive the characteristics of another base type.
These derived types act just like the base type, except for an explicit
list of:
1. Operations that are implemented differently, i.e., overridden
2. Additional operations and extra data members
3. Modified method access privileges
 C++ supports both single and multiple inheritance, e.g.,
class X { /* . . . */ };
class Y : public X { /* . . . */ };
class Z : public X { /* . . . */ };
class YZ : public Y, public Z { /* . . . */ };
Copyright c
1997-2000 Dept. of Computer Science, Washington University 69
CS 342: OO Software Development Lab OO Programming with C++
Dynamic Binding Preview
 Dynamic binding is a mechanism used along with inheritance to
support a form of polymorphism
 C++ uses virtual functions to implement dynamic binding:
– The actual method called at run-time depends on the class of the
object used when invoking the virtual method
 C++ allows the class definer the choice of whether to make a
method virtual or not
– This leads to time/space performance vs. flexibility tradeoffs
 Depending on the compiler, virtual methods may introduce a
small amount of overhead for each virtual function call
Copyright c
1997-2000 Dept. of Computer Science, Washington University 70 C S
3
4
2
:
O
O
S
o
f
t
w
a
r
e
D
e
v
e
l
o
p
m
e
n
t
L
a
b
O
O
P
r
o
g
r
a
D
y
n
a
m
i
c
B
i
n
d
i
n
g
P
r
e
v
i
e
w
(
c
o
n
t
’
d
)
s
t
r
u
c
t
X
{
/
/
B
a
s
e
c
l
a
s
s
/
/
N
o
n
-
v
i
r
t
u
a
l
i
n
t
m
(
v
o
i
d
)
{
p
u
t
s
(
"
X
:
:
m
"
)
;
}
/
/
V
i
r
t
u
a
l
v
i
r
t
u
a
l
i
n
t
v
m
(
v
o
i
d
)
{
p
u
t
s
(
"
X
:
:
v
m
"
)
;
}
}
;
s
t
r
u
c
t
Y
:
p
u
b
l
i
c
X
{
/
/
D
e
r
i
v
e
d
c
l
a
s
s
/
/
N
o
n
-
v
i
r
t
u
a
l
i
n
t
m
(
v
o
i
d
)
{
p
u
t
s
(
"
Y
:
:
m
"
)
;
}
/
/
V
i
r
t
u
a
l
v
i
r
t
u
a
l
i
n
t
v
m
(
v
o
i
d
)
{
p
u
t
s
(
"
Y
:
:
v
m
"
)
;
}
}
;
/
/
N
o
t
e
,
c
a
n
a
l
s
o
u
s
e
r
e
f
e
r
e
n
c
e
s
.
v
o
i
d
f
o
o
(
X
*
x
)
{
x
-
>
m
(
)
;
/
/
d
i
r
e
c
t
c
a
l
l
:
_
m
_
1
X
(
x
)
;
x
-
>
v
m
(
)
;
/
/
i
n
d
i
r
e
c
t
c
a
l
l
:
(
*
x
-
>
v
p
t
r
[
1
]
)
} i
n
t
m
a
i
n
(
i
n
t
,
c
h
a
r
*
[
]
)
{
X
x
;
Y
y
;
f
o
o
(
&
x
)
;
/
/
X
:
:
m
,
X
:
:
v
m
f
o
o
(
&
y
)
;
/
/
X
:
:
m
,
Y
:
:
v
m
} C
o
p
y
r
i
g
h
t
c

1
9
9
7
-
2
0
0
0
D
e
p
t
.
o
f
C
o
m
p
u
t
e
r
S
c
i
e
n
c
e
,
W
a
s
h
i
n
g
t
o
n
U
n
i
v
e
r
s
i
t
y
CS 342: OO Software Development Lab OO Programming with C++
Dynamic Binding Preview (cont’d)
 Each class with 1 or more virtual functions generates one or
more virtual tables (vtables)
– Note, multiple inheritance creates multiple vtables
 A vtable is logically an array of pointers to methods
– A vtable is typically implemented as an array of pointers to C
functions
 Each object of a class with virtual methods contains one or more
virtual pointers (vptrs), which point at the appropriate vtable for the
object
– The constructor automatically assigns the vptrs to point to the
appropriate vtable
Copyright c
1997-2000 Dept. of Computer Science, Washington University 72
CS 342: OO Software Development Lab OO Programming with C++
C++ Comments
 C++ allows two commenting styles:
1. The traditional C bracketed comments, which may extend over
any number of lines, e.g., /* This is a multi-line C++ comment */
2. The “continue until end-of-line” comment style, e.g., // This is a
single-line C++ or Java comment
 C-style comments do not nest. However, C++ and C styles nest, so
it is possible to comment out code containing other comments, e.g.,
/* assert (i < size) // check index range */
// if (i != 0 /* check for zero divide */ && 10 / i)
Copyright c
1997-2000 Dept. of Computer Science, Washington University 73
C
S
3
4
2
:
O
O
S
o
f
t
w
a
r
e
D
e
v
e
l
o
p
m
e
n
t
L
a
b
O
O
P
r
o
g
r
a
C
+
+
C
o
m
m
e
n
t
s
(
c
o
n
t
’
d
)

N
a
t
u
r
a
l
l
y
,
i
t
i
s
s
t
i
l
l
p
o
s
s
i
b
l
e
t
o
u
s
e
C
/
C
+
+
p
r
e
p
r
o
c
e
s
s
o
r
d
i
r
e
c
t
i
v
e
s
t
o
c
o
m
m
e
n
t
o
u
t
b
l
o
c
k
s
o
f
c
o
d
e
:
#
i
f
0
/
*
M
a
k
e
s
u
r
e
o
n
l
y
v
a
l
i
d
C
+
+
c
o
d
e
g
o
e
s
*
/
/
*
h
e
r
e
!
i
.
e
.
,
d
o
n
’
t
u
s
e
a
p
o
s
t
r
o
p
h
e
s
!
*
/
#
e
n
d
i
f

B
e
w
a
r
e
o
f
s
u
b
t
l
e
w
h
i
t
e
s
p
a
c
e
i
s
s
u
e
s
.
.
.
i
n
t
b
=
a
/
/
*
d
i
v
i
d
e
d
b
y
4
*
/
4
;
-
a
;
/
*
C
+
+
p
r
e
p
r
o
c
e
s
s
i
n
g
a
n
d
p
a
r
s
i
n
g
.
*
/
i
n
t
b
=
a
-
a
;
/
*
C
p
r
e
p
r
o
c
e
s
s
i
n
g
a
n
d
p
a
r
s
i
n
g
.
*
/
i
n
t
b
=
a
/
4
;
-
a
;

N
o
t
e
,
i
n
g
e
n
e
r
a
l
i
t
i
s
b
e
s
t
t
o
u
s
e
w
h
i
t
e
s
p
a
c
e
a
r
o
u
n
d
o
p
e
r
a
t
o
r
s
a
n
d
o
t
h
e
r
s
y
n
t
a
c
t
i
c
e
l
e
m
e
n
t
s
,
c
o
n
s
t
c
h
a
r
*
x
=
"
h
e
l
l
o
"
;
i
n
t
f
o
o
(
c
o
n
s
t
c
h
a
r
*
=
x
)
;
/
/
O
K
i
n
t
b
a
r
(
c
h
a
r
*
=
x
)
;
/
/
E
r
r
o
r
C
o
p
y
r
i
g
h
t
c

1
9
9
7
-
2
0
0
0
D
e
p
t
.
o
f
C
o
m
p
u
t
e
r
S
c
i
e
n
c
e
,
W
a
s
h
i
n
g
t
o
n
U
n
i
v
e
r
s
i
t
y
CS 342: OO Software Development Lab OO Programming with C++
Type-Safe Linkage
 Type-safe linkage allows the linker to detect when a function is
declared and/or used inconsistently, e.g.,
// File abs.c
long abs (long arg)
{
return arg < 0 ? -arg : arg;
}
// File application.c
#include 
int abs (int arg);
int main (int, char *[]) {
printf (%d\n, abs (-1));
}
Copyright c
1997-2000 Dept. of Computer Science, Washington University 75
CS 342: OO Software Development Lab OO Programming with C++
Type-Safe Linkage (cont’d)
 Without type-safe linkage, this error would remain hidden until the
application was ported to a machine where ints and longs were
different sizes
– e.g., Intel 80286
 Type-safe linkage encodes all C++ function names with the types of
their arguments, called name mangling, e.g.,
long abs (long arg) -> _abs__Fl
int abs (int arg) -> _abs__Fi
 Therefore, the linker may be used to detect mismatches between
function prototypes, function definitions, and function usage
Copyright c
1997-2000 Dept. of Computer Science, Washington University 76
CS 342: OO Software Development Lab OO Programming with C++
Type-Safe Linkage (cont’d)
 Name mangling was originally created to support overload
resolution
 Only function names are mangled
– i.e., variables, constants, enums, and types are not mangled . . .
 On older C++ compilers, diagnostic messages from the linker are
sometimes rather cryptic!
– See the c++filt program . . .
Copyright c
1997-2000 Dept. of Computer Science, Washington University 77
CS 342: OO Software Development Lab OO Programming with C++
Type-safe Linkage (cont’d)
 Language interoperability issues
– This problem arises as a side effect of using type-safe linkage in
C++
– C functions used in C++ code (e.g., standard Unix library
functions) must be explicitly declared as requiring C linkage (i.e.,
names are not mangled) via the new extern C declaration
extern "C" int abs (int i);
double abs (double d);
Complex abs (Complex &c);
int foo (int bar) {
cout << abs (Complex (-10, 4.5)) // calls _abs__F7Complex
<< abs (bar) // calls _abs
<< abs (3.1416); // calls _abs__Fd
}
Copyright c
1997-2000 Dept. of Computer Science, Washington University 78
CS 342: OO Software Development Lab OO Programming with C++
Type-safe Linkage (cont’d)
 Language interoperability issues (cont’d)
– Other syntactic forms of extern blocks:
extern "C" {
char *mktemp (const char *);
char *getenv (const char *);
}
– Encapsulating existing header files
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include 
#ifdef __cplusplus
}
#endif /* __cplusplus */
– Note, extern blocks may also be used to support other
languages, e.g., Fortran, Pascal, Java, Basic, Ada, etc.
Copyright c
1997-2000 Dept. of Computer Science, Washington University 79
CS 342: OO Software Development Lab OO Programming with C++
Inline Functions
 Many programming languages force developers to choose between:
1. Modularity/abstraction (function call)
2. Performance (macro or inline-expansion by-hand)
 C++ allows inline function expansion, which has several advantages:
1. Combines the efficiency of a macro with the type-security and
abstraction of a function call
2. Reduces both execution time and code size (potentially)
3. Discourages the traditional reliance upon macro preprocessor
statements
Copyright c
1997-2000 Dept. of Computer Science, Washington University 80
CS 342: OO Software Development Lab OO Programming with C++
Inline Functions (cont’d)
 Here’s an example of a common C problem with the preprocessor:
– Classic C macro, no sanity-checking at macro expansion time
#define SQUARE(X) ((X) * (X))
int a = SQUARE (10); // ok
int b = SQUARE (a++); // trouble!!! (a++) * (a++)
– C++ inline function template
template inline
T square (T x) { return x * x; }
int c = square (a++); // OK
Copyright c
1997-2000 Dept. of Computer Science, Washington University 81
CS 342: OO Software Development Lab OO Programming with C++
Inline Functions (cont’d)
 Points to consider about inline functions:
1. Class methods that are defined in their declaration are
automatically expanded inline
2. It is difficult to debug code where functions have been inline
expanded and/or optimized
3. Compilers require more time and space to compile when there
are many inline functions
4. Inline functions do not have the pseudo-polymorphic properties of
macros
– However, inline templates approximate this functionality
5. Compilers often have limits on the size and type of function that
can be inlined.
Copyright c
1997-2000 Dept. of Computer Science, Washington University 82
CS 342: OO Software Development Lab OO Programming with C++
Inline Functions (cont’d)
 Size can be deceiving
– e.g., if stack frame is very large:
int foo (void) {
int local_array[1000000];
// . . .
– This can cause surprising explosion of code/data size, e.g.,
int bar (void) { foo (); foo (); }
Copyright c
1997-2000 Dept. of Computer Science, Washington University 83
CS
342:O
O
S
oftw
are
D
evelopm
entLab
O
O
Prog
ra
m
m
ing
w
ith
C++
Inline
Functions(cont’d)

To
sho
w
inlining
in
C
+
+
,
w
e
’lldiscuss
a
sim
ple
ru
n
-tim
e
function
call
trace
fa
cility
–
Pro
vides
a
rudim
entary
debugging
tool

e.g
.
,
u
sefulfo
rlong-ru
n
ning
n
etw
o
rk
se
rve
rs

The
goals
a
re
to
be
able
to:
1.
D
eterm
ine
the
dynam
ic
function
calling
beha
vior
ofthe
prog
ra
m
,
i.e
.
,tracing
2.
Allo
w
fo
rfine-g
rain
co
ntrolo
ve
r
whethertra
cing
is
e
n
abled,e.g
.
,
–
At
co
m
pile-tim
e(remo
ve
alltra
ce
s
ofTra
ce
a
nd
incur
n
o
ru
n
-tim
e
penalty)
–
At
ru
n
-tim
e(via
signals
a
nd/or
co
m
m
a
nd-line
options)
3.
M
ake
it
e
a
sy
to
a
utom
ate
so
u
rce
code
instru
m
e
ntation
–
e.g
.
,
w
rite
a
regular
e
xpression
to
m
atch
function
definitions
a
nd
then
insert
code
a
utom
atically
Copyright
c
1997-2000
Dept.
ofC
om
puterS
cience
,W
ashington
University
84
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
I n l i n e F u n c t i o n s ( c o n t ’ d )
 E x a m p l e o u t p u t :
% C C - D _ _ I N L I N E _ _ m a i n . c p p t r a c e . c p p
% a . o u t 1 0 1
e n t e r i n t m a i n ( i n t a r g c , c h a r * a r g v [ ] ) i n f i l e m a i n . c p p o n l i n
e n t e r v o i d f o o ( v o i d ) ( f i l e m a i n . c p p , l i n e 8 )
e n t e r v o i d f o o ( v o i d ) i n ( f i l e m a i n . c p p , l i n e 8 )
e n t e r v o i d f o o ( v o i d ) i n ( f i l e m a i n . c p p , l i n e 8 )
e n t e r v o i d f o o ( v o i d ) i n ( f i l e m a i n . c p p , l i n e 8 )
e n t e r v o i d f o o ( v o i d ) i n ( f i l e m a i n . c p p , l i n e 8 )
e n t e r v o i d f o o ( v o i d ) i n ( f i l e m a i n . c p p , l i n e 8 )
e n t e r v o i d f o o ( v o i d ) i n ( f i l e m a i n . c p p , l i n e 8 )
e n t e r v o i d f o o ( v o i d ) i n ( f i l e m a i n . c p p , l i n e 8 )
e n t e r v o i d f o o ( v o i d ) i n ( f i l e m a i n . c p p , l i n e 8 )
e n t e r v o i d f o o ( v o i d ) i n ( f i l e m a i n . c p p , l i n e 8 )
e n t e r v o i d f o o ( v o i d ) i n ( f i l e m a i n . c p p , l i n e 8 )
l e a v e v o i d f o o ( v o i d )
l e a v e v o i d f o o ( v o i d )
l e a v e v o i d f o o ( v o i d )
l e a v e v o i d f o o ( v o i d )
l e a v e v o i d f o o ( v o i d )
l e a v e v o i d f o o ( v o i d )
l e a v e v o i d f o o ( v o i d )
l e a v e v o i d f o o ( v o i d )
l e a v e v o i d f o o ( v o i d )
l e a v e v o i d f o o ( v o i d )
l e a v e v o i d f o o ( v o i d )
l e a v e i n t m a i n ( i n t a r g c , c h a r * a r g v [ ] )
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
I n l i n e F u n c t i o n s ( c o n t ’ d )
# i n c l u d e " T r a c e . h "
v o i d f o o ( i n t m a x _ d e p t h ) {
T ( " v o i d f o o ( v o i d ) " ) ;
/ / T r a c e _ _ ( " v o i d f o o ( v o i d ) " , 8 , " m a i n . c p p " )
i f ( m a x _ d e p t h > 0 )
f o o ( m a x _ d e p t h - 1 ) ;
/ / D e s t r u c t o r c a l l e d a u t o m a t i c a l l y .
}
i n t m a i n ( i n t a r g c , c h a r * a r g v [ ] ) {
c o n s t i n t m a x _ d e p t h =
a r g c = = 1 ? 1 0 : a t o i ( a r g v [ 1 ] ) ;
i f ( a r g c > 2 )
T r a c e : : s e t _ n e s t i n g _ i n d e n t ( a t o i ( a r g v [ 2 ] ) ) ;
i f ( a r g c > 3 )
T r a c e : : s t o p _ t r a c i n g ( ) ;
T ( " i n t m a i n ( i n t a r g c , c h a r * a r g v [ ] ) " ) ;
f o o ( m a x _ d e p t h ) ;
r e t u r n 0 ;
/ / D e s t r u c t o r c a l l e d a u t o m a t i c a l l y .
}
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
C S 3 4 2 : O O S o f t w a r e D e v e l o p m e n t L a b O O P r o g r a
I n l i n e F u n c t i o n s ( c o n t ’ d )
# i f n d e f _ T R A C E _ H
# d e f i n e _ T R A C E _ H
# i f d e f N T R A C E / * c o m p i l e - t i m e o m i s s i o n * /
# d e f i n e T ( X )
# e l s e / * ! N T R A C E * /
# d e f i n e T ( X ) T r a c e _ _ ( X , _ _ L I N E _ _ , _ _ F I L E _ _ )
# e n d i f / * ! N T R A C E * /
C l a s s T r a c e { p u b l i c :
T r a c e ( c o n s t c h a r * n , i n t l i n e = 0 ,
c o n s t c h a r * f i l e = ) ;
˜ T r a c e ( v o i d ) ;
s t a t i c v o i d s t a r t _ t r a c i n g ( v o i d ) ;
s t a t i c v o i d s t o p _ t r a c i n g ( v o i d ) ;
s t a t i c i n t s e t _ n e s t i n g _ i n d e n t ( i n t i n d e n t ) ;
p r i v a t e :
s t a t i c i n t n e s t i n g _ d e p t h _ ;
s t a t i c i n t n e s t i n g _ i n d e n t _ ;
s t a t i c i n t e n a b l e _ t r a c i n g _ ;
c o n s t c h a r * n a m e _ ;
} ;
# i f d e f _ _ I N L I N E _ _
# d e f i n e I N L I N E i n l i n e
# i n c l u d e " T r a c e . i "
# e l s e
# d e f i n e I N L I N E
# e n d i f / * _ _ I N L I N E _ _ * /
# e n d i f / * _ T R A C E _ H * /
C o p y r i g h t
c

 1 9 9 7 - 2 0 0 0 D e p t . o f C o m p u t e r S c i e n c e , W a s h i n g t o n U n i v e r s i t y
CS 342: OO Software Development Lab OO Programming with C++
Inline Functions (cont’d)
// Trace.i
#include 
INLINE
Trace::Trace (const char *n, int line, const char *file)
{
if (Trace::enable_tracing_)
fprintf (stderr, "%*senter %s (file %s, line %d)\n,"
Trace::nesting_indent_ *
Trace::nesting_depth_++, "", this->name_ = n, file, line);
}
INLINE
Trace::˜Trace (void)
{
if (Trace::enable_tracing_)
fprintf (stderr, "*sleave %s\n",
Trace::nesting_indent_ *
--Trace::nesting_depth_, "", this->name_);
}
Copyright c
1997-2000 Dept. of Computer Science, Washington University 88 C S
3
4
2
:
O
O
S
o
f
t
w
a
r
e
D
e
v
e
l
o
p
m
e
n
t
L
a
b
O
O
P
r
o
g
r
a
I
n
l
i
n
e
F
u
n
c
t
i
o
n
s
(
c
o
n
t
’
d
)
/
/
T
r
a
c
e
.
c
p
p
#
i
n
c
l
u
d
e
"
T
r
a
c
e
.
h
"
#
i
f
n
d
e
f
_
_
I
N
L
I
N
E
_
_
#
i
n
c
l
u
d
e
"
T
r
a
c
e
.
i
"
#
e
n
d
i
f
/
*
_
_
I
N
L
I
N
E
_
_
*
/
/
/
S
t
a
t
i
c
i
n
i
t
i
a
l
i
z
a
t
i
o
n
s
i
n
t
T
r
a
c
e
:
:
n
e
s
t
i
n
g
_
d
e
p
t
h
_
=
0
;
i
n
t
T
r
a
c
e
:
:
n
e
s
t
i
n
g
_
i
n
d
e
n
t
_
=
3
;
i
n
t
T
r
a
c
e
:
:
e
n
a
b
l
e
_
t
r
a
c
i
n
g
_
=
1
;
v
o
i
d
T
r
a
c
e
:
:
s
t
a
r
t
_
t
r
a
c
i
n
g
(
v
o
i
d
)
{
T
r
a
c
e
:
:
e
n
a
b
l
e
_
t
r
a
c
i
n
g
_
=
1
;
} v
o
i
d
T
r
a
c
e
:
:
s
t
o
p
_
t
r
a
c
i
n
g
(
v
o
i
d
)
{
T
r
a
c
e
:
:
e
n
a
b
l
e
_
t
r
a
c
i
n
g
_
=
0
;
} i
n
t
T
r
a
c
e
:
:
s
e
t
_
n
e
s
t
i
n
g
_
i
n
d
e
n
t
(
i
n
t
i
n
d
e
n
t
)
{
i
n
t
r
e
s
u
l
t
=
T
r
a
c
e
:
:
n
e
s
t
i
n
g
_
i
n
d
e
n
t
_
;
T
r
a
c
e
:
:
n
e
s
t
i
n
g
_
i
n
d
e
n
t
_
=
i
n
d
e
n
t
;
r
e
t
u
r
n
r
e
s
u
l
t
;
} C
o
p
y
r
i
g
h
t
c

1
9
9
7
-
2
0
0
0
D
e
p
t
.
o
f
C
o
m
p
u
t
e
r
S
c
i
e
n
c
e
,
W
a
s
h
i
n
g
t
o
n
U
n
i
v
e
r
s
i
t
y
CS 342: OO Software Development Lab OO Programming with C++
Dynamic Memory Management
 Dynamic memory management is a built-in language construct, e.g.,
int *a = new int[10];
int *b = new int;
// . . .
delete [] a;
delete b;
 Built-in support for memory management improves:
1. Type-security
2. Extensibility
3. Efficiency
Copyright c
1997-2000 Dept. of Computer Science, Washington University 90
CS 342: OO Software Development Lab OO Programming with C++
Const Type Qualifier
 C++ data objects and methods are qualifiable with the keyword
const, making them act as read-only objects
– e.g., placing them in the text segment
– const only applies to objects, not to types
Copyright c
1997-2000 Dept. of Computer Science, Washington University 91
CS 342: OO Software Development Lab OO Programming with C++
Const Type Qualifier, (cont’d)
const char *foo = "on a clear day";
char *const bar = "you can C forever!";
const char *const zippy = "yow!";
foo = "To C or not to C?"; // OK
foo[7] = ’C’; // error, read-only location
// error, can’t assign to const pointer bar
bar = "avoid cliches like the plague.";
// OK, but be careful of read-only memory!!!
bar[1] = ’D’;
const int index = 4 - 3; // index == 1
// read-only an array of constant ints
const int array[index + 3] = {2, 4, 8, 16};
Copyright c
1997-2000 Dept. of Computer Science, Washington University 92
CS 342: OO Software Development Lab OO Programming with C++
Const Type Qualifier (cont’d)
 User-defined const data objects:
– A const qualifier can also be applied to an object of a
user-defined type, e.g.,
const String string_constant (Hi, I’m read-only!);
const Complex complex_zero (0.0, 0.0);
string_constant = "This will not work!"; // ERROR
complex_zero += Complex (1.0); // ERROR
complex_zero == Complex (0.0); // OK
 Ensuring const correctness is an important aspect of designing C++
interfaces, e.g.,
1. It ensures that const objects may be passed as parameters
2. It ensures that data members are not accidentally corrupted
Copyright c
1997-2000 Dept. of Computer Science, Washington University 93
CS 342: OO Software Development Lab OO Programming with C++
Const Type Qualifier (cont’d)
 const methods may specify that certain read-only operations take
place on user-defined const objects, e.g.,
class String {
public:
size_t size (void) const { return this->len_; }
void set (size_t index, char new_char);
// . . .
private:
size_t len;
};
const String string_constant (hello);
string_constant.size (); // Fine
 A const method may not directly modify its this pointer
string_constant.set (1, ’c’); // Error
Copyright c
1997-2000 Dept. of Computer Science, Washington University 94
CS 342: OO Software Development Lab OO Programming with C++
Stream I/O
 C++ extends standard C library I/O with stream and iostream
classes
 Several goals
1. Type-Security
– Reduce type errors for I/O on built-in and user-defined types
2. Extensibility (both above and below)
– Allow user-defined types to interoperate syntactically with
existing printing facilities
– Contrast with printf/scanf-family
– Transparently add new underlying I/O devices to the iostream
model
– i.e., share higher-level formatting operations
Copyright c
1997-2000 Dept. of Computer Science, Washington University 95
CS 342: OO Software Development Lab OO Programming with C++
Stream I/O (cont’d)
 The stream and iostream class categories replace stdin, stdout,
and stderr with cout, cin, and cerr
 These classes may be used by overloading the ¡¡ and ¿¿ operators
– C++ does not get a segmentation fault since the correct function
is called
#include 
char *name = joe;
int id = 1000;
cout << "name = " << name << ", id = " << id << ’\n’;
// cout.operator<< (name = ).operator<< (joe) . . .
– In contrast, old C-style I/O offers no protection from mistakes, and
gets a segmentation fault on most systems!
printf (name = %s, id = %s\n, name, id);
Copyright c
1997-2000 Dept. of Computer Science, Washington University 96
CS 342: OO Software Development Lab OO Programming with C++
Boolean Type
 C++ a bool built-in type
– The bool values are called true and false
– Converting numeric or pointer type to bool takes 0 to false and
anything else to true
– bool promotes to int, taking false to 0 and true to 1
– Statements such as if and while are converted to bool
– All operators that conceptually return truth values return bool
 e.g., the operands of &&, ——, and !, but not &, —, and
Copyright c
1997-2000 Dept. of Computer Science, Washington University 97
CS 342: OO Software Development Lab OO Programming with C++
References
 C++ allows references, which may be:
1. Function parameters
2. Function return values
3. Other objects
 A reference variable creates an alternative name (alias) for an object
 References may be used instead of pointers to facilitate:
1. Increased code clarity
2. Reduced parameter passing costs
3. Better compiler optimizations
 References use call-by-value syntax, but possess call-by-reference
semantics
Copyright c
1997-2000 Dept. of Computer Science, Washington University 98
CS 342: OO Software Development Lab OO Programming with C++
References (cont’d)
 e.g., consider a swap abstraction:
void swap (int x, int y) // pass by value!
{ // requires function call!
int t = x; x = y; y = t;// only works for ints!
}
int main (int, char *[]) {
int a = 10, b = 20;
printf (a = %d, b = %d\n, a, b);
swap (a, b);
printf (a = %d, b = %d\n, a, b);
}
Copyright c
1997-2000 Dept. of Computer Science, Washington University 99
CS 342: OO Software Development Lab OO Programming with C++
References (cont’d)void swap (int *xp, int *yp) {
int t = *xp; *xp = *yp; *yp = t; }
int main (int, char *[]) {
int a = 10, b = 20;
printf (a = %d, b = %d\n, a, b);
swap (&a, &b);
printf (a = %d, b = %d\n, a, b); }
#define swap(X,Y,T) \
do {T __ = (X); (X) = (Y); (Y) = __;} while (0)
int main (int, char *[]) {
int a = 10, b = 20;
printf (a = %d, b = %d\n, a, b);
swap (a, b, int); // beware of a++!
printf (a = %d, b = %d\n, a, b); }
Copyright c
1997-2000 Dept. of Computer Science, Washington University 100
CS 342: OO Software Development Lab OO Programming with C++
References (cont’d)
template  inline void
swap (T &x, T &y) {
T t = x; x = y; y = t; }
int main () {
int a = 10, b = 20;
double d = 10.0, e = 20.0;
printf (a = %d, b = %d\n, a, b);
printf (d = %f, e = %e\n, d, e);
swap (a, b);
swap (d, e);
printf (a = %d, b = %d\n, a, b);
printf (d = %f, e = %e\n, d, e);
}
Copyright c
1997-2000 Dept. of Computer Science, Washington University 101
CS 342: OO Software Development Lab OO Programming with C++
References (cont’d)
10
iir
 With references (as with classes), it is important to distinguish
initialization from assignment, e.g.,
int i = 10;
// initialization of ir
int &ir = i; // Equivalent to: int *const ip = &i;
ir = ir + 10; // dereference is automatically done,
// equivalent to: *ip = *ip + 10;
Copyright c
1997-2000 Dept. of Computer Science, Washington University 102
CS 342: OO Software Development Lab OO Programming with C++
References (cont’d)
 Once initialized, a reference cannot be changed
– i.e., it may not be reassigned to reference a new location
– Note, after initialization all operations affect the referenced object
 i.e., not the underlying const pointer . . .
Copyright c
1997-2000 Dept. of Computer Science, Washington University 103
CS 342: OO Software Development Lab OO Programming with C++
Type Cast Syntax
 C++ introduces a new type cast syntax in addition to Classic C style
casts. This function-call syntax resembles the type conversion
syntax in Ada and Java, e.g.,
// function prototype from math.h library
extern double log10 (double param);
/* C style type cast notation */
if ((int) log10 ((double) 7734) != 0);
// C++ function-style cast notation
if (int (log10 (double (7734))) != 7734);
 This “function call” is performed at compile time
Copyright c
1997-2000 Dept. of Computer Science, Washington University 104
CS 342: OO Software Development Lab OO Programming with C++
Type Cast Syntax (cont’d)
 This type cast syntax is also used to specify explicit type conversion
in the C++ class mechanism
– This allows multiple-argument casts, i.e., “constructors”, e.g.,
class Complex {
public:
Complex (double, double = 0.0);
// . . .
private:
double real, imaginary;
};
// Convert 10.0 and 3.1416 into a Complex object
Complex c = Complex (10.0, 3.1416);
// Note that old-style C syntax would not suffice here . .
Complex c = (Complex) (10.0, 3.1416);
Copyright c
1997-2000 Dept. of Computer Science, Washington University 105
CS 342: OO Software Development Lab OO Programming with C++
Type Cast Syntax (cont’d)
 Note, there are a variety of syntactical methods for constructing
objects in C++, e.g.,
1. Complex c1 = 10.0;
2. Complex c2 = (Complex) 10.0;
3. Complex c3 = Complex (10.0);
4. Complex c4 (10.0);
 I recommend version 4 since it is the most consistent and also
works with built-in types . . .
– It also generalizes to multiple-argument casts . . .
Copyright c
1997-2000 Dept. of Computer Science, Washington University 106
CS 342: OO Software Development Lab OO Programming with C++
Default Parameters
 C++ allows default argument values in function definitions
– If trailing arguments are omitted in the actual function call these
values are used by default, e.g.,
void assign_grade (char *name, char *grade = A);
// additional declarations and definitions . . .
assign_grade (Bjarne Stroustrup, C++);
// Bjarne needs to work harder on his tasks
assign_grade (Jean Ichbiah);
// Jean gets an A for Ada!
 Default arguments are useful in situations when one must change a
class without affecting existing source code
– e.g., add new params at end of argument list (with default values)
Copyright c
1997-2000 Dept. of Computer Science, Washington University 107
CS 342: OO Software Development Lab OO Programming with C++
Default Parameters (cont’d)
 Default parameter passing semantics are similar to those in
languages like Java:
– e.g., only trailing arguments may have defaults
// Incorrect
int x (int a = 10, char b, double c = 10.1);
– Note, there is no support for named parameter passing
 However, it is not possible to omit arguments in the middle of a call,
e.g.,
extern int foo (int = 10, double = 2.03, char = ’c’);
foo (100, , ’d’); /* ERROR!!! */
foo (1000); /* OK, calls foo (1000, 2.03, ’c’);
 There are several arcane rules that permit successive redeclarations
of a function, each time adding new default arguments
Copyright c
1997-2000 Dept. of Computer Science, Washington University 108
CS 342: OO Software Development Lab OO Programming with C++
Declaration Statements
 C++ allows variable declarations to occur anywhere statements
occur within a block
– The motivations for this feature are:
1. To localize temporary and index variables
2. Ensure proper initialization
– This feature helps prevent problems like:
int i, j;
/* many lines of code . . . */
// Oops, forgot to initialize!
while (i < j) /* . . . */;
– Instead, you can use the following
for (int i = x, j = y; i < j; )
/* . . . */;
Copyright c
1997-2000 Dept. of Computer Science, Washington University 109
CS 342: OO Software Development Lab OO Programming with C++
Declaration Statements (cont’d)
 The following example illustrates declaration statements and also
shows the use of the scope resolution operator
#include 
struct Foo { Static int var; };
int Foo::var = 20;
const int MAX_SIZE = 100;
int var = 10;
Copyright c
1997-2000 Dept. of Computer Science, Washington University 110
CS 342: OO Software Development Lab OO Programming with C++
Declaration Statements (cont’d)int
main (int, char *[]) {
int k;
k = call_something ();
// Note the use of the scope resolution operator
// (::) to access the global variable {\tt var}
int var = ::var - k + Foo::var;
for (int i = var; i < MAX_SIZE; i++)
for (int j = 0; j < MAX_SIZE; j++) {
int k = i * j;
cout << k;
double var = k + ::var * 10.4;
cout << var;
}
}
Copyright c
1997-2000 Dept. of Computer Science, Washington University 111
CS 342: OO Software Development Lab OO Programming with C++
Declaration Statements (cont’d)
 However, the declaration statement feature may encourage rather
obscure code since the scoping rules are not always intuitive or
desirable
 Note, ANSI C++ allows definitions in the switch, while, and if
condition expressions . . .
 The scope of the definition of i in the following loops is limited to the
body of the for loop:
for (int i = 0; i < 10; i++)
/* . . . */;
for (int i = 0; i < 20; i++)
/* . . . */;
However, older compilers don’t restrict the scope, so they’ll complain
about multiple declarations of i.
Copyright c
1997-2000 Dept. of Computer Science, Washington University 112
CS 342: OO Software Development Lab OO Programming with C++
Abbreviated Type Names
 Unlike C, C++ allows direct use of user-defined type tag names,
without requiring a preceding union, struct, class, or enum
specifier, e.g.,struct Tree_Node { // C code
int item_;
struct Tree_Node *l_child_, *_child_;
};
struct Tree_Node { // C++ code
int item_;
Tree_Node *l_child_, *r_child_;
}
 Another way of looking this is to say that C++ automatically
typedefs tag names, e.g.,
typedef struct Tree_Node Tree_Node;
Copyright c
1997-2000 Dept. of Computer Science, Washington University 113
CS 342: OO Software Development Lab OO Programming with C++
Abbreviated Type Names, (cont’d)
 Note, this C++ feature is incompatible with certain classic and ANSI
C identifier naming conventions, e.g.,
struct Bar { /* . . . */ };
struct Foo { };
typedef struct Foo Bar; // Illegal C++, legal C!
Copyright c
1997-2000 Dept. of Computer Science, Washington University 114
CS 342: OO Software Development Lab OO Programming with C++
User-Defined Conversions
 The motivation for user-defined conversions are similar to those for
operator and function overloading
– e.g., reduces tedious redundancy in source code
– However, both approaches have similar problems with readability
. . .
 User-defined conversions allow for more natural looking
mixed-mode arithmetic for user-defined types, e.g.,
Complex a = Complex (1.0);
Complex b = 1.0; // implicit 1.0 -> Complex (1.0)
a = b + Complex (2.5);
a = b + 2.5 // implicit 2.5 -> Complex (2.5)
String s = a; // implicit a.operator String ()
Copyright c
1997-2000 Dept. of Computer Science, Washington University 115
CS 342: OO Software Development Lab OO Programming with C++
User-Defined Conversions (cont’d)
 Conversions come in two flavors:
1. Constructor Conversions: Create a new object from objects of
existing types
2. Conversion Operators: Convert an existing object into an object of
another type
class Complex {
public:
Complex (double); // convert double to Complex
operator String (); // convert Complex to String
// . . .
};
int foo (Complex c) {
c = 10.0; // c = Complex (10.0);
String s = c; // c.operator String ();
cout << s; }
Copyright c
1997-2000 Dept. of Computer Science, Washington University 116
CS 342: OO Software Development Lab OO Programming with C++
User-Defined Conversions (cont’d)
 The compiler first tries a single level of user-defined conversion to
determine if a type-signature matches a particular use, e.g.,
class String {
public:
String (const char *s);
String &operator += (const String &);
};
String s;
s += hello; // s += String (hello);
 Note, it is easy to make a big mess by abusing the user-defined
conversion language feature . . .
– Especially when conversions are combine with templates,
inheritance virtual methods, and overloading, etc.
Copyright c
1997-2000 Dept. of Computer Science, Washington University 117
CS 342: OO Software Development Lab OO Programming with C++
Static Initialization
 Static initializers can be comprised of arbitrary C++ expressions,
e.g.,
extern int foo (void); // file scope
int a = 100;
int i = 10 + foo ();
int j = i + *new int (1);
int foo (void) {
static int k = foo ();
return 100 + a;
}
 Note, needless to say, this can become rather cryptic, and the order
of initialization is not well defined between modules
Copyright c
1997-2000 Dept. of Computer Science, Washington University 118
CS 342: OO Software Development Lab OO Programming with C++
Miscellaneous Differences
 In C++, sizeof (’a’) == sizeof (char); in C, sizeof (’a’) ==
sizeof (int)
– This facilitates more precise overloading . . .
 char str[5] = hello is valid C, but C++ gives error because initializer
is too long (because of hidden trailing ’
0’)
 In C++, a function declaration int f(); means that f takes no
arguments (same as int f(void);). In C it means that f can take
any number of arguments of any type at all!
– C++ would use int f ( . . .);
 In C++, a class may not have the same name as a typedef
declared to refer to a different type in the same scope
Copyright c
1997-2000 Dept. of Computer Science, Washington University 119
CS 342: OO Software Development Lab OO Programming with C++
Miscellaneous Differences (cont’d)
 In C++, a struct or class is a scope; in C a struct, enum, or
enum literal are exported into the global scope e.g., struct Foo
enum Bar baz, foobar, bizbuzz; ; /* Valid C, invalid C++ */ enum Bar
bar = baz; // Valid C++, invalid C Foo::Bar bar = Foo::baz;
 The type of an enum literal is the type of its enumeration in C++; in
C it is an int, e.g.,
/* True in C, not necessarily true in C++. */
sizeof baz == sizeof (int);
/* True in C++, not necessarily true in C. */
sizeof Foo::baz == sizeof (Foo::Bar);
Copyright c
1997-2000 Dept. of Computer Science, Washington University 120
CS 342: OO Software Development Lab OO Programming with C++
Miscellaneous Differences (cont’d)
 In ANSI C, a global const has external linkage by default; in C++ it
has internal linkage, e.g., /* In C++, global1 is not visible to other
modules. */ const int global1 = 10; /* Adding extern makes it visible
to other modules. */ extern const int global2 = 100;
 In ANSI C, a void * may be used as the right-hand operand of an
assignment or initialization to a variable of any pointer type, whereas
in C++ it may not (without using a cast . . .)
void *malloc (size_t);
/* Valid C, invalid C++ */
int *i = malloc (10 * sizeof *i);
/* Valid C, valid C++ */
int *i = (int *) malloc (10 * sizeof *i);
Copyright c
1997-2000 Dept. of Computer Science, Washington University 121
C
S
3
4
2
:
O
O
S
o
f
t
w
a
r
e
D
e
v
e
l
o
p
m
e
n
t
L
a
b
O
O
P
r
o
g
r
a
S
u
m
m
a
r
y

C
+
+
a
d
d
s
m
a
n
y
,
m
a
n
y
,
m
a
n
y
n
e
w
f
e
a
t
u
r
e
s
t
o
t
h
e
C
p
r
o
g
r
a
m
m
i
n
g
l
a
n
g
u
a
g
e

I
t
i
s
n
o
t
n
e
c
e
s
s
a
r
y
t
o
u
s
e
a
l
l
t
h
e
f
e
a
t
u
r
e
s
i
n
o
r
d
e
r
t
o
w
r
i
t
e
e
f
fi
c
i
e
n
t
,
u
n
d
e
r
s
t
a
n
d
a
b
l
e
,
p
o
r
t
a
b
l
e
a
p
p
l
i
c
a
t
i
o
n
s

A
m
a
j
o
r
c
o
n
t
r
i
b
u
t
i
o
n
o
f
C
+
+
i
s
i
t
s
s
u
p
p
o
r
t
f
o
r
d
e
fi
n
i
n
g
a
b
s
t
r
a
c
t
d
a
t
a
t
y
p
e
s
(
A
D
T
s
)
–
e.
g.
,
cla
ss
es
,
pa
ra
m
et
er
ize
d
ty
pe
s,
a
n
d
ex
ce
pt
io
n
ha
nd
lin
g

F
o
r
s
o
m
e
s
y
s
t
e
m
s
,
C
+
+
’
s
A
D
T
s
u
p
p
o
r
t
i
s
m
o
r
e
i
m
p
o
r
t
a
n
t
t
h
a
n
u
s
i
n
g
t
h
e
O
O
f
e
a
t
u
r
e
s
o
f
t
h
e
l
a
n
g
u
a
g
e

F
o
r
o
t
h
e
r
s
y
s
t
e
m
s
,
t
h
e
u
s
e
o
f
C
+
+
’
s
O
O
f
e
a
t
u
r
e
s
i
s
e
s
s
e
n
t
i
a
l
t
o
b
u
i
l
d
h
i
g
h
l
y
fl
e
x
i
b
l
e
a
n
d
e
x
t
e
n
s
i
b
l
e
s
o
f
t
w
a
r
e
–
e.
g.
,
in
he
rit
an
ce
,
dy
na
m
ic
bi
nd
in
g,
a
n
d
RT
TI
C
o
p
y
r
i
g
h
t
c

1
9
9
7
-
2
0
0
0
D
e
p
t
.
o
f
C
o
m
p
u
t
e
r
S
c
i
e
n
c
e
,
W
a
s
h
i
n
g
t
o
n
U
n
i
v
e
r
s
i
t
y