An Introduction to C++ Object Oriented Programming

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Author(s): Ian D. Chivers
Year: 1999

Language: English

Table of Contents
1 Over view 14
1.1 Aims 14
1.2 As sump tions 14
1.3 Ad di tional Ma te rial and Rec om mended Sources 14
1.4 Com pilers and Stan dards 15
1.4.1 Con tents of the stan dard 16
1.5 Old and New 17
1.6 Coda 17
1.7 Course De tails 18
1.8 Prob lems 18
2 An In tro duc tion to Pro gramming Lan guages and Ob ject Ori ented Pro gramming 20
2.1 For tran 66, 1966 20
2.2 Pascal, 1975, ANSI & BSI 1982, ISO 1983, Ex tended Pascal 1991? 20
2.3 For tran 77, 1978 21
2.4 C, K&R 1978, Stan dard 1989. 21
2.5 Mod ula 2, 1982, Stan dard 1996? 21
2.6 Ada, ISO 8652: 1987 21
2.7 C++, 1986, Stan dard No vem ber 1997 21
2.8 Oberon 2, Late 1980's, early 1990's. 22
2.9 For tran 90, 1991. 22
2.10 Eif fel, 1988 22
2.11 Ada, ISO 8652: 1995 22
2.12 Java 23
2.13 Vi sual Ba sic 23
2.14 Lan guage Com par i son 23
2.15 Lan guage Fea tures 25
2.15.1 In de pend ent Com pi la tion 25
2.15.2 Sep a rate Com pi la tion 25
2.15.3 Con crete Data Types 25
2.15.4 Ab stract Data Types 25
2.15.5 Dy namic ar rays 25
2.15.6 Nu meric and Gen eral Poly mor phism 25
2.15.7 Mod ules 26
2.15.8 Pointers and Ref er ences 26
2.15.9 Pro ce dure Vari ables 26
2.15.10 In her i tance 26
2.15.11 Dy namic Bind ing 26
2.15.12 Op er a tor Over loading 26
2.15.13 Threads/Multitasking 26
2.15.14 Ex cep tion Han dling 27
2.16 Some Im por tant Mile stones in Pro gram Lan guage De vel op ment 27
2.16.1 Struc tured Pro gramming 27
2.16.2 Step wise Re fine ment 27
2.16.3 Data Struc turing, Con crete vs Ab stract Data Types 27
2.16.4 In for ma tion Hiding Œ Mod ules 27
2.17 Ter mi nol ogy of Ob ject Ori ented Pro gramming 27
2.18 Par al lel De vel op ments 27
2.18.1 Par al lel For tran Œ For tran 95, For tran 2000, SMP, MPI, HPF 28
2.18.2 Par al lel C++ 28
2.19 Ob ject Ori ented Pro gramming 28
2.20 Ob ject Ori ented Lan guages 29
2.20.1 Simula Œ 1967 29
2.20.2 Smalltalk Œ 1978 29
2.20.3 C++ 29
2.20.4 Eif fel 29
2.20.5 Oberon 2 29
2.20.6 Ada 95 30
2.20.7 Java 30
2.21 Other Lan guages 31
2.21.1 For tran 90 31
2.21.2 Mod ula 2 31
2.22 The OO Ap proach 31
2.22.1 Meyer's Ap proach 31
2.22.2 Rumbaugh et al 31
2.22.3 Prac ti cal Steps 32
2.23 Sim ple Ex am ple 32
2.24 Other De vel op ments 33
2.24.1 De vel op ment En vi ron ments 33
2.24.2 Graph i cal De vel op ment Tools 34
2.24.3 Soft ware Com po nents 34
2.24.3.1 COM, OLE, ActiveX 34
2.24.3.2 JavaBeans 35
2.25 Coda 35
2.26 Bib li og ra phy 36
2.27 Prob lems 41
3 An In tro duc tion to C++ 44
3.1 Hello World Œ Old C style 44
3.2 Hello World Œ New stan dard C++ style 44
3.3 Sim ple text i/o us ing C style ar rays of char 45
3.4 Sim ple text i/o us ing C++ style strings 46
3.5 Sim ple nu meric i/o 46
3.6 Some C++ Rules and Ter mi nol ogy 47
3.7 Good Pro gramming Guide lines 48
3.8 C++ for C Pro gram mers 48
3.8.1 Macros 48
3.8.2 Malloc 48
3.8.3 Pointers 48
3.8.4 Ar rays and C style strings 48
3.9 C++ Char ac ter Set 48
3.10 Sum mary 49
3.11 Key Con cepts 49
3.11.1 Ba sic struc ture of a C++ pro gram 49
3.11.2 In den ta tion 49
3.11.3 Data Types 49
3.12 Bib li og ra phy 50
3.13 Prob lems 50
4 Arith me tic and Ex pres sions in C++ 54
4.1 Ba sic nu meric types 54
4.2 In te ger Nu meric Type 54
4.2.1 Vari a tions on a theme - signed, un signed 54
4.3 Real Nu meric Type 55
4.4 Nu meric Type Con ver sion Rules 55
4.5 Com plex? 56
4.6 const 56
4.7 Char ac ter Data as a form of In te ger Data 56
4.8 Op er a tors and Ex pres sion Eval u a tion 57
4.8.1 Ex pres sion Eval u a tion 57
4.8.2 Se quence Points 57
4.8.3 Lvalue and Rvalue 57
4.8.4 Op er a tors, Pre ce dence and As so cia tiv ity. 57
4.8.4.1 :: [scope res o lu tion] class_name :: mem ber 59
4.8.4.2 :: [global] :: name 59
4.8.4.3 . [mem ber se lec tion] ob ject.mem ber 59
4.8.4.4 -> [mem ber se lec tion] pointer -> mem ber 59
4.8.4.5 [] [subscripting] pointer [expr] 59
4.8.4.6 () [func tion call] expr (expr_list) 59
4.8.4.7 () [value con struc tion] type(expr_list) 59
4.8.4.8 ++ [post in cre ment] lvalue ++ 59
4.8.4.9 Š [post dec re ment] lvalue Š 59
4.8.4.10 sizeof [size of ob ject] sizeof expr 59
4.8.4.11 sizeof [size of type] sizeof (type) 59
4.8.4.12 ++ [pre in cre ment] ++ lvalue 59
4.8.4.13 Š [pre dec re ment] Š lvalue 60
4.8.4.14 ~ [com ple ment] ~ expr 60
4.8.4.15 ! [not] ! expr 60
4.8.4.16 - [unary mi nus] - expr 60
4.8.4.17 + [unary plus] + expr 60
4.8.4.18 & [ad dress of] & expr 60
4.8.4.19 * [dereference] * expr 60
4.8.4.20 new [cre ate] new type 60
4.8.4.21 de lete [de stroy] de lete pointer 60
4.8.4.22 de lete[] [de stroy ar ray] de lete [] pointer 60
4.8.4.23 () [cast] (type) expr 60
4.8.4.24 .* [mem ber se lec tion] ob ject.* pointer_to_mem ber 60
4.8.4.25 ->* [mem ber se lec tion] pointer -> * pointer_to_mem ber 60
4.8.4.26 * [mul ti ply] expr * expr 60
4.8.4.27 / [di vide] expr / expt 60
4.8.4.28 % [modulo or re main der] expr % expr 61
4.8.4.29 + [plus] expr + expr 61
4.8.4.30 - [mi nus] expr - expr 61
4.8.4.31 << [shift left] expr << expr 61
4.8.4.32 >> [shift right] expr >> expr 61
4.8.4.33 < [less than] expr < expr 61
4.8.4.34 <= [less than or equal] expr <= expr 61
4.8.4.35 > [greater than] expr > expr 61
4.8.4.36 >= [greater than or equal] expr >= expr 61
4.8.4.37 == [equal] expr == expr 61
4.8.4.38 != [not equal] expr != expr 61
4.8.4.39 & [bitwise AND] expr & expr 61
4.8.4.40 ^ [bitwise ex clu sive OR] expr ^ expr 61
4.8.4.41 | [bitwise in clu sive OR] expr | expr 61
4.8.4.42 && [log i cal AND] expr && expr 61
4.8.4.43 || [log i cal in clu sive OR] expr || expr 61
4.8.4.44 ?: [con di tional ex pres sion] expr ? expr : expr 62
4.8.4.45 = [con ven tional as sign ment] lvalue = expr 62
4.8.4.46 *= [mul ti ply and as sign] lvalue *= expr 62
4.8.4.47 /= [di vide and as sign] lvalue /= expr 62
4.8.4.48 %= [modulo and as sign] lvalue %= expr 62
4.8.4.49 += [add and as sign] lvalue += expr 62
4.8.4.50 -= [sub tract and as sign] lvalue -= expr 62
4.8.4.51 <<= [shift left and as sign] lvalue <<= expr 62
4.8.4.52 >>= [shift right and as sign] lvalue >>= expr 62
4.8.4.53 &= [AND and as sign] lvalue &= expr 62
4.8.4.54 |= [in clu sive OR and as sign] lvalue |= expr 62
4.8.4.55 ^= [ex clu sive OR and as sign] lvalue ^= expr 62
4.8.4.56 throw [throw ex cep tion] throw expr 62
4.8.4.57 , [comma] expr , expr 62
4.9 Ex pres sion Ex am ples 62
4.10 Sum mary 64
4.11 Key Con cepts 65
4.11.1 Nu meric Data Types 65
4.11.1.1 In te ger 65
4.11.1.2 Real 65
4.11.1.3 Com plex Œ sup ported via 65
4.11.1.4 Char ac ter Data as Nu meric Data 65
4.11.2 Con stants Œ use the const at trib ute 65
4.11.3 Op er a tors Œ 45 but ef fec tively 57 with vari ants 65
4.11.4 Ex pres sions Œ the or der of ex pres sion eval u a tion is of ten un de fined 65
4.12 Prob lems 65
5 Strings and other Data Types 68
5.1 Char ac ter Data or Strings 68
5.1.1 C Style strings 68
5.1.1.1 Ex am ple 1 68
5.1.1.2 Ex am ple 2 68
5.1.1.3 strcpy(s1,s2) 69
5.1.1.4 strcat(s1,s2) 69
5.1.1.5 strcmp(s1,s2) 69
5.1.1.6 strlen(s) 69
5.1.1.7 strchr(s,c) 69
5.1.2 C++ Style strings Œ 69
5.1.2.1 as sign ment 69
5.1.2.2 char ac ter ac cess 69
5.1.2.3 com par i son 70
5.1.2.4 con cat e na tion 70
5.1.2.5 con struc tors 70
5.1.2.6 i/o 70
5.1.2.7 in ser tion 70
5.1.2.8 iterators 70
5.1.2.9 length 70
5.1.2.10 re moval 70
5.1.2.11 re place ment 70
5.1.2.12 resize 70
5.1.2.13 search 70
5.1.2.14 Ex am ple 3 71
5.1.3 Guide lines for use 71
5.2 Boolean or Log i cal Data 71
5.3 Ref er ence Vari ables 71
5.4 Enu mer a tion Types 72
5.5 Type Con ver sion 73
5.6 Scope 73
5.7 Void 74
5.8 Mem ory and C++ 74
5.9 Sum mary 74
5.10 Key Con cepts 74
5.10.1 Data Types 74
5.10.1.1 Se quence of char ac ters as an ar ray of char 74
5.10.1.2 Seqence of char ac ters as a string 74
5.10.1.3 Log i cal or boolean 74
5.10.1.4 Ref er ence Vari able 74
5.10.1.5 Enu mer ated Types 74
5.10.1.6 Void 74
5.10.1.7 Type Con ver sion 74
5.10.2 Scope 74
5.10.3 Mem ory 74
5.11 Prob lems 75
6 Ar rays, Vec tors and Valarrays in C++ 78
6.1 Old C Style ar rays 78
6.1.1 One d ex am ple Œ Monthly Rain fall 78
6.1.2 One d ar ray Œ Peo ple's Weights 79
6.1.3 Two d Ar ray Œ lat i tude and lon gi tude 79
6.1.4 Sim ple phys ics Œ volt age ex am ple 80
6.1.5 Time Zone Ex am ple 80
6.1.6 Ar ray In itial is ation 81
6.1.7 Whole Ar ray Ma nip u la tion 81
6.2 Vec tors 81
6.2.1 Ex am ple 1 81
6.2.2 Ex am ple 2 Œ sub script check ing 82
6.2.3 Ex am ple 3 Œ sub script check ing with try/catch 83
6.2.4 Ex am ple 4 Œ whole ar ray as sign ment 83
6.3 Valarrays 84
6.3.1 Ex am ple 1 84
6.4 Ar ray El e ment Or der ing in C++ 85
6.5 Sum mary 85
6.6 Key Con cepts 85
6.6.1 Data Types 85
6.6.1.1 ar ray 85
6.6.1.2 vec tor 85
6.6.1.3 valarray 86
6.6.1.4 As so ci ated con trol struc ture Œ for loop 86
6.7 Prob lems 86
7 Con trol Struc tures 88
7.1 Com pound State ment or Block 88
7.2 Ex pres sion 88
7.3 Boolean 88
7.4 if (ex pres sion) state ment 88
7.4.1 Ex am ple 1 88
7.4.2 Ex am ple 2 88
7.5 if (ex pres sion) state ment; else state ment; 89
7.5.1 Ex am ple 1 89
7.5.2 Ex am ple 2 89
7.6 switch (ex pres sion) state ment 89
7.6.1 Ex am ple 1 89
7.7 while (ex pres sion) state ment 90
7.7.1 Ex am ple 1 90
7.8 do state ment while (ex pres sion); 90
7.8.1 Ex am ple 1 90
7.9 for (init-statement;ex pres sion 1; ex pres sion 2) state ment 91
7.9.1 Ex am ple 1 91
7.9.2 Ex am ple 2 92
7.10 break, con tinue, goto state ments 92
7.11 Sum mary 92
7.12 Key Con cepts 93
7.12.1 log i cal ex pres sions 93
7.12.2 log i cal and re la tional op er a tors 93
7.12.3 A block of state ments Œ { .;.;.} 93
7.12.4 Con trol State ments 93
7.12.4.1 the if ex pres sion state ment 93
7.12.4.2 the if ex pres sion state ment else state ment 93
7.12.4.3 the switch state ment 93
7.12.4.4 while ex pres sion state ment 93
7.12.4.5 do state ment while ex pres sion 93
7.12.4.6 the for () state ment 93
7.12.4.7 break state ment 93
7.12.4.8 con tinue state ment 93
7.12.4.9 goto state ment 93
7.13 Prob lems 93
7.14 Bib li og ra phy 95
8 Pointers 98
8.1 Ex am ple 1: Ba sic Pointer Us age 98
8.2 Ex am ple 2: Ar rays and Pointers 99
8.3 Ex am ple 3: Pointers and Sen ti nels 99
8.4 Ex am ple 4: In dis crim i nate Pointer Us age 100
8.5 Sum mary 101
8.6 Key Con cepts 101
8.6.1 * Œ pointer to 101
8.6.2 & Œ ad dress of 101
8.6.3 ar rays and point ers 101
8.7 Prob lems 101
9 Func tions 104
9.1 Pre de fined Func tions 104
9.1.1 Trig o no met ric func tion us age 104
9.1.2 Pass ing ar gu ments of nu meric type to stan dard maths func tions 104
9.1.3 Func tions in 105
9.2 User De fined Func tions 105
9.2.1 One d ar ray as ar gu ment and one func tion 105
9.2.2 One func tion, vec tor as ar gu ment 105
9.2.3 3 func tions, one d ar ray as ar gu ment 106
9.2.4 Using 2d ar rays as ar gu ments 107
9.2.5 Pass ing 2d dy namic ar rays as ar gu ments 108
9.2.6 Pass ing func tions as ar gu ments to other func tions and pro ce dure vari ables 109
9.3 Func tion Ar gu ments 111
9.3.1 Swapping ar gu ments Œ pass ing by ad dress 111
9.3.2 Swapping ar gu ments Œ pass ing by ref er ence 111
9.3.3 May hem 112
9.4 C++ Stan dard Func tions 112
9.5 Sum mary 113
9.6 Key Con cepts 113
9.6.1 Pre de fined func tions 113
9.6.2 User De fined func tions 113
9.6.3 Ba sic syn tax 113
9.6.4 Pa ram e ter Pass ing 113
9.6.4.1 Pass by value Œ copy made 113
9.6.4.2 Ar ray as pa ram e ter Œ base ad dress used 113
9.6.4.3 Pass by ref er ence 114
9.6.4.4 Pass by const ref er ence 114
9.7 Prob lems 114
10 Classes Œ User De fined Data Types 116
10.1 Con crete Data Types 116
10.1.1 Dates 116
10.1.2 Ad dresses 117
10.1.3 Nested Data Types 118
10.1.4 Read ing user in put us ing a sin gly linked list 119
10.1.5 Read ing user in put us ing C++ style strings 120
10.2 Ab stract Data Types 120
10.2.1 Dates 120
10.2.2 Ad dresses 122
10.3 Sphere class 124
10.3.1 trans late_with_copy_sphere 127
10.3.2 trans late_by_ref er ence_sphere 127
10.3.3 trans late_by_pointer_sphere 127
10.4 Con struc tors and De struc tors 128
10.4.1 Con struc tor/De struc tor Ex am ple 1 128
10.4.2 Con struc tor/De struc tor Ex am ple 2 129
10.4.3 Con struc tor/De struc tor Ex am ple 3 131
10.4.4 Con struc tor/De struc tor Ex am ple 4 132
10.4.5 Con struc tor/De struc tor Ex am ple 5 133
10.4.6 Con struc tor/De struc tor Ex am ple 6 136
10.4.7 Con struc tor/De struc tor Rec om men da tions 138
10.4.8 Mem ory Al lo ca tion and Deallocation Œ Leak age and Gar bage Col lec tion 138
10.5 The C++ ob ject Model Œ taken from the stan dard 138
10.6 Sum mary 139
10.7 Key Con cepts 139
10.7.1 Con crete data types 139
10.7.1.1 Data pub lic 139
10.7.1.2 Func tions pub lic 139
10.7.2 Ab stract data types 139
10.7.2.1 Data Pri vate 139
10.7.2.2 Func tions pub lic 139
10.7.3 Con struc tors and De struc tors 139
10.7.3.1 Sim ple con struc tor 139
10.7.3.2 Copy con struc tor 139
10.7.3.3 Over load as sign ment op er a tor 139
10.7.3.4 De struc tor 139
10.7.4 Ba sic Class Syn tax 139
10.8 Prob lems 139
10.9 Bib li og ra phy 140
11 Tem plates 142
11.1 Ex am ple 1 Œ Sim ple min i mum 142
11.2 Ex am ple 2 Œ Hoare's Quicksort 143
11.3 Other Is sues 144
11.4 Sum mary 144
11.5 Key Con cepts 144
11.5.1 Ba sic Syn tax Ex am ple 144
11.6 Prob lems 145
12 Op er a tor and Func tion Over loading 148
12.1 Op er a tor Over loading. 148
12.1.1 Unary and Bi nary Op er a tors 148
12.1.2 Op er a tor Over loading 1: + , - , = 149
12.1.3 Op er a tor Over loading 2: [] 153
12.1.4 Op er a tor Over loading 3: () 153
12.1.5 Op er a tor Over loading 4: << and >> 154
12.1.6 Guide lines for Op er a tor Over loading 155
12.2 Func tion Over loading 156
12.2.1 Over load Res o lu tion 156
12.2.2 Ex act Matching 156
12.2.3 Matching through pro mo tion 158
12.2.4 Matching through Stan dard Con ver sion 158
12.2.5 Matching through User De fined Con ver sion 159
12.2.6 Guide lines for Func tion Over loading 160
12.3 Key Con cepts 160
12.3.1 Func tion Over loading 160
12.3.1.1 Func tions dis tin guished by sig na ture 160
12.3.2 Op er a tor Over loading 160
12.3.2.1 Unary op er a tors 160
12.3.2.2 Bi nary op er a tors 160
12.3.2.3 Commutivity 160
12.4 Prob lems 160
13 Vir tual Func tions and Ab stract Data Types 162
13.1 Vir tual Func tion Ex am ple 1 162
13.2 Vir tual Func tion Ex am ple 2 164
13.3 Sum mary 166
13.4 Key Con cepts 167
13.4.1 Vir tual func tion 167
13.4.2 Pure Vir tual Func tion 167
13.4.3 Pri vate 167
13.4.4 Pro tected 167
13.4.5 Pub lic 167
13.5 Prob lems 167
14 Com plete OO Ex am ple 170
14.1 Range Checked Ar ray 170
14.1.1 Notes 172
14.1.1.1 Class ar ray 172
14.1.1.2 Class checked_ar ray 172
14.2 Sum mary 173
14.3 Where do I go next? 173
14.4 Prob lems 173
15 Files and i/o 176
15.1 Nu meric i/o: width and pre ci sion 176
15.2 Nu meric i/o: set ting jus ti fi ca tion 176
15.3 Nu meric i/o: sci en tific no ta tion 176
15.4 Nu meric i/o: al ter nate num ber bases, oc tal and hex a dec i mal 177
15.5 File i/o: pick ing up the names from the com mand line 177
15.6 File i/o: hard coded file names in con struc tors 178
15.7 File i/o: strings passed as ar gu ments to con struc tors 178
15.8 Sum mary 178
15.9 Key Con cepts 179
15.9.1 Set ting the width 179
15.9.2 Set ting the pre ci sion 179
15.9.3 Jus ti fi ca tion 179
15.9.4 Read ing from files 179
15.9.5 Writ ing to files 179
15.9.6 Picking up files from the com mand line 179
15.9.7 Al ter nate num ber bases for nu meric out put 179
15.9.7.1 oc tal 179
15.9.7.2 dec i mal 179
15.9.7.3 hex a dec i mal 179
15.10 Prob lems 179
16 Er rors and Ex cep tion Han dling 182
16.1 Linked List Œ Pascal 182
16.2 Linked List Œ For tran 90 183
16.3 Linked List Œ C++, old C syn tax 183
16.4 Dis cus sion 185
16.5 Ex am ple 1 Œ Ba sic Syn tax 185
16.6 Ex am ple 2 Œ Ex cep tion raised in a func tion 185
16.7 Ex am ple 3 Œ Func tion with Ex cep tion Spec i fi ca tion 186
16.8 Ex am ple 5 Œ Ex cep tions and con struc tors and de struc tors. 188
16.9 Key Con cepts 188
16.9.1 Ba sic Syn tax 188
16.10 Prob lems 188
17 The Stan dard Tem plate Li brary 190
17.1 Li brary Or gani sa tion 190
17.1.1 Some ba sic ter mi nol ogy 190
17.2 Con tainers 190
17.2.1 vec tor 190
17.2.2 list 191
17.2.3 queue 191
17.2.4 stack 191
17.2.5 deque 192
17.2.6 map, multimap 192
17.2.7 set, bitset, multiset 192
17.3 Iterators 192
17.4 Mis cel la neous Op er a tions 192
17.5 Con struc tors 193
17.6 Al go rithms 193
17.6.1 Non mod i fy ing se quence op er a tions 193
17.6.2 Mod ifying se quence op er a tions 193
17.6.3 Sorted se quences 194
17.6.4 Set al go rithms 194
17.6.5 Heap op er a tions 195
17.6.6 Min i mum and max i mum 195
17.6.7 Per mu ta tions 195
17.7 Strings 195
17.8 Numerics 195
17.8.1 com plex 196
17.8.2 valarray 196
17.8.3 nu meric 196
17.9 Com plete list of C++ tem plate classes 197
17.10 Use ful Sources 197
17.11 Sum mary 197
17.12 Prob lems 197
18 Mis cel la nea 200
18.1 C++ as a Pro gramming Lan guage 200
18.2 The Stan dard 200
18.3 C++ Stan dard Def i ni tions 201
18.3.0.1 ar gu ment 201
18.3.0.2 dy namic type 201
18.3.0.3 im ple men ta tion de fined be hav iour 201
18.3.0.4 im ple men ta tion lim its 201
18.3.0.5 multibyte char ac ter 201
18.3.0.6 pa ram e ter 201
18.3.0.7 sig na ture 201
18.3.0.8 static type 201
18.3.0.9 un de fined be hav iour 201
18.3.1 C++ Im ple men ta tion Op tions: Free vs Hosted 202
18.3.2 The C++ Mem ory Model, taken from the stan dard 202
18.4 Coda 202