ebook55-1



Similar documents
ebook55-13

新版 明解C++入門編

FY.DOC

c_cpp

新・解きながら学ぶC言語

新・明解C言語入門編『索引』

新版 明解C言語入門編

C/C++ - 文件IO

IO

_汪_文前新ok[3.1].doc

Strings

CC213

Microsoft Word - 物件導向編程精要.doc

untitled

提问袁小兵:

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

untitled

untitled

C/C++程序设计 - 字符串与格式化输入/输出

Microsoft Word - 01.DOC

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C Project 30 C Project 3 60 Project 40

ebook15-C

一 耀 州 青 瓷 的 裝 飾 手 法 與 紋 飾 種 類 耀 州 窯 的 裝 飾 紋 樣, 豐 富 多 變, 而 且 題 材 內 容 廣 泛, 組 合 形 式 多 樣, 圖 案 形 象 優 美, 令 人 賞 心 悅 目, 並 且 反 映 了 當 時 社 會 的 審 美 趣 味 和 理 想 裝 飾

nooog

得 到 了 補 償. 對 於 武 姜 而 言, 莊 公 與 自 己 的 關 係 並 不 親 密, 而 共 叔 段 又 是 自 己 向 來 疼 愛 有 加 的 兒 子, 所 以, 對 莊 公 提 出 再 怎 麼 無 理 的 要 求, 武 姜 也 不 會 覺 得 有 什 麼 不 妥 之 處, 而 對 共

C/C++ - 字符串与字符串函数

Windows RTEMS 1 Danilliu MMI TCP/IP QEMU i386 QEMU ARM POWERPC i386 IPC PC104 uc/os-ii uc/os MMI TCP/IP i386 PORT Linux ecos Linux ecos ecos eco

C++ 程式設計

C/C++语言 - C/C++数据

新・解きながら学ぶJava

## $%& %& ## () #) (( * (+++ () #) #) (+ (+ #) #) ( #, - #)). #))- # ( / / / / $ # ( * *..# 4 #$ 3 ( 5 ) ### 4 $ # 5, $ ## # 4 $# 5 ( %

(\244j\257d\276\307\274\351_ C.indd_70%.pdf)

Microsoft PowerPoint - OPVB1基本VB.ppt

プログラムの設計と実現II

epub83-1

Microsoft PowerPoint - plan08.ppt

ebook10-5

全国计算机技术与软件专业技术资格(水平)考试

untitled

Microsoft Word 軟體設計第二部份範例試題_C++_ _1_.doc

Microsoft Word - CPE考生使用手冊 docx

ebook50-15

概述

ebook

ebook140-9

ebook39-5

<4D F736F F D20A4D5A46CA1D0A740A4E5A5FEB6B02E646F63>

口 的 70% 连 南 县 的 瑶 族. 有 排 瑶 过 山 瑶 排 瑶 6 万 多 人 住 在 三 排 南 岗 i 雨 水 大 麦 山 大 坪 香 坪 盘 石 金 坑 8 个 乡 镇. 形 成 了 占 全 县 面 积 80% 的 聚 居 地 << 连 州 志 } 卷 八 排 瑶 志 曰 在 连 者

ebook8-30

(procedure-oriented)?? 2

51 C 51 isp 10 C PCB C C C C KEIL

C 1

前言 C# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii

untitled

chp6.ppt

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

,,,,,,,,,, ( http: \ \ www. ncre. cn,, ) 30,,,,,,,, C : C : : 19 : : : /16 : : 96 : : : ISBN 7

untitled

.' 6! "! 6 "'' 6 7% $! 7%/'& 人 类 非 洲 锥 虫 病 又 称 昏 睡 病 是 布 氏 锥 虫 冈 比 亚 亚 种!! 或 布 氏 锥 虫 罗 得 西 亚 种 "#$$ %! &'!!! 感 染 引 起 的 一 种 寄 生 虫 病 以 采 采 蝇! 为 传 播 ' 媒

Java

Strings

Microsoft Word - chap10.doc

科学计算的语言-FORTRAN95

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023)

Microsoft Word - 11.doc

ebook39-6

(2) 廠 商 具 有 維 修 維 護 或 售 後 服 務 能 力 之 證 明 ;(3) 廠 商 具 有 製 造 供 應 或 承 做 能 力 之 證 明 ;(4) 具 有 相 當 人 力 之 證 明 屬 特 定 資 格 之 ㄧ 8.(3) 機 關 辦 理 預 算 金 額 為 新 台 幣 四 億 元

Transcription:

1 O O P O O P 2 1.1 C + + [1] 1.1.1 + 60 S i m u l a - 67 O O P O O P S m a l l t a l k C++ O O P 1.1.2 [1] The Tao of Objects Gary Entsminger

2 C + + 1.1.3 O O P O O P

1 3 1 4 v i r t u a l v i r t u a l C + + Vi r t u a l 1.1.4 OOP C C++ C C + + m a i n () C C + + C 1.2 C++ C + + C O O P C O O P C C C C + + O O P C + + C + + C + + 1.2.1 C C + + C C + +

4 C + + C + + r e f e r e n c e s ( ) C 1.2.2 C + + C C + + C C + + C + + 1.2.3 C + + C C + + C + + C C + + C + + C 10 % O O P C 1.2.4 1.2.5 C + + C

1 5 1.2.6 C C + + 17 1.2.7 B A S I C C 50 000 C B A S I C C + + h e l l o - w o r l d O O P 1.3 O O P C + + C + + C + + 1.3.1 1) 2)

6 C + + 1.3.2 C + + 50 F O RT R A N F O R m u l a - T R A N s l a t i o n C O B O L COmmon Business-Oriented Language L i s p L i s t - P r o c e s s i n g A P L A Programming L a n g u a g e L i s p A P L m i s s i o n F O RT R A N C O B O L F O RT R A N C O B O L A l g o l P a s c a l P r o l o g F O RT H F O RT H B A S I C Beginners All-purpose Symbolic Instruction Code 60 A P L B A S I C A P L

1 7 1.3.3. F o o b l e B l a h 1.

8 C + + 1) 2) 2. 80 % 2 0 % 20 % 3. OOP O O P O O P

1 9 6 50 50 O O P O O P O O P 1.3.4 [ 1 ] 1) 2) 3) 4) 5) 1) 2) 3) 4) 5) [1] Design Patterns:Elements of Reusable Object-Oriented Software by Erich Gamma et al., Addison-We s l e y, 1995

10 C + + 1.3.5 1. O O P C + + [ 1 ] 2. O O P 1.3.6 1. C + + 1) 2) [1] A reference to vampires made in The Mythical Man-Month, by Fred Brooks, Addision-We s l e y, 1975

1 11 2. 1) 2) 3. 1) 2) 3) 4) 5) C + + O O P 6) 7) 4. Michael Wi l k O O P 5.

12 C + + Software Creativity Robert Glass P r e n t i c e - H a l l, 1995 G l a s s P. J. P l a u g e r [ 1 ] Pe o p l e w a r e Tom Demarco Timothy Lister Dorset House,1987 X Y Z C o m p l e x i t y, M. Mitchell Wa l d r o p Simon & Schuster, 1992 Santa Fe, New Mexico 1.4 Robert McKee [2] M c K e e [1] Object Lessons Tom Love, SIGS Books, 1993 [2] Through Two Arts, Inc.,12021 Wilshire Blvd. Suite 868, Los Angeles, CA 90025

1 13 1.4.1 1) C + + 2) C + + D B M S D B M S w i l d - c a r d [ 1 w i l d - c a r d ] w i l d - c a r d w i l d - c a r d w i l d - c a r d O O P B r o o k s [2] O O P [1] w i l d - c a r d [2] The Mythical Man-Month,

14 C + + O O P [ 1 ] C + + [ 2 ] 1.4.2 1.4.3 (treatment) 1.4.4 1. [1] Microsoft Wo r d [2]

1 15 2. derived:public base O c c a m s Razor 3. o o p o o p

16 C + + 1.4.5 1. main( ) main( ) 2. 1) 2) 3) / / # [ 1 ] // #[2] Rich Text Format(RT F ) 4) 5)

1 17 1.4.6 # i n c l u d e m a i n () A B C m a i n () 1.4.7 1.5 [ 1 20 ] [1] Object Analysis and Design:Description of Methods, edited by Andrew T. F.Hutt of Object Management Group(OMG),john Wiley & Sons, 1994

18 C + + 1.5.1 Booch B o o c h [1] O O P 1. 2. 3. CRC Class, Responsibility, Collaboration, CRC) 4. 5. B o o c h / C R C [1] Object-Oriented Design with Applications by Grady Booch, Benjamin Cummings, 1991. C + +

1 19 1.5.2 RDD [1] C R C B o o c h R D D R D D 1) 2) 3) 4) 1.5.3 OMT [ 2 ] O M T B o o c h R D D O M T 1) B o o c h R D D 2) 3) 1.6 OOP O O P O O P C C + + 1.6.1 OOP O O P C + + 1. C 6 9 [1] Designing Object-Oriented Software by Rebecca Wirfs-Brock et al., Prentice Hall, 1990 [2] Object-Oriented Modeling and Design by James Rumbaugh et al., Prentice Hall,1991

20 C + + 2. O O P C + + 3. [1] 4. C + + m a i n ( ) O O P C + + 5. C + + C + + C C + + C + + O O P 1.6.2 C + + C C + + O O P C + + 1. C + + C + + [1] Camma et al.,

1 21 C + + 2. O O P O O P C + + C + + C C + + 10 % C + + C C++ C C C + + C C + + C C + + 3. O O P C + + O O P 1.7 C + + O O P C + + O O P O O P O O P C + + O O P C + + C + C + + C + +

2 C + + C L I B L I B U N I X Wi n d o w s D D L D D C + + C C + + 2.1 A int A e x t e r n e x t e r e x t e r n

2 23 int f(float,char); f C + + C C + + g r e p a w k 2.2 C C C s t r u c t s t r u c t C s t r u c t s t r u c t s t a s h

24 C + + s t r u c t s t r u c t C t y p e d e f s t r u c t s t r u c stash A, B, C; C C C s t o r a g e unsigned char* C s t a s h v o i d * O B J L I B D D L

2 25 #include #include

26 C + + initialize( ) struct stash s t o r a g e size add( ) s t a s h inflate( ) v o i d * C memcpy( ) memcpy( ) &(S->storage[S->next * S->size]) n e x t & memcpy( ) n e x t fetch( ) fetch( ) add( ) C count( ) struct stash i n t S t a s h i n t S t a s h. n e x t c o u n t (& i n t S t a s h ) s t a s h s t r u c t n e x t n e x t s t a s h s t o r a g e 200 a i r p l a n e 20 C malloc( ) calloc( ) realloc( ) free( ) inflate( ) realloc( ) s t a s h realloc( ) initialize( ) realloc( ) assert( ) malloc( ) calloc( ) realloc( ) C free( ) Microsoft Wi n d o w s

2 27 assert( ) A S S E RT. H assert( ) C 17 C + + free( ) free( ) malloc( ) calloc( ) realloc( ) cleanup( ) s t a s h i n t 80

28 C + + main( ) s t a s h i n t S t a s h s t r i n g S t a s h L I B T E S T.C stringstash C s t a s h fopen( ) FILE struct FILE struct fgets( ) fclose( ) FILE struct s t r u c t C s t a s h i n t S t a c f o r count( ) s t r i n g S t a s h w h i l e fetch( ), C C C i n t i n t LIB.H stash C C void foo(float) i n t f l o a t.o.obj L I B T E S T.C initialize( ) fetch( ) ( ) LIB.C, L I B T E S T.C

2 29 C f o o ( i n t ) f o o ( f l o a t ) _ f o o _ f o o foo( ) i n t foo( ) f l o a t f l o a t 2.3 c p p : cpp libtest.c lib.c U N I X make m a k e [1] make make make makefile makefile m a k e f i l e C. h.c C++ Unix.H.C M S - D O S DOS C++.hxx. c x x C C + +. c p p. h 2.4 stash C [1] C++ Inside & Out, (Osborne/McGraw-Hill,1993)

30 C + + C C initialize( ) cleanup( ) C C C initialize( ) cleanup( ) stash_initialize( ) stash_cleanup( ) struct s t r u c t C + + struct s t r u c t struct 2.5 C + + stash // C C C + + //

2 31 t y p e d e f C + + t y p e d e f i n t c h a r f l o a t d o u b l e stash s t r u c t C C + + C stash A, B, C; add( ) C stash_initialize( ) stash_cleanup( ) struct stash initialize( ) initialize( ) initialize( ) struct stash s t r u c t C + + :: struct initialize( ) s t a s h stash::initialize(int Size, int Quantity) stash C + +

32 C + + C++ C struct s->size = size size = size s - > C++ C + +

2 33 C S stash* C + + t h i s s t r u c t C S C this->size = Si z e ; t h i s C inflate( ) v o i d * S->storage = v; C++ C v o i d * malloc( ) calloc( ) realloc( ) C C + + C + + C + + s t r u c t C++ s t r u c t s t r u c t C + + v o i d * void* void* void* calloc( ) realloc( ) unsigned char* C + + C C C C++ C C++ C C++ C C + + C C + + stash C + +

34 C + +. this C + + intstash.initialize(sizeof(int), 100) s t a s h _ i n i t i a l i z e (& i n t S t a s h, sizeof(int), 100) C + + c f r o n t AT & T C C c f r o n t C + + C C++ while(cp = (char*)stringstash.fetch(i++)) C++ 2.6 C++ C

2 35 C struct O O P O O S i m u l a - 67 1 967 S c a n d i n a v i a S i m u l a Simula class C++ o b j e c t - b a s e d C++ 2.7 [ 1 ] f l o a t float int stash add( ) fetch( ) inflate( ) stash S s t a s h float f float stash s t a s h abstract dada type C + + s t a s h stash o b j e c t. m e m b e r _ f u n c t i o n ( a rg l i s t ) stash S S. a d d (& i ) S add( ) C++ 2.8 C C C s t r u c t C struct C++ C C + + C [1]

36 C + + struct 14 16 s i z e o f struct 200 int struct B s t r u c t C C++ s t r u c t printf () sizeof C++ C C++ 2.9 C C C++ C P P

2 37 C P P C C++ C++ C++ F O O. H FOO.H #ifndef FOO_H_ #define FOO_H_ // Rest of header here... #endif // FOO_H_ C C++ main( ) 2.10

38 C + + struct l i n k link link next head struct link link* head C initialize( ) stack initialize( ) cleanup( ) push( ) pop( ), d a t a data peek( ) data cleanup data

2 39 struct stack::link::initialize( ) stack::initialize( ) head stack::push( ) stack malloc( ) link l i n k initialize( ) link next h e a d head link link stack::pop( ) data head stack::cleanup( ) cursor free( ) l i n k d a t a link

40 C + + A A

2 41 S::f( ) f( ) A 2.11 C++ C + + struct 2.12 1) struct 2) 3) 4) 5) C++ struct typedef

3 C C + + s t r u c t s t r u c s t r u c t t y p e d e f C + + s t r u c t 3.1 C s t r u c t s t r u c i n i t i a l i z e () c l e a n u p () s t r u c t C s t r u c t s t a c k malloc() 3.2 C++ C + + s t r u c t p u b l i c p r i v a t e p r o t e c t e d s t r u c t p u b l i c p u b l i s t r u c t s t r u c t

3 43 p r i v a t e p r i v a t e struct B

44 C + + f o o () B m a i n () s t r u c t s t r u c t protected) p r o t e c t e d p r o t e c t e d p r i v a t e p r o t e c t e d p r i v a t e 13 3.3 s t r u c t s t r u c s t r u c t s t r u c t

3 45 struct Y f( ) X C + + struct Y Y :: f(x*) struct X Y :: f(x*) struct X Y :: f(x*) X X Y :: g(x) X Y :: f(x*) struct X X s t r u c t, struct X Y :: f(x*) Y g( ) g( f r i e n d s t r u c t s t r u c t :friend struct Z s t r u c t 3.3.1 s t r u c t s t r u c t

46 C + + s t r u c t f r i e n d

3 47 struct holder p o i n t e r, p o i n t e r p o i n t e r h o l d e r s t r u c t p o i n t e r h o l d e r friend holder : :pointer ; s t r u c t p o i n t e r p o i n t e r h o l d e r main( ) p o i n t e r C h o l d e r

48 C + + 3.3.2 f r i e n d C + + f r i e n d C + + 3.4 2 C s t r u c t C + + s t r u c t C + + C struct C s t r u c t C + + C + + C C + + s t r u c t s t r u c t C C + + s t r u c t 3.5 s t r u c t p r i v a t e C s t r u c t S i m u l a - 67 c l a s s S t r o u s t r u p C + +

3 49 C s t r u c t c l a s s C + + s t r u c t c l a s s s t r u c t p u b l i c C + + c l a s s c l a s s S t r o u s t r u p s t r u c t s t r u c t p u b l i c

50 C + + m X Y x m 3.5.1 stash 2

3 51 i n f l a t e ( ) a d d ( ) 3.5.2 stack s t a c k s t a c k 3.6 handle classes C + + C + + 3.6.1

52 C + + 3.6.2 handle classes Cheshire Cat [ 1 ] s m i l e struct cheshire; cheshire [1] John Carolan Lewis Carroll C + +

3 53 cheshire struct handle::cheshire { [ 1 h a n d l e :: i n i t i a l i z e () cheshire struct ] h a n d l e :: c l e a n u p () H A N D L E. C P P c h e s h i r e H A N D L E. C P P h a n d l e [1] 12 n e w

54 C + + 3.7 C + + 3.8 1) p u b l i c private p r o t e c t e d 2) f r i e n d 3) H A N D L E. C P P c h e s h i r e U S E H A N D L. C P P

4 2 C s t r u c t C C + + C + + C C + + C s t r u c t s t r u c t C s t r u c t C + + C + + 4.1 s t a s h s t a c k i n i t i a l i z e () C + + S t r o u s t r u p 3

56 C + + a a X :: X () t r e e tree t(12); // 12 t r e e ( i n t ) i n i t i a l i z e () C + + v o i d 4.2 C?? C + + ~ class Y { p u b l i c : } ; ~ Y ( ) ;

4 57 g o t o C g o t o C + + g o t o C setjmp() l o n g j m p ()

58 C + + 4.3 C P a s c a l C + + C C + + C C C + + C + +

4 59 b u f x y g C 4.3.1 for C + + f o r f o r C + + i j f o r C f o r i j i _ l o o p _ c o u n t e r i C + + f o r

60 C + + 4.3.2 C s w i t c h g o t o g o t o s w i t c h 12

4 61 4.4 stash c l e a n u p () s t a s h i n i t i a l i z e ( ) i n i t i a l i z e () c l e a n u p ()

62 C + + s t a s h

4 63 c l e a n u p () i n t S t a s h s t r i n g S t a s h 4.5 stack s t a c k s t a c k l i n

64 C + + l i n k s t a c k : : p u s h [ 1 ] C + + [1]

4 65 n e w 12 C s t a c k t e x t l i n e s 4.6 s t r u c t c l a s s C + + int a[5]={1,2,3,4,5};

66 C + + int b[6]={0}; 0 f o r 1 f o r int c[] = {1,2,3,4}; sizeof c/sizeof *c( ) for(int i = 0; i< sizeof c / sizeof *c; i++) c [ i ] + + ; s t r u c t C s t r u c struct X { } ; int i; float f; char c; X x1 = {1,2.2,'c' }; s t r u c t X x2[3] = {{1,1.1, 'a'},{2,2.2, 'b'}}; s t r u c t s t r u c t struct Y { } ; float f; int i; Y(int A); // presumably assigned to i Y y2[] = {Y(1),Y(2),Y(3)}; s t r u c t c l a s s,

4 67 4.7 v a n i l l a, Y Y y4[2] = {Y(1)} Y Y y5[7] Y y; struct c l a s s class Z { int i; // private }; // no constructor Z z,z2[10]; C C + + C + + C C + + C s t r u c t

68 C + + C + + C C + + C C + + C + + C C + + C + C 4.8 C + + S t r o u s t r u p C + + C C + + 4.9 1) 3 HANDLE.H,HANDLE.CPP USEHANDL.CPP 2) 3) 4)

5 p r i n t _ i n t () p r i n t _ c h a r ( ) p r i n t _ f l o a t ( ), C + + C + + f ( h e l l o ), f ( h i, 1 ) f ( h o w d y, 2, c ) 5.1 2

70 C + + void f(); class x {void f();}; x f f x :: f () 2 _ f _ x _ f void print(char); void print(float); _ p r i n t _ p r i n t _ c h a r _ p r i n t _ f l o a t 5.1.1 void f(); int f(); int x = f(); C C + + 5.1.2 C C + +

5 71 //: DEF.CPP -- Function definition void f(int) {} //: USE.CPP -- Function misdeclaration void f(char); main() { //! f(1); //Causes a linker error } f ( i n t ), ( ) f ( c h a r ) C C + + f _ i n t f _ c h a r f _ c h a r f _ i n t C + + C 5.2 s t a s h s t a s h () Q u a n t i t y q u a n t i t y s t o r a g e

72 C + +

5 73 s t o r a g e, a d d () a d d ()

74 C + + s t a s h 5.3 s t a s h C + + s t a s h stash(int Size);// zero quantity stash(int Size,int Quantity); stash(int Size, int Quantity=0); s t a s h ( i n t ) s t a s h ( i n t, i n t stash A(100),B(100,0); A void fn(int x /* =0*/ ) { //... void f(int X, int = 0, float =1.1); C + + void f(int X, int,float f) {/*...*/} x f, p l a c e h o l d e r, f ( 1 ) f ( 1, 2, 3. 0 )

5 75 o n o ff

76 C + + b i t s () B i t Ve c t o r B i t Ve c t o r b i t s () b i t s ( i n t )

5 77 8 B i t Vector b(0) 8 B i t Vector char* 0 B i t Ve c t o r B i t Vector b((unsigned char * )0) B i t Vector b b. b i t s ( 64 ), 8 c h a r * unsigned char* B i t Vector(unsigned char*,int) B i t Ve c t o r ( c h a r *) p r i n t () c h a r * B i t Ve c t o r 0110010 s e t () c l e a r () r e a d (), p r i n t () B i t Ve c t o r 0 1 B i t Ve c t o r 8 B i t Ve c t o r p r i n t ( )

78 C + +

5 79 f o r f o i n i t [ i n d e x ]&( 0 x 80 > > o ff s e t ) 16 0 x 80 1 o ff s e t 1 s e t () B i t Vector p r i n t () 0 1 B i t Ve c t o r

80 C + + 8 n u m B y t e s 8 0 s e t () c l e a r () r e a d () a s s e r t ( S e t () r e a d () 1 s e t () 1 r e a d () c l e a r ( 1 ~ s e t () r e a d () c l e a r () c l e a r () bytes[bit/char_bit]&=~(1<<(bit % CHAR_BIT)) b i t s () r e a l l o c () b y t e s r e a l l o c () p r i n t () m s g C p u t s () r e a d () 8 B i t Ve c t o r p r i n t () B i t Ve c t o r B i t Ve c t o

5 81 b v 1 b v 2 b v 3 B i t Ve c t o r s e t () c l e a r () r e a d () p r i n t () b v 2 B i t Ve c t o r C + + b i t s b i t s t r i n g 5.4 B i t Ve c t o r b i t s () int bits(int sz=-1); - 1 b i t s - 1 int i=bv1.set(10); - 1

82 C + + 5.5 1) m e s s a g e c h a r * c h a r * p r i n t (); c h a r * 2) 3) S TA S H 4. H S TA S H 4. C P P s t a s h 4) f l a g s B i t Ve c t o r s e t () c l e a r () r e a d () i n d e x o ff s e t m a s k 5) F L A G S. C P P 100

6 C I / O I / O I / O C C + + C I/O 6.1 C C C c l o s e () C I / O r I / O fp( )

84 C + + fopen( ) exit( ) fp( ) f I / O fp( ) fp( ) f p () f f f p () I / O C C + +

6 85 p r i n t f ( ) S T D I O. H I / O v f p r i n t f () F i l e F i l F i l e F i l e f 0 f

86 C + + 0 F F F i l e [ 1 ] I / O / 1) p r i n t f ( " % c ", ' x ' ) ; 2) p r i n t f () 3) p r i n t f () C + + I / O 4) C + + p r i n t f () C p r i n t f () s c a n f () p r i n t f () C + + 6.2 C + + I / O h e l l o, w o r l d, I / O C + + I / O I / O C + + 6.2.1 C + + < < > > 5 [1] FULLWRAP test file

6 87 C + + C C + + 1) C C + + C + + C C + + C 2) C + + C ++ C C C + + 1 < < 4 1. 4 1 4 < < 1 ; 6.2.2 < < > > i s t r e a m o s t r e a m ( i f s t r e a m s ) ( o f s t r e a m s ) c h a r * i s t r s t r e a m s ( o s t r s t r e a m s ) C + + s t r i n g i s t r i n g s t r e a m s ( o s t r i n g s t r e a m s ) I / O c i n c i C s t d i n I O S T R E A M. H

88 C + + > > > < < c o u t ( c e r r ) p r i n t f () 1. e n d l c o u t < < f l u s h o c t ( ) d e c ( ) h e x ( ) c o u t < < h e x < < " 0 x " < < i < < e n d l c i n > > w s e n d s e n d l s t r s t r e a m s I O S T R E A M. H I O M A N I P. H 6.2.3 c i n > > c o u t < < s c a n f ()

6 89 b u f t h i s b u f b u c i n

90 C + + G U I I / O c i n 1) G U I 2) 3) c o u t c o u c o u t 6.2.4 g e t g e t l i n e \ n g e t g e t g e t g e t l i n e g e t l i n e 1. get g e t i n 10 2. r e a d w r i t e 3. g e t g e g e t l i n e g e t E O F g o o d e o f f a i l b a d e o f b i t f a i l b i t b a d b i t r e a d

6 91 w h i l e ( c i n ) i f ( c i n ) i f c i n e o f b i t f a i l b i t [ 1 c l e a r () ] 6.3 C S T D I O. H c l o s e o f s t r e a m F S T R E A M. I / O I O S T R E A M. H [1]

92 C + + i f s t r e a m o f s t r e a m a s s e r t () (, 11 ) w h i l e g e t () S Z - 1 \ n g e t g e t g e i n. g e t () g e t i n i g n o r e 1 i g n o r e () E O F c o u t o u t o s t r e a m g e t l i n e o u t c l o s e o p e n i n 12 w h i l e g e t l i n e \ n g e t l i n e () g e t () i o s :: i n i o s :: o u t i o s :: a p p i o s :: a t e i o s :: n o c r e a t e i o s :: n o r e p l a c e i o s :: t r u n c i o s :: b i n a r y i f s t r e a m i o s :: a p p i o s :: a t e i o s :: i n o f s t r e a m i o s :: t r u n c

6 93 O R 6.4 I / O s t r e a m b u f s t r e a m b u f I / O s t r e a m b u f s t r e a m b u f s t r e a m b u f s t r e a m b u f s t r e a m b u f s t r e a m b u f r d b u f () s t r e a m b u f s t r e a m b u f s t r e a m b u f < < < < i f s t r e a m a s s e r t ( i n ) c o u t < < i n. r d b u f ( ) c o u t streambuf get() g e t s t r e a m b u f s t r e a m b u

94 C + + 10 g e t r d b u f () g e t i g n o r e g e t () 6.5, s t r e a m p o s C f s e e k () s t r e a m p o s t e l l t e l l p t e l l g p g s t r e a m p o s s e e k p s e e k g s t r e a m p o s s e e k p s e e k g I o s : : b e g I o s : : c u r I o s : : e n d C C + + S T D I O. H C + + ( c i n c o u t )

6 95 i f s t r e a m a s s e r t s e e k g s t r e a m p o s l o n g t y p e d e f t e l l g 1 / 10 s t r e a m p o s s p 2 s e e k g r d b u f s t r e a m b u f s e e k g streampos sp2 / s t r e a m b u f i f s t r e a m i f s t r e a m o s t r e a

96 C + + i o f i l e. o u t c o u t < < i n 2. r d b u f () Where does this end up? s e e k p s e e k g o u t 2 6.6 strstreams s t r s t r e a m s t r s t r e a i s t r s t r e a m s t r s t r e a m o s t r s t r e a s t r s t r e a m s 6.6.1 i s t r s t r e a m s b u f [ s i z e ] i s t r s t r e a m [ 1 ] [1] D O S

6 97 C a t o f () a t o i () istrstream s("1.414 47 This is a test") s > > i > > f i f 1.414 47 This is a test i 1 f 0. 414 b u f 2 r d b u f s t r s t r e a m s o s t r s t r e a m : : o s t r s t r e a m ( c h a r *, i n t, i n t = i o s : : o u t ) i o s :: a t e i o s :: a p p o s t r s t r e a m s e n d s o s t r s t r e a m

98 C + + i n t f l o a t r d b u f ( c o n t r o l - Z ( u n i x c o n t r o l - D ) g e t l i n e b u f b u f ostrstream os i o s :: a p p b u f o s t r s t r e a m e n d s e n d l b u f c o u t < < b u f o s. r d b u f () s t r e a m b u f c o u t < < o s. r d b u f () 6.6.2 s t r s t r e a m s i s t r s t r e a m s o s t r s t r e a m ostrstream A A s t r s t r e a m s t r e a m b u f A s t r char* cp=a.str() A A A o s t r s t r e a m s t r c h a r *

6 99 s t r o s t r s t r e a m o s t r s t r e a m o s t r s t r e a s t r () c h a r * C + + n e w d e l e t e 12 C m a l l o c () f r e e () n e w d e l e t e C + + n e w o s t r s t r e a m n e w d e l e t e ostrstream A s t r () c h a r * delete A.str() o s t r s t r e a m f r e e z e () f r e e z e () o s t r s t r e a m s t r e a m b u f f r e e z e A. r d b u f ( ) - > f r e e z e ( 0 ) A A s t r s e n d s s t r () c h a r * s s e n d s s e e k p r d b u f () f r e e z e ( 0 ) s t r e a m b u f s

100 C + + s s t r ( ) o s t r s t r e a m o s t r s t r e a m c h a r * o s t r s t r e a m r d b u f () o s t r s t r e a m 1. s t r () o s t r s t r e a m s t r s s t r c h a r * c h a r * d e l e t e s t r () c h a r * v o i d * c h a r * < < v o i d * < < s t r () s 0 o s t r s t r e a m 2. C++ string s t r i n g s t r e a m [ 1 c h a r * s t r s t r e a m ] 6.7 p r i n t f () [1]

6 101 6.7.1 i o s I O S T R E A M. H i o s :: f l a g s () long(typedefed to fmtflags) s e t f ( ) o n o ff o n / o ff s e t f ( f m t f l a g s ) o n u n s e t f ( f m t f l a g s ) o ff o n / o ff i o s : : s k i p w s i o s : : s h o w b a s e i o s : : s h o w p o i n t i o s : : u p p e r c a s e i o s : : s h o w p o s i o s : : u n i t b u f i o s : : s t d i o C + + A - F E + C I / O c o u t c o u t. s e t f ( i o s :: s h o w p o s ) c o u t. u n s e t f ( i o s : : s h o w p o s ) C I / O C i o s :: s t d i o p r i n t f () 1. s e t f i o s :: b a s e f i e l d i o s d e c s e t f i o s :: h e x i o s :: h e x i o s :: d e c s e t f () s e t f ( i o s :: h e x, i o s :: b a s e f i e l d ) i o s :: b a s e f i e l d i o s :: h e x s e t f () h e x ()

102 C + + s e t ( ) i s o : : b a s e f i e l d i o s : : d e c i o s : : h e x i o s : : o c t i o s :: f l o a t f i e l d i o s :: s c i e n t i f i c i o s :: f i x e d a u t o m a t i c (Neither bit is set) i o s : : a d j u s t f i e l d i o s : : l e f t i o s : : r i g h t i o s : : i n t e r n a l 2. f u n c t i o n int ios::width() int ios::width(int n) int ios::fill() int ios::fill(int n) int ios::precision() int ios::precision(int n) 0 6 i o s :: floatfield 0 123 2 123 0 w i d t h () 6.7.2

6 103

104 C + + D a s t r i n g i z i n g a D a a T

6 105

106 C + + 6.8 C + + I O S T R E A M. H d e c o c t h e x s e t f ( i o s :: d e c, i o s :: b a s e f i e l d ) s e t f ( i o s :: o c t, i o s :: b a s e f i e l d ) s e t f ( i o s :: h e x, i o s :: b a s e f i e l d ) IOSTREAM.H [ 1 ] w s e n d l e n d s f l u s h s h o w b a s e n o s h o w b a s e s h o w p o s n o s h o w p o s u p p e r c a s e n o u p p e r c a s e s h o w p o i n t n o s h o w p o i n t s k i p w s n o s h i p w s l e f t r i g h t i n t e r n a l s c i e n t i f i c f i x e d C + + + A - F E s e t p r e c i s i o n ( ) i o s : : p r e c i s i o n ( ) I O M A N I P. H setiosflags(fmtflags n) resetiosflags(fmtflags n) setbase(base n) setfill(char n) setprecision(int n) setw(int n) n i o s :: s e t f () n i o s :: u n s e t f () n n 1 0 8 1 6 0 n 0 10 C 10 1 0 0 10 8 0 x f 1 5 d e c o c t h e x n i o s f i l l n i o s p r e c i s i o n n i o s w i d t h [1]

6 107

108 C + + s e t i o s f l a g s ( ) r e s e t i o s f l a g s ( ) O R s e t f () u n s e t f () 6.9 e n d l o s t r e a m 10 e n d l ostream& endl(ostream&) cout<<"howdy" <<endl e n d l I O S T R E A M. H o s t r e a m n l

6 109 o s < < ' \ n ' [ 1 o s n l ] n l e n d l Jerry Schwarz [2] < < < < 11 [1] n 1 8 [2]

110 C + + f i x w c h a r * < < f i x w o s t r e a m o s t r e a m f i x w cout <<fixw(string,i)<<endl f i x w < < bin U L O N G _ M A X unsigned long L I M I T S. H c h a r * f i x w unsign long b i n f i x w b i 9

6 111 6.10 u n i x s e d a w k s e d a w k 6.10.1 m a i n i o s :: n o r e p l a c e i s t r s t r e a m C t o u p p e r () i n t c h a r 1.

112 C + + N A M E. H N A M E. C P P

6 113

114 C + +

6 115 o s t r s t r e a m enum bufs o s t r s t r e a m c h a r b c h a r b u f n u m b u f n u m b u f s e n u m b u f n u m b u f b S Z b a s e h e a d e r i m p l e m e n t C P P H l i n e 1 guard1 g u a r d 2 g u a r d 3 C P P l i n e l C P P i n c l u d e C P P o s a r r a y o s t r s t r e a m o s t r s t r e a m b u f s b o s a r r a y b o s t r s t r e a m C P P i f s t r e a m s s t r s t r e a m // C s t r s t r () o s t r s t r e a m s t r s t r () s t r s t r e a m o s t r s t r e a o f s t r e a m o s t r s t r e a m 2. //

116 C + +

6 117 m a k e r m a k e r strstream edited e d i t e d 6.10.2

118 C + + p r i n t f ( ) d a t a p o i n t o s t r e a m p r i n t f ()

6 119 p r i n t f () s e t f () p r e c i s i o n ( 4 s e t w () 2 s e t w () 10 0 * ` ` ` 1. w r i t e (),

120 C + + d a t a p o i n t : : p r i n t ( ) A S C D ATA. T X T A S C D ATA. B I N i o s :: b i n a r y C t i m e (), t i m e _ t 1970 1 1 0 0 0 0 00 GMT C s c r a n d () t m

6 121 t m C l o c a l t i m e () t m t m l o c a l t i m e () l o c a l t i m e () l o c a l t i m e () d a t a p o i n t tm struct l o c a l t i m e () d. Ti m e = * l o c a l t i m e ( & t i m e r ) t i m e r 55 C r a n d () R A N D _ M A X % r a n d () D ATA. B I N R A M w r i t e () unsigned char* d a t a p o i n t d a t a p o i n 2. D ATA 2. T X T D ATA. T X T

122 C + + ifstream bindata D ATA. B I N

6 123 i o s : : n o c r e a t e a s s e r t () r e a d ( datapoint d d a t a p o i n t r e a d () b i n d a t a f a i l b i t w h i l e c l e a r () f a i l b i t r e c n u m 4 s e t w () r e a d () e l s e f a i l b i t c l e a r () r e a d () f l a t - f i l e D B M S A p p l e B A S I C D B M S C + + 6.11 6.12 1) in i f s t r e a m o s o s t r s t r e a r d b u f () o s t r s t r e a m s t r () o s c h a r * C toupper() o s 2) 3) 1 4) g r e p

7 c o n s t C + + C c o n s t v o l a t i l e, c o n s t c o n s t # d e f i n e s 7.1 C C + + c o n s t C #define BUFSIZE 100 B U F S I Z E B U F S I Z E C + + c o n s t const bufsize=100 const int bufsize=100 b u f s i z e char buf[bufsize] c h a r i n t f l o a t d o u b l e c o n s t c o n s # d e f i n e 7.1.1 const # d e f i n e c o n s t c o n s t

7 125 c o n s t C + + c o n s c o n s t c o n s t c o n s t e x t e r n extern const bufsize e x t e r n c o n s t C + + c o n s t c o n s t c o n s t c o n s t c o n s t c p p c o n s t 7.1.2 const c o n s t # d e f i n e s c o n s t i j i j j j j b u f j m a i n () c c o n s t C c 2 c # d e f i n e

126 C + + c o n s t 7.1.3 c o n s t c o n s t 7.1.4 C C + + C C c o n s t C C c o n s t C const bufsize=100 char buf[bufsize] b u f s i z e C const bufsize C + + C C c o n s t C + + c o n s t C + + C e x t e r n extern const bufsize;//declaration only C C c o n s t c o n s t C # d e f i n e

7 127 7.2 c o n s t c o n s t c o n s t c o n s t c o n s 7.2.1 const c o n s t const int* x x const int x c o n s t x c o n s t int const* x x i n t c o n s t x c o n s t i n t c o n s t i n t 7.2.2 const c o n s t c o n s t * int d=1 int* const x=&d x i n t c o n s t c o n s t * x = 2 c o n s t c o n s int d=1 const int* const x=&d // (1) int const* const x2=&d // (2) c o n s t * int* u=&w

128 C + + i n t * * int* u=&w v = 0 int* u int v 7.2.3 C + + c o n s t c o n s t c o n s t c o n s t c o n s c o n s t c o n s t C + + c o n s t c o n s t char* cp="howdy" h o w d y 7.3 c o n s t c o n s t c o n s t 7.3.1 const c o n s t

7 129 x ( ) c o n s t c o n s t c o n s 10 & c o n s t 7.3.2 const const int g() c o n s t c o n s t

130 C + + f 5 ( ) const X f 6 () const X c o n s t c o n s t c o n s t f 7 () c o n s t C + + 10 c o n s t

7 131 7.3.3 c o n s t c o n s t c o n s t c o n s t c o n s t t () c o n s t u () c o n s t

132 C + + u () c o n s t c o n s t c o n s t c o n s v () w () v () const char* v () w () v ( w () m a i n () t ( c o n s t c o n s t t () u () c o n s t c o n s t c o n s t v () c o n s t w () c o n s t const int* const int* const int*const c o n s t C C + + C + + c o n s t c o n s t C c o n s t

7 133 f () X f () g 1 () g 2 () g 1 g 1 () c o n s t g 2 () 7.4 c o n s t c o n s t c o n s t c o n s t c o n s t 7.4.1 const enum c o n s t # d e f i n e s i z e s i z e # d e f i n e # d e f i n c o n s t c o n s t C c o n s t c o n s t c o n s t c o n s t c o n s t c o n s t 1. c o n s t c o n s

134 C + + 2. c o n s t float pi (3.14159) i n t e g e r i n t e g e r integer I[100] f o r m e m s e t () 7.4.2 c o n s t c o n s t C c o n s t c o n s t e n u m

7 135 e n u m enum { one=1,two=2,three} e n u m t h r e e 3 e n u m

136 C + + p u s h () const char* p o p () const char* s t a c k const char* s t r i n g s t a c k i c e C r e a m S t r i n g s t a c k e n u m C + + static const 9 C C + + C + + C a c o l o r C a + + C + + C + + c o l o r i n t 1 i n t c o l o r C + + c o l o r i n t b l u e 1 c o l o r 1 e n u m e n u 7.4.3 const c o n s t c o n s t c o n s t const int i=1 const blob B(2)

7 137 B b l o b c o n s t 2 c o n s t c o n s t c o n s t c o n s c o n s t c o n s t c o n s t c o n s t c o n s t c o n s t c o n s t c o n s t c o n s t c o n s t c o n s c o n s t int X::f() const {return i } f () i c o n s t c o n s t c o n s c o n s t c o n s t

138 C + + c o n s t q u o t e () c o n s t l a s t q u o t e L a s t q u o t e () c o n s t c o n s c q c o n s t c o n s t c o n s t c o n s t c o n s t c o n s t c o n s t c o n s t c o n s t t h i s t h i s c o n s t

7 139 t h i s c o n s t m u t a b l e c o n s t 7.4.4 c o n s t c o n s t R O M

140 C + + c o n s t c o n s t c o n s m u t a b l e c o n s t c o n s t 1) class s t r u c t 2), c o n s t R O M R O 7.5 volatile v o l a t i l e c o n s t v o l a t i l e v o l a t i l e v o l a t i l e c o n s t v o l a t i l e const volatile

7 141 c o n s t v o l a t i l e v o l a t i l e v o l a t i l e i s r () t h i s I S R i s r ( ) v o l a t i l e c o n s t c - v 7.6 c o n s t const correctness c o n s t c o n s t C 1 0 11 c o n s t 7.7 1. f l y () b i r d f l y () r o c k r o c k v o i d * v o i d * b i r d f l y () C v o i d * C 2. c o n s t c o n s 3. c o n s t c o n s t c o n s c o n s t 4. 5. C C + + c o n s t c o n s C C + +

8 C + + C C + + C C ( m a c r o ) C A L L R E T U R N C + + C C + + ( p r i v a t e ) C + + (inline function) C + + C + + 8.1 #define f (x) (x+1) f f (1) (x) (x+1) (1) f f ( 1 ) (1 + 1) #define floor(x,b) x>=b?0:1

8 143 if(floor(a&0x0f,0x07)) //... if(a&0x0f>=0x07?0:1) & > = #define floor(x,b) ((x)>=(b)?0:1) > = + / - - #define band(x) (((x)>5 && (x)<10)? (x) : 0)

144 C + + a 4 a 5 S T D I O. H putc( ) C toupper( toupper(*p++ ) [1] C C + + class X { int i; p u b l i c : #define val (X::i) //Error p u b l i c 8.2 C + + C + + [1] Andraw Koenig C A d d i s i o n - We s l e y, 1989

8 145 i n l i n e inline int PlusOne(int x); inline int PlusOne(int x) { return ++x ;} + C A L L static, 8.2.1 i n l i n e

146 C + + 8.2.2 ( p r i v a t e ) ( p r i v a t e ) read( ) read( t h i s C A L L i ( p u b l i c ), i i i i n t f l o a t int int i ( a c c e s s o r s ) ( m u t a t o r s )

8 147 7 13 Wi d t h H e i g h t C Ti m e

148 C + +

8 149 C Ti m e time_t T tm local A S C I I A s c i i t i m e _ t updatelocal( ) updateascii( ) mark( ) A S C I I ascii( ) updateascii( ) ascii( ) updateascaii( C DaylightSaving( ) updatelocal( ) updatelocal( ) Ti m e 2 Ti m

150 C + + 8.3 8.3.1 ( t h i s ) 8.3.2

8 151 g( ) f( g( ) g( ) f( ) f( ) g( ) 8.3.3

152 C + + w i t h M e m b e r s Q P S 8.4 Dan Saks in situ ( ) i n l i n e. 2. 2 R E C TA N G L. C P P

8 153 i n l i n e In situ in situ 8.5 C C + + 3 # #define DEBUG(X) cout<<#x " = " << X << endl #define TRACE(S) cout << #S << endl; S # S 2 S f o r for (int i = 0 ; i < 100 ; i++ ) TRACE(f(i)) ; TRACE( ) f o r FIELD( )

154 C + + 8.6 assert( ) 17 _ F I L E _ _ L I N E _ assert( ) assert( ) assert( ) assert( ) #define NDEBUG A l l e g e a s s e r t allege_error( ) f a l s e

8 155 fprintf( ) i o s t r e a m s e x i t ( 1 ) allege( ) i f - t h e n - e l s e e x p r allege_error( ) assert( ) assert( ) assert( ) allegefile( ) allegemen( ) allege( ) A L L E G E. H //#define NDEBUG // turn off asserts allege( ) assert( ) assert( ) 8.7 8.8 1. 7 2 Print( ) 2. 3 N E S T F R N D. C P P

156 C + + in situ initialize( ) 3. 6 N L. C P P n l 4. A B, A B B B 5. 4 Ti m e

9 C + + s t a t i c C C + + s t a t i c s t a t i c C + + C 9.1 C C C + + s t a t i c 1) 2) C + + s t a t i c s t a t i c C 9.1.1 C C + + s t a t i c

158 C + + static char* s o n e c h a r ( ) o n e c h a r () s o n e c h a r () 0 s t r i n g s o n e c h a r () s s static char* s=0 o n e c h a r () s i f (! s ) s 1.

9 159 f () X 2. main() C e x i t () m a i n ( e x i t () e x i t ( C a b o r t () C a t e x i t () m a i n ( e x i t () m a i n () e x i t () a t e x i t () m a i n ()

160 C + + o b j c A o b j m a i n () f o b j B g () C m a i n () f () A m a i n () B f () m a i n () g () B C g () f () o f s t r e a m o u t e x t e r n o u t C + + m a i n () m a i n () m a i n ( C 9.1.2 s t a t i c. C P P inline function C + + C /

9 161 s t a t i c int a=0; a m a i n () a s t a t i c e x t e r n extern int a=0; : static int a=0; a a s t a t i c e x t e r n s t a t i c e x t e r n s t a t i c e x t e r n extern void f() void f() static void f() f () 9.1.3 s t a t i c e x t e r n a u t o, a u t o r e g i s t e r r e g i s t e r r e g i s t e r 9.2 s t a t i c t y p e d e f C + + c l a s s, s t r u c t, e n u m u n i o n,

162 C + + n a m e s p a c e 9.2.1 n a m e s p a c e c l a s s s t r u c t u n i o n e n u m 1) namespace 2) n a m e s p a c e 3) n a m e s p a c e 4) n a m e s p a c e 5) 1. n a m e s p a c e

9 163 2. friend s t a t i c y o u () m e 9.2.2 u s i n g 1.

164 C + + 2. using using u s i n g n a m e s p a c e using using m a t h u s i n g using u s i n g u s i n g using

9 165 u s i n g u s i n g u s i n g 3. using u s i n g u s i n u s i n g u s i n g u s i n g u s i n g u s i n u s i n g

166 C + + u s i n g using u s i n g 9.3 C++ C p u b l i c p r i v a t e p r o t e c t e d 9.3.1 int A::i=1;

9 167 int i=1; i A :: i = w i t h S t a t i c :: w i t h S t a t i c 1.

168 C + + 2. 7 enum hack e n u m 9.3.2

9 169 9.3.3 t h i s t h i s t h i s s t a t i c

170 C + + t h i s t h i s m a i n () e g g e g g

9 171 E 9.4 out o o f o u t o u t m a i n () ARM [1] [1] The Annotated C++ Reference Manual,Bjarne Stroustrup M a rgaret Ellis 1990 p p. 20-21

172 C + + fstream out y x 1 y 2 x, y 1 x 2 1) 2) 3) Jerry Schwarz i o s t r e a m c i n, c o u t c e r r

9 173 x y initializer init s t a t i c x y i n i t _ c o u n t i n i t D E P E N D. H i n i t _ c o u n t ~ i n i t i a l i z e r ()

174 C + + i n i t i a l i z e r i n i t i a l i z e r () C + + n e w d e l e t e 12 9.5 C + + C float f(int a,char b); C + + _ f _ i n t _ i n t C _ f C + + f C + + e x t e r n e x t e r n extern "C" float f(int a,char b); f () C C + + C + + C C + C + + C C + + 9.6 s t a t i c C + + s t a t i c 9.7 1.

9 175 const int static int i n l i n e i n l i n p r i n t () 2. STAT D E S T. C P P m a i n () f () g () 3. S TAT D E S T. C P P o u t e x t e r n A o b j o u t 4. e x i t () 5. 7 V O L AT I L E. C P P c o m m :: i s r ()

1 0 C + + P a s c a l C + + A l g o l C + + 11 C C + + C + + ( c o p y - c o n s t r u c t o r ) ( & ) C + + ( p o i n t e r- t o - m e m b e r ) 10.1 C++ C C + + C + + v o i d * C v o i d * bird* b; rock* r; void* v; v = r; b = v; C + + 18 C + + 10.2 C++ (&) int x; int & r = x; int & q = 12; 12 int x=0; int & a = x; a + + ; a x

10 177 1) 2) 10.2.1 3) N U L L f( ) g ( ) 1. R E F R N C E. C P P g ( )

178 C + + f ( 1 ) i n t 1 2. C void f (int**); int I = 47; int* ip = &I; f (&ip); C + + 10.2.2

10 179 10.3 C + + X ( X &) X X 10.3.1 C int f (int x, char c); int g = f (a,b); c h a r i n t f l o a t d o u b l e f ( ) push b push a call f ( ) add sp,4 mov g, register a b a _ b _ a g f( ) n a m e - m a n g l i n g a C P U C C + + add sp,4 f( ) 1.

180 C + + h e l p e r m a i n () b i g f u n ( B B h e l p e r PA S S T R U C. C P P B 2 2. C A L L C P U R E T U R N C A L L (function frame) 3. C C + + I S R I S

10 181 I S R v o i d C P U I S R R E T U R N R E T U R N I S R C C + + C + PA S S T R U C. C C P main( ) bigfun( ) B 2 bigfun( ) 4. b i t c o p y C C + + 9 ( s t a t i c )

182 C + + h o w m a n y i n t print( ) h 1 f( ) 2 h 2 0 f( ) h C C++ howmany f( ) o b j e c t _ c o u n t o b j e c t _ c o u n t 0 h 2 h h 2 o b j e c t _ c o u n t 10.3.2

10 183 howmany h2 = f(h); h 2 f( ) b i t c o p y h o w m a n y ( b i t c o p y ) X ( X &) X ( b i t c o p y ) HO W M A N Y. C P P

184 C + + i d C strncpy( ) i d strncpy( ) h o w m a n y 2 ( h o w m a n y 2 &) i d i d c o p y C strncat( ) i d print( ) print( i d s t a t i c main( ) f( ) C

10 185 i o s t r e a m s s t r s t r e a m i f s t r e a m ofstream, ofstream getline( ) s t r s t r e a m 2 2 L I N E N U M. C P P H O W M A N Y 2. O U T

186 C + + h 1 f( ) f( h h 2 8 f( ) h h 2 h f( ) 9 h 2 h f( ) 3 h 13 f( ) 2 h h2 h 2 h 15 f ( h ) 16 21 t h i s f( )

10 187 25 2 6 10.3.3 2 3 2 4 2 8-31 h 2 h 0 C b i t c o p y C + + c o m p o s i t i o n

188 C + + w i t h C C c o m p o s i t e w i t h C C w i t h C C w i t h C C w o C C print( ) c o m p o s i t e 7 13 c o m p o s i t e w i t h C C w o C C W O C C c o m p o s i t e main( ) composite c2 = c ; c o m p o s i t e 13 w i t h C C w o C C b i t c o p y c o m p o s i t e m a i n composite::print( ) c 2. W O C C c. W O C C memberwise initialization 10.3.4

10 189 1. p r i v a t e f r i e n d nocc(const nocc&) c o n s t 2. i o s t r e a m s get( ) c h a r & get( ) char c; c i n. g e t ( c ) ; c o n s t c o n s t

190 C + + 10.4 C + + ( p o i n t e r- t o - m e m b e r ) C + + struct simple { int a ; }; s p s o sp->a ; so.a ; i n t e g e r i p i p * *ip = 4 ; * * s i m p l e sp->*pm = 47 ; so.*pm = 47 ; - > *. p m * int simple::*pm ; int simple::*pm = &simple::a ; s i m p l e : : a & s i m p l e :: a p o i n t e r- t o - m e m b e r int (*fp)(float) ; (* f p ) i n t * struct simple2 { int f(float); } ;

10 191 int (simple2::*fp) (float) ; int (simple2::*fp) (float) = &simple2::f ; & fp = simple2::f ; ( p u b l i c ) t y p e d e f [ 1 ] [1] Owen Mortensen

192 C + + main( ) Count( ) fptr[1] = &g ; g (this->*fptr[i]) (j); t h i s 10.5 C + + C C + + c a s t C + + A l g o l P a s c a l ( r e f e r e n c e )

10 193 ( p r i v a t e ) 10.6 1. c h a r & main( ) c h a r 2. c o u t 3., PA S S T R U C. C P P

11 C + + o p e r a t o r 11.1 C C 1 << 4; 1.414 << 2; 11.2 o p e r a t o r @ @ 1) 2)

11 195 K += I + J ; + + = r v 11.3 C C ** 11.3.1 i n t e g e r b y t e

196 C + +

11 197

198 C + +

11 199 + + - - + + a o p e r a t o r + + ( a ); a + + o p e r a t o r + + ( a, i n t ) U N A RY. C P P + + b B::perator++( ) b + + B :: o p e r a t o r + + ( i n t ) i n t 11.3.2 U N A RY. C P P

200 C + +

11 201

202 C + +

11 203

204 C + +

11 205

206 C + +

11 207

208 C + +

11 209 = ( s e l f - a s s i g n m e n t ) + = A + = A =

210 C + + = 11.3.3 U N A RY. C P P B I N A RY. C P P 1) c o n s t + - c o n s t c o n s t ( o p e r a t o r- a s s i g n m e n t + = = ( c o n s t a n t ) 2) i n t e g e r :: o p e r a t o r + i n t e g e r c o n s t 3) A = B = C c o n s t n o n c o n s t A = B = C n o n c o n s t A = B. f o o B A foo( ) n o n c o n s t 4) i n t b o o l C + + b o o l i n t t y p e d e f 5) c o n s t * t h i s int b o o l ( i t e r a t o r ) c o n s t n o n c o n s t (++A).foo( ) foo( ) A (A++).foo( ) foo( ) + + c o n s t c o n s t 1. c o n s t c o n s t + f ( A + B ) A + B f( ) c o n s t c o n s t

11 211 A + B (A+B).g( ) g( ) i n t e g e r c o n s t c o n s t c o n s t 2. + return integer (left.i + right.i) ; t m p t m t m p integer tmp(left.i + right.i) ; return tmp ; 11.3.4 [ ],

212 C + + ( ) n e w d e l e t e - > * ( s m a r t ) - >, i t e r a t o r

11 213

214 C + + o b j f( ) g( ) o b j _ c o n t a i n e r add( ) o b j _ c o n t a i n e r o b j _ c o n t a i n e r s p f r i e n d s p + + -- ( i t e r a t o r ) 15 main( ) O C o b j S P SP->f( ) ; //Smart pointer calls SP->g( ) ; S P f( ) g( ) o b j * obj* s p :: o p e r a t o r- > --- 11.3.5. - >.*. F o t r a n ** C C + + 11.4

11 215 i o s t r e a m s << > >

216 C + + I[4] = -1 ; [ ] os << ia.i[j] ; I O S T R E A M. H ostream& operator<<(ostream&,int) i a. i [ j ] i n t i s t r e a m o s t r e a m Murry [1] = ( ) [] -> += -= /= *= ^= &= = %= >>= <<= 11.5 C + + = = foo B ; foo A = B ; A = B ; 2 A C + + A f o o = [1] Rob Murray, C++ Strategies & Tactics, Addision-We s l e y, 1993, page 47.

11 217 3 = A f o o :: o p e r a t o r = f o o :: o p e r a t o r = o p e r a t o r = = = = = = fee fum(fi) ; = B I N A RY. C P P = = int operator=(int,foo) ; // global = not allowed! = =

218 C + + = ( s e l f - a s s i g n m e n t ) 1.

11 219 4 = = (reference counting) 0 ( c o p y - o n - w r i t e ) 1

220 C + +

11 221 m e m b l o c k b l o c k m e m b l o c k attach( ) m e m b l o c k detach( ) 0 delete this set( ) m e m b l o c k connted::unalias( ) connted::unalias( ) m e m b l o c k :: u n a l i a s ( ) 1 b l o c k 1 C + + n e w d e l e t e C malloc( ) free( ) n e w d e l e t e new d e l e t e malloc( ) free( ) b l o c k b l o c k memblock::attach( ) = = m e m b l o c k detach( ) m e m b l o c k

222 C + + = detach( ) m e m b l o c k write( ) unalias( ) main( ) = main( ) C write( ) C A 2. R E F C O U N T. C P P

11 223

224 C + +

11 225 m e m b l o c k s t a t i c b l o c k c o u n t b l o c k n u m print( ) c o u n t e d i d c o u n t e d m e m b l o c k b l o c k m e m b l o c c o p y addname( ) i d 3. = t y p e : : o p e r a t o r = ( t y p e ) = (memberwise assignment)

226 C + + f o o = b a r :: o p e r a t o r = = p r i v a t e 11.6 C C + + C + + 11.6.1

11 227 f( ) o n e f( ) t w o one t w o t w o :: t w o ( o n e ) t w o :: t w o ( o n e ) t w o f( f( ) t w o f( ) explicit [1] t w o t w o f ( t w o ( O n e )) O n e t w o 11.6.2 o p e r a t o r [1] e x p l i c i t

228 C + +

11 229 n u m b e r + ( f r i e n d ) - i n t i n t n u m b e r main( ) n u m b e r n u m b e r n u m b e r + i n t n u m b e r :: o p e r a t o r + i n t n u m b e r i n t + n u m b e r n u m b e r :: o p e r a t o r + n u m b e r - n u m b e r 1 - a n u m b e r 1-1 n u m b e r - C 1-1 11.6.3 strings s t r i n g C

230 C + + strcmp( ) S T R I N G. H S T R I N G. H s t r i n g c h a r * c h a r * s t r i n g 11.6.4

11 231 X operator Y( ) Y X X Y ( f a n - o u t )

232 C + + C A B h( ) main( ) make_a( ) make_b( ) 11. 5 F E E F I. C P P f o fee fiddle f o f e e f e e f e e = fee fiddle = FO ; 11.7

11 233 11.8 1. + + 2. private char i o s t r e a m < < > > I O S O P. C P P f s t r e a m s s t r s t r e a m s s t d i o s t r e a m s ( c i n c o u t ) 3. + - * / n u m b e r int( ) 4. U N A RY. C P P B I N A RY. C P P 5. FA N O U T. C P P

1 2 C A D C malloc( ) free( ) malloc( ) C + + 1) C + + 2) 3) C + + malloc( ) free( ) C + + n e w d e l e t e 12.1 C + + 1) 2) 2 C + + 1 1) 2) 3)

12 235 12.1.1 C C malloc( ) calloc( ) realloc( ) free( C malloc( )

236 C + + obj* Obj = (obj*)malloc(sizeof(obj)) ; malloc( ) v o i d * C + + void* malloc( ) 0 Obj->initialize( ) ; C C + + C 12.1.2 new C + + n e w n e w n e w foo *fp = new foo(1,2) ; m a l l o c ( s i z e o f ( f o o )) 1 2 f o o t h i s f p f o o n e w n e w n e w foo *fp = new foo ;, 12.1.3 delete n e w d e l e t e d e l e t e free( ) n e w d e l e t e delete fp ; f o o d e l e t e n e w malloc( ) calloc( ) realloc( ) d e l e t e n e w d e l e t

12 237 malloc( ) free( ) 0 0 12.1.4 t r e e < < o s t r e a m f r i e n d o s t r e a m & o s t r e a m 12.1.5 calloc( ) realloc( ) malloc( ) 1) malloc( ) 2) m a l l o c ( ) 3) malloc( ) free( )

238 C + + malloc( ) 12.2 n e w d e l e t e s t a s h s t a c k 12.2.1 string s t a s h s t a c k s t a s h s t a c d e l e t e v o i d d e l e t e v o i d s t a s h s t a c k 13 15 d e l e t e S t r i n g S t r i n g

12 239 p r i v a t e p r i v a t e = S t r i n g S t r i n g f r i e n d makestring( ) s t a t i c make( ) String::make( ) s = new char[strlen(s) +1] ; c h a r c h a r * c h a r S t r i n g i o s t r e a m i o s t r e a m St r i n g 12.2.2 stash 5 s t a s h 5 p s t a s h 5 s t a s s t a s h n e w d e l e t e pointer stash

240 C + + s t o r a g e v o i d n e w malloc( ) v o i d * v o i d storage = new void*[quantity = Quantity] ; v o i d v o i d [] fetch( ) v o i d

12 241 add( ) inflate( ) realloc( ) n e w r e a l l o c malloc( ) realloc( ) free( ) n e w d e l e t e malloc( ) new( ) delete( ) p s t a s h s t a s h

242 C + + intstash.add( new int(i) ) ; s t a s h n e w new int(i) i n t i n t i p s t a s h :: o p e r a t o r [] p s t a s h v o i d 2 p s a t s h S t r i n g makestring( ) String::make( ) ( s t a t i c char* p = *(String*)stringStash[i] ; [] S t r i n g * S t r i n g c h a r * S t r i n g S t r i n g c h a r * 12.2.3 stack s t a c k 4

12 243 struct link stack::push( ) n e w l i n k :: l i n k d a t a n e x t stack::push( ) l i n k l i n k head = new link(data, head) ; 4 d e l e t e free( ) s t a s h v o i d s t a c k

244 C + + s t a c k s t a s h S t r i n g s t a c k s t a c k s t a c k 12.3 new delete C + + 4 n e w foo* fp = new foo[100] ; 100 f o o f o o * foo* fp2 = new foo ; f p f p [ 2 ] delete fp2 ; //O K delete fp ; // Not the desired eff e c t f o o f p 2 f p 99

12 245 delete []fp ; delete [100]fp ; f p int const* q = new int[10] ; const int* q = new int[10] ; c o n s t int int* const q = new int[10] ; q q + + 12.4 n e w n e w - h a n d l e r 0 n e w - h a n d l e r (throw an exception) 17 n e w - h a n d l e r N E W. H set_new_handler( ) n e w - h a n d l e r

246 C + + new-handler v o i d w h i l e i n t n e w n e w - h a n d l e r n e w - h a n d l e r 12.5 new delete n e w n e d e l e t e d e l e t n e w d e l e t e n e w d e l e t e C + + n e w d e l e t e n e w d e l e t e C + + n e w d e l e t e n e w n e w n e w d e l e t e n e w n e 0 n e w - h a n d l e r b a d _ a l l o c 17 n e w d e l e t e 12.5.1 new delete n e w d e l e t e

12 247 n e w s i z e _ t 0 0 n e w - h a n d l e r n e w v o i d * d e l e t e n e w v o i d * v o i d * d e l e t e v o i d n e w d e l e t e

248 C + + n e w d e l e t e C malloc( ) free( ) n e w d e l e t e printf( ) puts( ) i o s t r e a m s i o s t r e a m c i n c o u t c e r r n e w printf( ) n e w main( ) n e w d e l e t e s n e w d e l e t e 12.5.2 new delete n e w d e l e t e s t a t i c s t a t i c n e w n e w n e w n e w d e l e t e n e w d e l e t e f r a m i s f r a m i s ( b y t e s ) ( b y t e )

12 249 psize f r a m i s f r a m i s p s i z e 0 0 n e w n e w 0 1 0 n e w - h a n d l e r n e w d e l e t e i o s t r e a m s d e l e t e f r a m i s f r a m i s n e w n e d e l e t e

250 C + + d e l e t e 0 main( ) f r a m i s f r a m i s n e w d e l e t e 12.5.3 new delete n e w d e l e t e new( ) delete( ) n e w [] d e l e t e []

12 251 n e w d e l e t e n e w d e l e t e n e w d e l e t n e w d e l e t e n e w i n t 2 20 n e w * d e l e t e d e l e t e w i d g e t n e w 4 4 delete []widget ; n e w d e l e t e 12.5.4 foo* f = new foo ; n e w f o o f o

252 C + + n e w 0 0 n e w n e 0 12.5.5 n e w 1) 2) n e w n e w n e w n e w s i z e _ t X* xp = new(a) X ;

12 253 a n e w n e w n e w n e w d e l e t e xp->x::~x( ) ; //explicit destructor call new 12.6

254 C + + C C + + n e w d e l e t e C + + n e w d e l e t e 17 12.7 1. c o u t n e w d e l e t e n e w d e l e t e 2. p s t a s h 1 n e w 3. n e w d e l e t e 4. F R A M I S. C P P n e w d e l e t e n e w d e l e t e

1 3 C + + C C + + 13.1 X

256 C + + p r i v a t e p u b l i c p e r m u t e () X X Y 13.2 Y Y X

13 257 Y X Y X X m a i n () s i z e o f ( Y s i z e o f ( X ) p u b l i c p r i v a t e p u b l i c p r i v a t e p u b l i c p u b l i c p u b l i c h a n g e () p e r m u t e () p u b l i c s e t () s e t () Y r e a d () p e r m u t e () m a i n ( Y s e t () c h a n g e () s e t ()

258 C + + 13.3 C + + p r i v a t e C + + b a r f o o bar i n t foo::foo(int i) : bar(i) { //... 13.3.1 foo2:foo2(int I) : bar(i), memb(i+1) { //... foo2 bar memb 13.3.2 C + +

13 259 int i(100); 13.4

260 C + + C B A C::f() B :: f () a. f ( C::f() C f() a. f () B :: f ( 13.4.1

13 261 ofstream 18 13.4.2

262 C + + bart d o h () bart, milhouse 13.4.3 operator= 11 operator=

13 263 operator= operator other() root other derived root f() other main() root derived operator=( ) root other root 13.5

264 C + + private p u b l i c Car

13 265 i s - a has-a 13.5.1 ifstream ifstream strstream fname1 ifstream & ifstream fname1 main

266 C + + c o u t < < f i l e. r d b u f ( ) < < e n d l ; filebuf * rdbuf() {return File.rdbuf();} fname1 rdbuf() ifsream

13 267 ofstream fname2 fname2 o f s t r e a m 14 13.5.2 stack stack S T R I N G S. H 12. 2. 1 S TA C K 11.H 1 2. 2. 3 12

268 C + + S TA C K 11.OBJ s t r i n g l i s t stack p u s h () s t r i n g s t a c k v o i d p e e k () p o p () s t r i n g v o i d 12 s t a c k :: ~ s t a c k () d e l e t e v o i d d e l e t e v o i d s t r i n g l i s t :: ~ s t r i n g l i s t () s t r i n g d e l e t e 1) stack p r o t e c t e d s t r i n g l i s t p r o t e c t e d 2) s t a c k 3) s t r i n g s t r i n g l i s t 13.5.3 15 public p r i v a t e 13. 5. 1 F N A M E 2. C P P private p r i v a t e p r i v a t e p r i v a t e p r i v a t e p r i v a t e p u b l i c p r i v a t e p u b l i c

13 269 p r i v a t e p r i v a t e 18 13.6 p r o t e c t e d p r i v a t e p r o t e c t e d p r i v a t e p r i v a t e C S S H A P E p r o t e c t e d

270 C + + p r i v a t e p u b l i c p r i v a t e p u b l i c p r o t e c t e d i s - a 13.7 16 13.8 13.9 ofstream ofstream 13.5.1 FNAME2.CPP ofstream fname2

13 271 i n s t r u m e n t w i n d i n s t r u m e n t p l a y () wind w i n d instrument t u n e () i n s t r u m e n t m a i n () t u n e () w i n d C + + w i n d i n s t r u m e n t t u n e () instrument w i n d t u n e () i n s t r u m e n t i n s t r u m e n t w i n d i n s t r u m e n t 13.9.1 W I N D. C C P 17

272 C + + 13.9.2 s t a c k s t r i n g l i s t s t r i n g I N H S TA C K. C P P ( 13. 5. 2 ) s t a c k s t r i n g l i s t s t r i n g l i s t p r i v a t e s t r i n g l i s t

13 273 13.9.3 W I N D. C P P ( 13. 9 ) w i n d i n s t r u m e n t 13.9.4 wind w; instrument * ip = &w; i p i n s t r u m e n t i p w i n d p l a y () i p - > p l a y ( m i d d l e C ) ; i n s t r u m e n t p l a y () i n s t r u m e n t p l a y () w i n d p l a y () C + + v i r t u a l 13.10 13.11 1. C A R. C P P v e h i c l e v e h i c l e v e h i c l e c a r 2. A B A C C B C C 3. 12 P S TASH.H & PSTA S H. C P P p s t a s h S t r i n g P S T E S T. C P P p s t a s h 4. p r i v a t e p r o t e c t e d

1 4 C + + w h a t h o w p r i v a t e C + + C C + + C + + C C + + C + + C C + + C C + + C + + O O P 14.1

14 275 t u n e () i n s t r u m e n t i n s t r u m e n t m a i n () w i n d t u n e () instrument w i n d w i n d i n s t r u m e n t w i n d i n s t r u m e n t w i n d i n s t r u m e n t 14.2 W I N D 2. C P P i n s t r u m e n t :: p l a y

276 C + + w i n d i n s t r u m e n t w i n d :: p l a y i n s t r u m e n t p l a y C W I N D 2. C P P b i n d i n g C i n s t r u m e n t 14.3 C + + v i r t u a l v i r t u a l v i r t u a l v i r t u a l v i r t u a v i r t u a l v i r t u a v i r t u a l W I N D 2. C P P p l a y () v i r t u a l

14 277 w i n d :: p l a y v i r t u a l W I N D 2. C P P play( ) v i r t u a l t u n e () O O P t u n e () i n s t r u m e n t t u n e ()

278 C + +

14 279 w i n d v i r t u a l a d j u s t () b r a s s w o o d w i n d A [] instrument f () t u n e () v i r t u a l C + + C 14.4 C++ C + + v i r t u a l b r a s s i n s t r u m e n t p l a y ()

280 C + + V TA B L E V TA B L E v p o i n t e r V P T R V TA B L E V P T R V TA B L E V TA B L E V P T R 14.4.1

14 281 i n t o n e _ v i r t u a l n o _ v i r t u a l v o i d V P T R one_virtual t w o _ v i r t u a l s V P T R C + + v i r t u a l int a 14.4.2 1 4. 3 W I N D 4. C P P A [] i n s t r u m e n t instrument w i n d p e r c u s s i o n s t r i n g b r a s s i n s t r u m e n t i n s t r u m e n t 14-1 i n s t r u m e n t v i r t u a l V TA B L E v i r t u a l v i r t u a b r a s s V TA B L E a d j u s t V P T R S I Z E S. C P P V P T R V P T R V TA B L E V P T R V TA B L E C A L L i n s t r u m e n t b r a s s a d j u s t ()

282 C + + i n s t r u m e n t i n s t r u m e n t i n s t r u m e n t i n s t r u m e n t V P T R V P T R V P T R V TA B L E V TA B L E 14-2 p l a y () w h a t () a d j u s t () a d j u s t () V P T R + 2 i n s t r u m e n t :: a d j u s t V P T R + 2 V P T R 14.4.3 f i n s t r u m e n t & i i. a d j u s t ( 1 ) ; C + + C 1 s i intel x86 i t h i s t h i s s t a t i c t h i s V P T R V TA B L E V P T R t h i s V P T R mov bx word ptr[si] s i t h i s V P T R V P T R b x b x V P T R V TA B L E V TA B L 0 V P T R a d j u s t () V TA B L E V TA B L E call word ptr [bx+4]

14 283 C C + + 14.4.4 vpointer V P T R V P T V TA B L E V P T R W I N D W I N D V P T R i n s t r u m e n t i n s t r u m e n t V P T R 14.4.5 C + + b 1 - > f () b 2. f () b 1 b 2 b a s e b 3. f () b a s e b a s

284 C + + 14.5 C + + C A L L [ 1 ] C + + C C U n i x C + + [ 2 C ] C C + + C C + + V TA B L E S t r o u s t r u p v i r t u a l C + + C 10 % C + + C 14.6 i n s t r u m e n t i n s t r u m e n t i n s t r u m e n t 14-3 i n s t r u m e n t [1] Smalltalk [2] C + +

14 285 v i r t u a l v i r t u a l i n s t r u m e n t i n s t r u m e n t i n s t r u m e n i n s t r u m e n t C + + virtual void x() = 0; V TA B L E V TA B L E V TA B L E WIND4.CPP 14.3

286 C + +

14 287 V TA B L E

288 C + + base VTA B L E 14.7 VTABLE V TA B L E V TA B L E

14 289 b a s e v a l u e () d e r i v e d s h i f t () v a l u e b a s e d e r i v e d V TA B L E derived VTA B L E v a l u e base VTA B L E d e r i v e d s h i f t V TA B L E d e r i v e d V TA B L E V TA B L E v a l u e d e r i v e d s h i f t V TA B L E V TA B L E ( ( d e r i v e d * ) B [ 1 ] ) - > s h i f t ( 3 ) B [ 1 ] d e r i v e d RT T I 1 8 C + + [ 1 ] 14-4 [1]

290 C + + c a l l () b a s e b a s e s u m () 10 57 10 c a l l () b a s e b a s e c a l l () b a s e 14-5 b a s e c a l l b a s e b a s e d e r i v e d d e r i v e d b a s e V P T R base VTA B L E b a s e b a s e

14 291 14.8 V P T R V TA B L E V P T R V P T R V P T R i n l i n e C + + i n l i n e V P T R t h i s operator new 14.8.1 class x public p r o t e c t e d

292 C + + 14.8.2 V P T R -- V P T R V TA B L E V P T R V TA B L E V P T R V TA B L E V P T R V P T R V TA B L E V TA B L V TA B L E V TA B L E 14.9 B

14 293 d e l e t e n e w d e l e t e V P T R

294 C + + V P T R 14.10 C + + c o n s t s w i t c h C + + C + + 15 17 14.11 1. s h a p e s h a p e c i r c l e s q u a r e t r i a n g l e d r a w () s h a p e d r a w () 2. 1 d r a w () s h a p e d r a w () 3. 4. E A R LY. C P P 5. class X X class Y X X 6. 5 V P T R 7. 8.

1 5 (building block), S m a l l t a l k C + + C + + S m a l l t a l k S m a l l t a l k C + + C + + C + + 15.1 C C + + s t a c k

296 C + +

15 297 i s t a c k i s t a c k I t e r i s t a c k i s t a c k I t e r i s t a c k i s t a c k i s t a c k I t e r i s t a c k i s t a c k I t e r + + f i b o n a c c i m a i n () n e w d e l e t e C A D C n e w d e l e t e h u g e m a l l o c () f r e e (), h u g e C + + n e w n e w 15.2 i s t a c k

298 C + + C C + + S m a l l t a l k C + + 15.2.1 C C s t a c k 15.2.2 Smalltalk S m a l l t a l k o b j e c t S m a l l t a l k S m a l l t a l k S m a l l t a l k S m a l l t a l k S m a l l t a l k o b j e c t S m a l l t a l k S m a l l t a l k o b j e c t o b j e c t O O P o b j e c t o b j e c t S m a l l t a l k C + + C + + 15-1 C + + S m a l l t a l k [1] C + + S m a l l t a l k C + + o b j e c t C o b j e c t C + + S m a l l t a l k o b j e c t Object [1] OOPS Keith Gorlen N I H

15 299 o s h a p e s h a p e o b j e c t C + + C + + C + + S m a l l t a l k o b j e c t [ 1 ] o b j e c t 15-2 15.2.3 [2] S t r o u s t r u p S m a l l t a l k S t r o u s t r u p C + + [3] 15-3 o b j e c t C + + 15.3 t e m p l a t e [1] AT & T [2] The C++Programming Language Bjarne Stroustrup A d d i s i o n - We s l e y, 1986 [3] A D A

300 C + + : template<class T> T a r r a y o p e r a t o r [] C a s s e r t () a s s e r t () a l l e g e () 17 m a i n ( ) array<int> ia; array<float> fa; a r r a y _ i n t a r r a y _ f l o a t i a f a 15.3.1

15 301 a r r a y < T > t e m p l a t e < > Windows DLL C P P 15.3.2 (the stack as a template) I S TA C K. C P P ( 1 5. 1 )

302 C + + stackt<t>& S p u s h () s t a c k t T

15 303 i s i t 15.3.3

304 C + + m b l o c k 17 a s s e r t () h o l d e r m b l o c k h o l d e r m b l o c k m b l o c k h o l d e r 15.4 stash & stack s t a s h s t a c k

15 305 15.4.1 C + + 15.4.2 stash s t a s h, ( 12 )

306 C + +

15 307

308 C + + enum owns s t o r a g e t s t a s h S O RTED.CPP ( 1 5. 7 ) o w n o w n s () t s t a s h I t e r forward() b a c k w a r d () r e m o v e () t s t a s h I n t 12 S t r i n g

15 309

310 C + + 15.4.3 stack 13 s t a c k

15 311

312 C + + t s t a s h O w n s () t s t a c k I t e r a t o r c u r r e n t () 11 operator int() C P P t s t a c k S t r i n g

15 313 15.5 s t a c k t s t a c 15.5.1 S S t r i n g

314 C + + typedef Hstring t y p e d e f = n e w d e l e t e = s t r i c m p () C 15.5.2 i n t e g e r i n t

15 315 11 15.6 t s t a s h o p e r a t o r []. 15.6.1 o p e r a t o r [] =

316 C + +

15 317 e x p a n d () e x p a n d () T **& r e a l l o c () s i z e index delete size int& o p e r a t o r [ ]

318 C + + C s t r t o k () St r i n g w o r d s 15.6.2

15 319 a d d ( ) c o n t a i n s ( ) i n d e x () o p e r a t o r [] l e n g t h ( ) i n d e x () c o n t a i n s () w i t h i n ( )

320 C + + strtok() a d d () c h a r * S S t r i n g < 40 > c h a r * a d d () a d d () a d d () n e w 15.6.3

15 321 o p e r a t o r = o p e r a t o r [] in_value() o u t _ v a l u e ()

322 C + + b u i l t - i n S S t r i n g

15 323 strcount[s]++;//count word s c h a r * o p e r a t o r [] S S t r i n g < 80 > S S t r i n g < 80 > ( c h a r *) o p e r a t o r [] o p e r a t o r [] i n t e g e r & a s s o c _ a r r a y C i s s p a c e () shoplist[&buf[i]] +=count; c h a r * S S t r i n g 15.7 t s t a s h

324 C + +

15 325 o p e r a t o r ( ) u r a n d m a p m a p m a s o r t e d > S S t r i n g i n t e g e r i n t () >

326 C + + i n t e g e r t s t a s h s t o r a g e s o r t e d t s t a s h s o r t e d t s t a s h s o r t e d S e t s o r t e d S e s e t s e s o r t e d C + + S O RT E D. H 15.7.1 SString i n t e rg e r s o r t e d a d d ()

15 327 1) 2) 3) C + + 15.7.2 13 I N H S TA K. C P P ( 1 3. 5. 2 ) s t a c k

328 C + + ( ) 15.8 18 ( RTTI)

15 329

330 C + +

15 331 t s t a c k t r a s h t r a s h t s t a c k t r a s h p u s h () t r a s h tally s o r t e r t r a s h b i n 15.9 C + + S T L standard template library S T L A S T L S T L (associative container) S T L

332 C + + S T L S T L S T L S T L S T S T L S T L 15.10 C + + [ 1 ] 15.10.1 m a l l o c () c a l l o c () r e a l l o c () [1] C++Inside & out(osborne/mcgraw-hill, 1993)

15 333 r e a l l o c ( ) ) getmem() g e t m e m ()

334 C + + typedef cntr o l d m e m o l d m e m g e t m e m () g e t m e m () f r e e m e m g e t m e m () o l d m e m f r e e () g e t m e m () c n t r o l d m e m T * c n t r * f r e e () free(&(((cntr*) oldmem )[-1])); g e t m e m () c n t r r e a l l o c () m e m s e t () o l d m e m o l d m e m = ( T * ) & ( ( ( c n t r * ) m ) [ 1 ] ) ; o l d m e m g e t m e m () g e t m e m ()

15 335 g e t m e m ( ) g e t m e m () void* & o l d m e m v o i d * 15.10.2 tstack t s t a c k t s t a c t s t a c k

336 C + +

15 337 a p p l i s t ( ) a p p l i s t ( ) 10 a p p l i s t () a p p l i s t ( ) [ 1 g r o m i t ] a p p l i s t () t s t a c k t s t a c 15.10.3 a p p l i s t (), d o g s. a p p l i s t ( & g r o m i t : : s i t ) ; [ 2 2 ] 15.11 template class bobbin<thread>; template void sort<char>(char *[]); S O RT E D. C P P 15. 7 [1] Nick Park [2]

338 C + + s o r t e d c h a r * c h a r * s o r t e d

15 339 15.12 b u b b l e s o r t () s t r c m p ( > O O P C [ 1 ] C + + 16 12 15.13 1. 14 tstack t s t a c k I t e r a t o r s h a p e t s t a c k s h a p e 2. 14 S S H A P E 2. C P P t s t a c k 3. R E C Y C L E. C P P t s t a s h t s t a c k 4. S E T T E S T. C P P s o r t e d S e t s e t. 5. tstash A P P L I S T. C P P 6. T S TA C K. H A P P L I S T. C P P t s t a c k 7. t s t a c k a d d () a d d () 8. t s t a c k 9. S O RT E D. C P P u r a n d S O RT E D. C P P 10. G E T M E M. H typedef cntr 11. S S t r i n g [1] C++

1 6 M I multiple inheritance) M I M I 16.1 C + + S m a l l t a l k S m a l l t a l k S m a l l t a l k, C + + C + + C S m a l l t a l k O b j e c t S m a l l t a l k S m a l l t a l k S m a l l t a l k Smalltalk class S m a l l t a l k O b j e c t C + + C + + M I C + + AT&T cfront2.0 M I M I M I M I v o i d * pstash s t a c k S m a l l t a l k O b j e c t s O b j e c t S m a l l t a l k S m a l l t a l k O b j e c t O b j e c t s 16-1 C + + A h o l d e r B B i t I m a g e B i t I m a g e h o l d e r B i t I m a g e h o l d e M I 15 M I

16 341 M I M I M I 16.2 16-2 d 1 d 2 m i m i d 1 d 2 m i 16-3 16-4 d 1 d 2 b a s e d 1 d 2 b a s e m i 16.3 m i b a s e b a s e?

342 C + + d 1 d 2 v f () m i b [] new mi b a s e * d 1 b a s e d 2 b a s e 16.4 m i v f () v i r t u a l v i r t u a l p o i n t e r m a g i c

16 343 16.4.1 m i v f () m o s t - d e r i v e d b a s e d 1 d 1 m i m

344 C + + d 1 d 2 b a s e m i x b a s e d 1 b a s e d 2

16 345 16.4.2,

346 C + + 16.5 pointer magic

16 347 core size s i z e o f ( b ) = 2 s i z e o f ( n o n v _ i n h e r i t a n c e ) = 2 sizeof (v_inheritance)=6 s i z e o f ( M I ) = 1 2 b n o n v _ i n h e r i t a n c e V P T R V P T R [ 1 ] 16.6 t h i s t h i s [1] Jan Gray C++Under the Hood, a chapter in Black Belt C++(edited by Bruce Eckel, M& T press, 1995)

348 C + + t h i s m i t h i s m i sizeof(mi)=40 hex mi this=0x223e base1 this=0x223e

16 349 base2 this=0x224e member1 this=0x225e member2 this=0x226e base 1 pointer=0x223e base 2 pointer=0x224e C + + b a s e 1 b a s e 2 t h i s 1) 2) 3) 4) s e r i a l i z a t i o n d e s e r i a l i z a t i o n serialize() 1. M I p e r s i s e n c e

350 C + +

16 351 persistent::read() p e r s i s t e n t :: w r i t e () t h i s read() write() I O w r i t e () 18 s i z e o f d a t a V P T R w d a t a 1 m a i n () F 1. D AT p e r s i s t e n t w d a t a 2 p e r s i s t e n t t h i s t h i s t h i s 18 2. [ 1 ] [1]

352 C + +

16 353

354 C + + p e r s i s t e n t d a t a d a t a p r o t e c t e d w d a t a 1 w d a t a 2 I O d a t a w r i t e () c o n g l o m e r a t e d a t a w d a t a 1 w d a t a 2 p e r s i s t e n t V P T R w r i t e () r e a d () r e a d () w r i t e () r e a d () w r i t e () c h a r * r e a d () r e a d () w r i t e () read() w r i t e ( ) C + + 16.7 MI P E R S I S T 2. C P P d a t a read() w r i t e () d a t a w d a t a 1 w d a t a 2 M I 1) 2)

16 355 M I 16.8 A () B ()

356 C + + V E N D O R. L I B V E N D O R. O B J

16 357 f () A () B ()

358 C + + m y b a s e ( M I ), f () g ( M I paste1::v() p a s t e 1 :: f () m a i n ( m y b a s e mybase * mp=plp;// upcast d e l e t e m p g () m p A () B () v () 16.9 C + + O O P O O C + + S m a l l t a l k C + + M I

16 359 Zack Urlocker g o t o g o t o M I C + + O c c a m O c c a m s Razor 16.10 1. M I i n t v o i d f () X X Y Z Y Z i n t Y Z A A f () 2. X p x A p x X A X 3. f () p x f ()

1 7, printf( ) C C + + 1) 2) C C + + t r y t h r o w c a t c h C + + 17.1 C 8 C assert( ) 8 assert( #define NDEBUG assert( ) allege( ) 8 allege( ) C 1) C errno( ) perror( )

17 361 2) C s i g n a l () r a i s e () 3) C setjmp( ) longjmp( ) setjmp( ) longjmp( ) setjmp( ) C + + C s e t j m p / l o n g j m p s e t j m p / l o n g j m p s e t j m p ( ) j m p _ b u f j m p _ b u f l o n g j m p () s e t j m p () s e t j m p () l o n g j m p () l o n g j m p ()

362 C + + j m p _ b u f g o t o s e t j m p / l o n g j m p [1] C++ longjmp() C++ 17.2 throw myerror ("something bad happened"); m y e r r o r t h r o w t h r o t h r o w 17.3 C + + 17.3.1 try t r y try { } // code that may generate exceptions [1] C + + longjmp( )

17 363 17.3.2 t r y c a t c h c a t c h i d 1 id2 c a t c h c a t c h s w i t c h c a s e b r e a k C + + C ++ w h i l e

364 C + + 17.3.3 C + + t h r o w t h r o void f ( ) throw ( toobig, toosmall, divzero) void f ( ); void f ( ) throw ( ); 1. unexpected( ) unexpected( ) 2. set_unexpected( ) unexpected( ) set_new_handler( ) set_unexpected( ) set_unexpected( ) v o i d u n e x p e c t e d unexpected( ) set_unexpected( ) E X C E P T. H

17 365 up f i t f( ) up f i t g( ) v e r s i o n 1 f( ) g( ) v e r s i o n 1 g( ) v e r s i o n 2 g( ) v e r s i o n 2 f( ) g( f( ) f( ) my_unexpected( ) unexpected( ) unexpected( ) exit( ) abort( ) unexpected( ) u n e x c e p t e d u n e x c e p t e d new_handler( ) unexpected( ) terminate( ) unexpected( ) f o r

366 C + + f o r, while, do if f( ) u p f i t f( ) g( ) v e r s i o n 2 my_unexpected( ) set_unexcepted( ) unexcepted( ) 17.3.4 void f ( ) void f ( ) throw (...) // not in C++ void f ( ) 17.3.5 (á la C) catch (... ) { } cout << "an exception was thrown" <<endl; 17.3.6 t h r o w catch (...) { } cout << "an exception was thrown "<<endl; t h r o w ;

17 367 c a t c h 17.3.7 1. terminate( ) terminate( ) unexception( C abort( ) abort( ) main( ) (catch(...)) 2. set_terminate( ) set_ terminate( ) terminate( ) set_ terminate( ) terminate( ) terminate( ) v o i d terminate( ) terminate( ) unexpected( ) terminate( ) set_ terminate( ) set_ terminate( ) terminate( ) t e r m i n a t e ( )

368 C + + o l d _ t e r m i n a t e o l d _ t e r m i n a t e set_ terminate( ) b o t c h f( ) terminate( ) c a t c h (... terminate( ) b o t c h terminate( ) 17.4 C + + unexpected( )

17 369

370 C + + n o i s y n o i s i o b j n u m n a m e C strncpy( ) z unexpected( ) new d e l e t e unexpected_rethrow( ) main( ) unexpected( ) n o i s y n 2

17 371 1 ~ 4 n e w d e l e t e d e l e t n e w 12 d e l e t e n 1 n 2 unexpected_rethrow( ) n 3 n 4 unexpection( ) unexpected_rethrow( ) unexpected_rethrow( ) n 4 n 3 unexpected_rethrow( ) 17.5

372 C + + u s e R e s o u r c e s b o n k og::operator new u s e R e s o u r c e s u s e R e s o u r c e s b o n k

17 373

374 C + + u s e R e s o u r c e s p w r a p o p e r a t o r [] r a n g e E r r o r o p e r a t o r [] o p e r a t o r [] 0 r a n g e E r r o r o g b o n k

17 375 17.6 e x c e p t 1 e x c e p t 2 e x c e p t 1

376 C + + t r o u b l e t r o u b l e s m a l l b i g t r o u b l e 17.7 C + + e x c e p t i o n l o g i c _ e r r o r u n t i m e _ e r r o r C + + w h a t () e x c e p t i o n e x c e p t i o n I / O i o s :: f a i l u r e e x c e p t i o n d o m a i n _ e r r o r i n v a l i d _ a rg u m e n t l e n g t h _ e r r o r o u t _ o f _ r a n g e b a d _ c a s t b a d _ t y p e i d l o g i c _ e r r o r N P O S N P O size_t d y n a m i c _ c a s t 18 t y p e i d (* p ) P 18 r u n t i m e _ e r r o r r a n g e _ e r r o r o v e r f l o w _ e r r o r b a d _ a l l o c

17 377 17.8 C 17.8.1 1. C s i g n a l () 2. C + + C + + 3. s w i t c h 4. a s s e r t () a b o r t () 5. t r y c a t c h ( ) t r y

378 C + + 17.8.2 1) 2) 3) 4) 5) 6) 7) C 8) 9) 1. u n e x p e c t () u n e x p e c t e d () 2. C + + e x p e c t i o n e x p e c t i o n w h a t () 3. 4. C + + 5. 15 6.

17 379, b a s e ( ) b a s e

380 C + + 7. 1) 2) C C C + + 8. t e r m i n a t e () t r y 9. 17. 5. 1 W R A P P E D. C P P 17.9 17.10 C + + C + + 17.11 1)

17 381 c h a r * t r y 2) 12 s t a s h o p e r a t o r [] o u t - o f - r a n g e 3) m a i n () 4) n e w 10 11 t r y c a t c h w h i l e 5) t e r m i n a t e () 6) 7) 8)

1 8 Run-time type identification, RT T I C + + C + + RT T I C + + RT T I 18.1 shape s h a p e c i r c l e s q u a r e t r i a n g l e s h a p e r h o m b o i d s h a p e d r a w () s h a p e d r a w () d r a w () s h a p e () ( c i r c l e s q u a r e t r i a n g l e ) s h a p e * 18.2 RTTI 18-1 Tu r n C o l o r I f Yo u A r e A () s h a p e :: c i r c l e s h a p e : : s q u a r e s h a p e : : t r i a n g l e i s A () type Of() RT T I t r i a n g l e

18 383 C + + RT T I C + + RT T I RT T I RTTI RT T I s i z e o f () t y p e i d () t y p e i n f o = = = n a m e () t y p e i d () s h a p e * s h a p e s s h a p e *, cout << typeid(*s).name()<<endl; s b e f o r e ( t y p e i n f o &) t y p e i n f o t y p e i n f o t r u e f a l s e if(typeid(me).before(typeid(you))) //... m e y o u RT T I c i r c l e * s h a p e * s h a p e c i r c l e * c i r c l e s h a p e * s h a p e * c i r c l e * C C + + s t a t i c _ c a s t, c i r c l e * c i r c l e RT T I s h a p e * c i r c l e *( ) n u l l c i r c l e * C + + RT T I d y n a m i c _ c a s t s h a p e

384 C + + s h a p e

18 385

386 C + + RT T I 18.3 RT T I 18.3.1 typeid() t y p e i d () t r u e 18.3.2 t y p e i d ( )

18 387 t y p e i n f o : : n a m e ( ) o n e : : n e s t e d 18.3.3 typeid( ) t y p e i d ( ) RT T I 18.3.4

388 C + + m i 2 m i m i 2 D 2 m i 2 d 1 d y n a m i c _ c a s t t y p e i d () t y p e i d ( typeinfo t r u e t y p e i d () d y n a m i c _ c a s t D 2 D 2 18.3.5 void v o i d v o i d * 18.3.6 RTTI RT T I

18 389 1 3 T Y P E I N F O. H t y p e i d () t y p e i n f o RT T I X 18.4 C + + RT T I

390 C + + t y p e i d ( ) t y p e i d ( ) t y p e i d () C + + b a d - c a s t D 1 X x r t y p e i d () C + + b a d - t y p e i d : t y p e i d 18.5 RT T I v i r t u a l

18 391 v i r t u a l t y p e i d () 18.6 RTTI RT T I s w i t c h RT T I C + + RT T I RT T I s w i t c h 1 4 1 4. 6 W I N D S. C P P i n s t r u m e n t

392 C + + C l e a r S p i t v a l v e () p e r c u s s i o n e l e c t r o n i c RT T I w i n d RT T I RT T I 15 trash recycling RT T I

18 393

394 C + + RT T I 15 RT T I C + + RT T I 18.7 RTTI RT T I V TA B L E t y p e i n f o t y p e i n f t y p e i d () V P T R t y p e i n f o t y p e i n f o d y n a m i c _ c a s t < * > < > RT T I * RT T I * *

18 395 t y p e i d ( ) 18.8 RTTI RT T I RT T RT T I C + + RT T I C + + RT T I RT T I d y n a c a s t () d y n a m i c _ t y p e ()

396 C + +

18 397 t y p e I D d y n a m i c _ t y p e () t y p e I D d y n a c a s t () s e c u r i t y t y p e I D b a s e I D b a s e I D e n u m p r o t e c t e d enum baseid s e c u r i t y I D I D I I D I p r o t e c t e d RT T I m e t a l c o m m o d i t y c o m m o d i t y s p e c i a l () m e t a l s p e c i a l () d y n a m i c _ t y p e () m e t a l c o m m o d i t y RT T I d y n a c a s t () d y n a m i c _ t y p e () t y p e I D d y n a m i c _ t y p e () t y p e I D

398 C + + d y n a m i c _ t y p e () d y n a m i c _ t y p e ( I D d y n a m i c _ t y p e () d y n a m i c _ t y p e () 1 ( t r u e ) d y n a c a s t () s e c u r i t y f a l s e, d y n a c a s t () C + + d y n a m i c _ c a s t C + + d y n a m i c _ c a s t D Y N A M I C - C A S T 18.9 [ 1 ] C v o i d C + + d y n a m i c _ c a s t ( ) c o n s t _ c a s t s t a t i c _ c a s t r e i n t e r p r e t _ c a s t n e w t y p e s t a t i c _ c a s t c o n s t _ c a s t d y n a m i c _ c a s t r e i n t e r p r e t _ c a s t c o n s t v o l a t i l e [1] Josée Lajoie The new cast notation and the bool data type C + + 1994 9 p p. 46-51.

18 399 18.9.1 static_cast s t a t i c _ c a s t s t a t i c _ c a s t v o i d *

400 C + + 1 C i n t l o n g f l o a t i n s t a t i c _ c a s t 2 i n t l o n g f l o a t C + + v o i d * C 3 s t a t i c _ c a s t 4 s t a t i c _ c a s t d y n a m i c _ c a s t 5 s t a t i c _ c a s t d y n a m i c _ c a s t 18.9.2 const_cast const c o n s t v o l a t i l e v o l a t i l e c o n s t _ c a s t c o n s t _ c a s t

18 401 c o n s t c o n s c o n s t c o n s t _ c a s v o l a t i l e c o n s t X * t h i s c o n s t _ c a s t m u t a b l e c o n s t 18.9.3 reinterpret_cast c o n s t _ c a s t r e i n t e r p r e t _ c a s t r e i n t e r p r e t _ c a s t C r e i n t e r p r e t _ c a s t

402 C + + X m a i n () X r e i n t e r p r e t _ c a s t i n t * i n t * [1] 1 47 0 0 0 0 0 47 0 0 0 0 V P T R x p [ 0 ] x p [ 1 ], V P T R V P T R l o n g ( r e i n t e r p r e t _ c a s t ) V P T R i n t * 0 47 0 0 0 [1]

18 403 r e i n t e r p r e t _ c a s t 18.10 RT T I RT T I RT T I 1) 2) 3) RT T I 4) RT T I C + + RT T I C + + 18.11 1. RT T I t y p e i d () 2. RT T I Tu r n C o l o r I f Yo u A r e A () 3. 14 W I N D 5. C P P i n s t r u m e n t w i n d C l e a r S p i t Va l v e () w i n d t s t a s h i n s t r u m e n t n e w i n s t r u m e n t RT T I w i n d C l e a r S p i t Va l v e () i n s t r u m e n t C l e a r S p i t Va l v e ()

A C + + A.1 bool true false [ 1 ] t y p e d e f t y p e d e f t y p e d e i n t i n t b o o l b o o l t r u e 1 f a l s e ( 0 ) & & < > <= >= ==!= if,for w h i l e, d o b o o l b o o l b o o b o o l b o o l?: b o o l i n t i n b o o l + + t r u e e n u m bool i n t b o o l 0 ~ 1 b o o l A.2 C + + C + + I O S T R E A M. H # include <iostream> [1] Josée Lajoie "The new cast notation and the bool data type C + + 1994 9

A 405 C. h C + + C A.3 C++ C + + C C C C + + I n t e r n e t B B S 6 C + + < c l i m i t s > < c f l o a t > < n e w > b a d _ a l l o c s e t _ n e w _ h a n d l e r RT T I < t y p e i n f o > t e r m i n a t e () unexpected() < e x c e p t i o n > C + + < s t d e x c e p t > < c a s s e r t > C A S S E RT. H C + +! = > < = > = t u p l e p a i r S T L S T C a p p e n d () a s s i g n () i n s e r t () r e m o v e () r e p l a c e () r e s i z e () c o p y () f i n d () r f i n d f i n d _ f i r s t _ o f () f i n d _ l a s t _ o f ( ) f i n d _ f i r s t _ n o t _ o f ( ) f i n d _ l a s t _ n o t _ o f ( ) s u b s t r ( ) c o m p a r e ( ) = + = [ ] w s t r i n g s t r i n g wstring ( < s t r i n g > C S T R I N G. H ) b a s i c _ s t r i n g s t r s t r e a m 6 < b i t s > < b i t s t r i n g > b i t s b i t _ s t r i n g b i t s b i t _ s t r i n g 5 b i t s set() reset() c o u n t () l e n g t h () t e s t () any() n o n e () t o _ u s h o r t ( t o _ u l o n g ( ) t o _ s t r i n g ( ) b i t _ s t r i n g b i t s s t r i n g b i t s b i t _ s t r i n g b i t s 0 b i t _ s t r i n g b i t s b i t _ s t r i n g b i t s b i t _ s t r i n g

406 C + + S T L S T L a d j a c e n t _ f i n d p r e v _ p e r m u t a t i o n b i n a r y _ s e a r c h p u s h _ h e a p c o p y r a n d o m _ s h u ff l e c o p y _ b a c k w a r d r e m o v e c o u n t r e m o v e _ c o p y c o u n t _ i f r e m o v e _ c o p y _ i f e q u a l, r e m o v e _ i f e q u a l _ r a n g e r e p l a c e f i l l r e p l a c e _ c o p y f i l l _ n r e p l a c e _ c o p y _ i f f i n d r e p l a c e _ i f f i n d _ i f r e v e r s e f o r _ e a c h r e v e r s e _ c o p y g e n e r a t e r o t a t e g e n e r a t e _ n r o t a t e _ c o p y i n c l u d e s s e a r c h i n p l a c e _ m e rg e s e t _ d i ff e r e n c e l e x i c o g r a p h i c a l _ c o m p a r e s e t _ i n t e r s e c t i o n l o w e r _ b o u n d s e t _ s y m m e t r i c _ d i ff e r e n c e m a k e _ h e a p s e t _ u n i o n m a x s o r t m a x _ e l e m e n t s o r t _ h e a p m e rg e s t a b l e _ p a r t i t i o n m i n s t a b l e _ s o r t m i n _ e l e m e n t s w a p m i s m a t c h s w a p _ r a n g e s n e x t _ p e r m u t a t i o n t r a n s f o r m n t h _ e l e m e n t u n i q u e p a r t i a l _ s o r t u n i q u e _ c o p y p a r t i a l _ s o r t _ c o p y u p p e r _ b o u n d p a r t i t i o n C + + f l o a t d o u b l e long double A.4 STL 1 5 C + + C + + STL [1] S T L S T L S T L S T L S T S T L S T L s e t s e i n t [1] Alexander Stepanov Meng Lee C + +

A 407 s e t i n s e r t () c o p y () s e t b e g i n () e n d () c o p y () i n t c o u t c o p y ( ) c o p y () I N T S E T. C P P 15 H s t r i n g i n t s t r t o k () I N T S E T. C P P

408 C + + S T L v e c t o r l i s t d e q u e s t a c k q u e u e p r i o r i t y _ q u e u e m u l t i s e t m a p m u l t i m a p / / b o o l / / / v e c t o r l i s t d e q u e l i s t d e q u e t o p (), p u s h () p o p () v e c t o r d e q u e ( h a s h ) S T L S T A.5 asm C + + C + + C + + A.6 & C C + + a n d o r n o t n o t _ e q b i t a n d a n d _ e q bit or o r _ e q x o r x o r _ e q c o m p l &&( )!! = & & = = ^ ^ = ~ 1

B [1] C + + Dan Saks Tom Plum C++ Programming G u i d e l i n e s Plum Hall 1991 Scott Meyers( E ffective C++ A d d i s o n - We s l e y, 1992 ) Rob Murray C++ strategies & Ta c t i c s A d d i s i o n - We s l e y, 1993 1. C + + C C + + C C + + C + + 2. 3. 4. private, p u b l i c p u b l i p r i v a t e p r o t e c t e d 5. C + + 6. 7. Andrew Koenig 8. 9. [1] Andrew Binstock Unix Review

410 C + + 10. 11. 12. 13. 14. is-a has-a 15. 16. p r o t e c t e d 17. 18. v i r t u a l 19. 20. 21. 22. C + + 23.

B 411 11 24. 25. = = 26. = 27. 3 /Cheshire cat 28. 29. 30. 31. o s t r e a m o s t r e a m class ostream o s t r e a m t y p e d e f #include <iostream.h> 32. = x = x * t h i s A = B = C 33. c o n s t 34. r e t u r n

412 C + + return foo(i,j); foo x(i,j); return x; 35. 36. 37. 38. C + + 39. 40. 41. 42. S T D I O. H p r i n t f () 6 C + + C 43. C C + + C + 44. * 45. = 46. foo a=b;

B 413 = foo a(b); 47. C + + C 48. C + + 49. 50. a s s e r t () 51. 52. set_unexpected() u n e x p e c t e d () u n e x p e c t e d ( ) 53. t e r m i n a t e () 54. t e r m i n a t e () 55. 56. f o r f o f o r f o 57. 58. n e w 0 0 59. n e w d e l e t e n e w d e l e t e n e w d e l e t e

414 C + + 60. 14 61. 62.

C C.1 s h a p e s d r a w () d r a w () 14 ` V TA B L E V P T R s h a p e c i r c l e s q u a r e t r i a n g l e Coplien [1] s h a p e [1] James O.Coplien C + + A d d i s o n _ We s l e y, 1992

416 C++

C 417 s h a p e s h a p e t y p e s h a p e s h a p e s h a p e t y p e s h a p e s h a p s h a p e 3 cheshire cat s h a p e t y p e class shape p r o t e c t e d s h a p e s h a p e ( t y p e ) s h a p e S 0 C o p l i e n S S S S 0

418 C++ s w i t c h d r a w () s h a p e d r a w () S d r a w () s h a p e m a i n () s [] s h a p e V P T R V P T R s h a p e c i r c l e square t r i a n g l e f o r s h a p e d r a w () V P T R s h a p e d r a w d r a w () S d r a w () s h a p e t h i s S S 0 p r o t e c t e d S 0 0 1 ) 2) t h i s t h i s S 0 C + + s h a p e d e l e t e s q u a r e

C 419 d e l e t e m a i n () delete s [i] S [ i ] s h a p e s h a p s q u a r e s h a p e s h a p e V P T R s h a p e V P T R s q u a r e s h a p e S d e l e t e S s q u a r e s q u a r e s q u a r e delete S S d e l e t e S delete 0 p r o t e c t e d S C.2

420 C++

C 421 e n u m p r o t e c t e d p r o t e c t e d

Powered by XiaoGuo s publishing Studio QQ:8204136 Website: www.mcuzone.com 2005