ebook img

An Introduction to C++ Object Oriented Programming PDF

191 Pages·1999·0.563 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview An Introduction to C++ Object Oriented Programming

An Introduction to C++ and Object Oriented Programming Abstract The aim of the notes is to provide an introduction to the C++ programming language and object oriented programming. It is assumed that you know one programming language mod- erately well. Author: Ian D Chivers Email: [email protected] Version: 6.1 Date: August 1999 © Ian D Chivers. Permission to copy all or part of this work is granted, provided that the copies are not made or distributed for resale (except a nominal copy fee may be charged), and provided that the Author, Copyright, & No Warranty sections are retained verbatim and are displayed conspicuously. If anyone needs other permissions that aren't covered by the above, please contact the author. No Warranty: this work is provided on an as is basis. The author provides no warranty whatsoever, either express or implied, regarding the work, including warranties with respect to its merchantability or fitness for any particular purpose. All comments welcome. Contents 3 Table of Contents 1 Overview...............................................................................................................14 1.1 Aims............................................................................................................................14 1.2 Assumptions...............................................................................................................14 1.3 Additional Material and Recommended Sources......................................................14 1.4 Compilers and Standards............................................................................................15 1.4.1 Contents of the standard...................................................................................................16 1.5 Old and New...............................................................................................................17 1.6 Coda............................................................................................................................17 1.7 Course Details............................................................................................................18 1.8 Problems.....................................................................................................................18 2 An Introduction to Programming Languages and Object Oriented Pro- gramming...................................................................................................................20 2.1 Fortran 66, 1966.........................................................................................................20 2.2 Pascal, 1975, ANSI & BSI 1982, ISO 1983, Extended Pascal 1991?......................20 2.3 Fortran 77, 1978.........................................................................................................21 2.4 C, K&R 1978, Standard 1989....................................................................................21 2.5 Modula 2, 1982, Standard 1996?...............................................................................21 2.6 Ada, ISO 8652: 1987.................................................................................................21 2.7 C++, 1986, Standard November 1997.......................................................................21 2.8 Oberon 2, Late 1980's, early 1990's...........................................................................22 2.9 Fortran 90, 1991.........................................................................................................22 2.10 Eiffel, 1988.................................................................................................................22 2.11 Ada, ISO 8652: 1995.................................................................................................22 2.12 Java.............................................................................................................................23 2.13 Visual Basic................................................................................................................23 2.14 Language Comparison................................................................................................23 2.15 Language Features......................................................................................................25 2.15.1 Independent Compilation.................................................................................................25 2.15.2 Separate Compilation.......................................................................................................25 2.15.3 Concrete Data Types........................................................................................................25 2.15.4 Abstract Data Types.........................................................................................................25 2.15.5 Dynamic arrays.................................................................................................................25 2.15.6 Numeric and General Polymorphism...............................................................................25 2.15.7 Modules ...........................................................................................................................26 2.15.8 Pointers and References...................................................................................................26 2.15.9 Procedure Variables.........................................................................................................26 2.15.10 Inheritance........................................................................................................................26 2.15.11 Dynamic Binding.............................................................................................................26 2.15.12 Operator Overloading.......................................................................................................26 2.15.13 Threads/Multitasking........................................................................................................26 2.15.14 Exception Handling..........................................................................................................27 2.16 Some Important Milestones in Program Language Development............................27 2.16.1 Structured Programming..................................................................................................27 2.16.2 Stepwise Refinement........................................................................................................27 2.16.3 Data Structuring, Concrete vs Abstract Data Types........................................................27 2.16.4 Information Hiding – Modules........................................................................................27 2.17 Terminology of Object Oriented Programming.........................................................27 2.18 Parallel Developments................................................................................................27 2.18.1 Parallel Fortran – Fortran 95, Fortran 2000, SMP, MPI, HPF.......................................28 2.18.2 Parallel C++......................................................................................................................28 2.19 Object Oriented Programming...................................................................................28 2.20 Object Oriented Languages........................................................................................29 2.20.1 Simula – 1967..................................................................................................................29 2.20.2 Smalltalk – 1978..............................................................................................................29 2.20.3 C++...................................................................................................................................29 2.20.4 Eiffel.................................................................................................................................29 2.20.5 Oberon 2...........................................................................................................................29 2.20.6 Ada 95..............................................................................................................................30 2.20.7 Java...................................................................................................................................30 2.21 Other Languages.........................................................................................................31 4 Contents 2.21.1 Fortran 90.........................................................................................................................31 2.21.2 Modula 2...........................................................................................................................31 2.22 The OO Approach......................................................................................................31 2.22.1 Meyer's Approach............................................................................................................31 2.22.2 Rumbaugh et al................................................................................................................31 2.22.3 Practical Steps..................................................................................................................32 2.23 Simple Example..........................................................................................................32 2.24 Other Developments...................................................................................................33 2.24.1 Development Environments.............................................................................................33 2.24.2 Graphical Development Tools.........................................................................................34 2.24.3 Software Components......................................................................................................34 2.24.3.1 COM, OLE, ActiveX.................................................................................................34 2.24.3.2 JavaBeans...................................................................................................................35 2.25 Coda............................................................................................................................35 2.26 Bibliography...............................................................................................................36 2.27 Problems.....................................................................................................................41 3 An Introduction to C++......................................................................................44 3.1 Hello World – Old C style.........................................................................................44 3.2 Hello World – New standard C++ style....................................................................44 3.3 Simple text i/o using C style arrays of char..............................................................45 3.4 Simple text i/o using C++ style strings.....................................................................46 3.5 Simple numeric i/o.....................................................................................................46 3.6 Some C++ Rules and Terminology...........................................................................47 3.7 Good Programming Guidelines..................................................................................48 3.8 C++ for C Programmers.............................................................................................48 3.8.1 Macros..............................................................................................................................48 3.8.2 Malloc...............................................................................................................................48 3.8.3 Pointers.............................................................................................................................48 3.8.4 Arrays and C style strings................................................................................................48 3.9 C++ Character Set......................................................................................................48 3.10 Summary.....................................................................................................................49 3.11 Key Concepts..............................................................................................................49 3.11.1 Basic structure of a C++ program...................................................................................49 3.11.2 Indentation........................................................................................................................49 3.11.3 Data Types........................................................................................................................49 3.12 Bibliography...............................................................................................................50 3.13 Problems.....................................................................................................................50 4 Arithmetic and Expressions in C++..................................................................54 4.1 Basic numeric types...................................................................................................54 4.2 Integer Numeric Type................................................................................................54 4.2.1 Variations on a theme - signed, unsigned........................................................................54 4.3 Real Numeric Type....................................................................................................55 4.4 Numeric Type Conversion Rules...............................................................................55 4.5 Complex?....................................................................................................................56 4.6 const............................................................................................................................56 4.7 Character Data as a form of Integer Data..................................................................56 4.8 Operators and Expression Evaluation........................................................................57 4.8.1 Expression Evaluation......................................................................................................57 4.8.2 Sequence Points................................................................................................................57 4.8.3 Lvalue and Rvalue............................................................................................................57 4.8.4 Operators, Precedence and Associativity.........................................................................57 4.8.4.1 :: [scope resolution] class_name :: member..............................................................59 4.8.4.2 :: [global] :: name......................................................................................................59 4.8.4.3 . [member selection] object.member.........................................................................59 4.8.4.4 -> [member selection] pointer -> member................................................................59 4.8.4.5 [] [subscripting] pointer [expr]..................................................................................59 4.8.4.6 () [function call] expr (expr_list)...............................................................................59 4.8.4.7 () [value construction] type(expr_list).......................................................................59 4.8.4.8 ++ [post increment] lvalue ++...................................................................................59 4.8.4.9 — [post decrement] lvalue —...................................................................................59 4.8.4.10 sizeof [size of object] sizeof expr.............................................................................59 4.8.4.11 sizeof [size of type] sizeof (type)..............................................................................59 Contents 5 4.8.4.12 ++ [pre increment] ++ lvalue....................................................................................59 4.8.4.13 — [pre decrement] — lvalue.....................................................................................60 4.8.4.14 ~ [complement] ~ expr..............................................................................................60 4.8.4.15 ! [not] ! expr..............................................................................................................60 4.8.4.16 - [unary minus] - expr................................................................................................60 4.8.4.17 + [unary plus] + expr.................................................................................................60 4.8.4.18 & [address of] & expr................................................................................................60 4.8.4.19 * [dereference] * expr................................................................................................60 4.8.4.20 new [create] new type................................................................................................60 4.8.4.21 delete [destroy] delete pointer...................................................................................60 4.8.4.22 delete[] [destroy array] delete [] pointer...................................................................60 4.8.4.23 () [cast] (type) expr....................................................................................................60 4.8.4.24 .* [member selection] object.* pointer_to_member..................................................60 4.8.4.25 ->* [member selection] pointer -> * pointer_to_member........................................60 4.8.4.26 * [multiply] expr * expr............................................................................................60 4.8.4.27 / [divide] expr / expt..................................................................................................60 4.8.4.28 % [modulo or remainder] expr % expr.....................................................................61 4.8.4.29 + [plus] expr + expr...................................................................................................61 4.8.4.30 - [minus] 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 exclusive OR] expr ^ expr........................................................................61 4.8.4.41 | [bitwise inclusive OR] expr | expr..........................................................................61 4.8.4.42 && [logical AND] expr && expr.............................................................................61 4.8.4.43 || [logical inclusive OR] expr || expr.........................................................................61 4.8.4.44 ?: [conditional expression] expr ? expr : expr..........................................................62 4.8.4.45 = [conventional assignment] lvalue = expr...............................................................62 4.8.4.46 *= [multiply and assign] lvalue *= expr...................................................................62 4.8.4.47 /= [divide and assign] lvalue /= expr........................................................................62 4.8.4.48 %= [modulo and assign] lvalue %= expr..................................................................62 4.8.4.49 += [add and assign] lvalue += expr..........................................................................62 4.8.4.50 -= [subtract and assign] lvalue -= expr.....................................................................62 4.8.4.51 <<= [shift left and assign] lvalue <<= expr..............................................................62 4.8.4.52 >>= [shift right and assign] lvalue >>= expr............................................................62 4.8.4.53 &= [AND and assign] lvalue &= expr......................................................................62 4.8.4.54 |= [inclusive OR and assign] lvalue |= expr..............................................................62 4.8.4.55 ^= [exclusive OR and assign] lvalue ^= expr...........................................................62 4.8.4.56 throw [throw exception] throw expr..........................................................................62 4.8.4.57 , [comma] expr , expr................................................................................................62 4.9 Expression Examples..................................................................................................62 4.10 Summary.....................................................................................................................64 4.11 Key Concepts..............................................................................................................65 4.11.1 Numeric Data Types........................................................................................................65 4.11.1.1 Integer........................................................................................................................65 4.11.1.2 Real............................................................................................................................65 4.11.1.3 Complex – supported via <complex>.......................................................................65 4.11.1.4 Character Data as Numeric Data...............................................................................65 4.11.2 Constants – use the const attribute..................................................................................65 4.11.3 Operators – 45 but effectively 57 with variants..............................................................65 4.11.4 Expressions – the order of expression evaluation is often undefined.............................65 4.12 Problems.....................................................................................................................65 5 Strings and other Data Types............................................................................68 5.1 Character Data or Strings...........................................................................................68 5.1.1 C Style strings..................................................................................................................68 5.1.1.1 Example 1..................................................................................................................68 5.1.1.2 Example 2..................................................................................................................68 5.1.1.3 strcpy(s1,s2)...............................................................................................................69 6 Contents 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 – <string>............................................................................................69 5.1.2.1 assignment..................................................................................................................69 5.1.2.2 character access..........................................................................................................69 5.1.2.3 comparison.................................................................................................................70 5.1.2.4 concatenation.............................................................................................................70 5.1.2.5 constructors................................................................................................................70 5.1.2.6 i/o...............................................................................................................................70 5.1.2.7 insertion......................................................................................................................70 5.1.2.8 iterators.......................................................................................................................70 5.1.2.9 length..........................................................................................................................70 5.1.2.10 removal.......................................................................................................................70 5.1.2.11 replacement................................................................................................................70 5.1.2.12 resize..........................................................................................................................70 5.1.2.13 search.........................................................................................................................70 5.1.2.14 Example 3..................................................................................................................71 5.1.3 Guidelines for use............................................................................................................71 5.2 Boolean or Logical Data............................................................................................71 5.3 Reference Variables....................................................................................................71 5.4 Enumeration Types.....................................................................................................72 5.5 Type Conversion........................................................................................................73 5.6 Scope...........................................................................................................................73 5.7 Void............................................................................................................................74 5.8 Memory and C++.......................................................................................................74 5.9 Summary.....................................................................................................................74 5.10 Key Concepts..............................................................................................................74 5.10.1 Data Types........................................................................................................................74 5.10.1.1 Sequence of characters as an array of char...............................................................74 5.10.1.2 Seqence of characters as a string...............................................................................74 5.10.1.3 Logical or boolean.....................................................................................................74 5.10.1.4 Reference Variable....................................................................................................74 5.10.1.5 Enumerated Types.....................................................................................................74 5.10.1.6 Void............................................................................................................................74 5.10.1.7 Type Conversion........................................................................................................74 5.10.2 Scope................................................................................................................................74 5.10.3 Memory............................................................................................................................74 5.11 Problems.....................................................................................................................75 6 Arrays, Vectors and Valarrays in C++.............................................................78 6.1 Old C Style arrays......................................................................................................78 6.1.1 One d example – Monthly Rainfall.................................................................................78 6.1.2 One d array – People's Weights.......................................................................................79 6.1.3 Two d Array – latitude and longitude.............................................................................79 6.1.4 Simple physics – voltage example...................................................................................80 6.1.5 Time Zone Example.........................................................................................................80 6.1.6 Array Initialisation...........................................................................................................81 6.1.7 Whole Array Manipulation..............................................................................................81 6.2 Vectors........................................................................................................................81 6.2.1 Example 1.........................................................................................................................81 6.2.2 Example 2 – subscript checking......................................................................................82 6.2.3 Example 3 – subscript checking with try/catch ..............................................................83 6.2.4 Example 4 – whole array assignment..............................................................................83 6.3 Valarrays.....................................................................................................................84 6.3.1 Example 1.........................................................................................................................84 6.4 Array Element Ordering in C++................................................................................85 6.5 Summary.....................................................................................................................85 6.6 Key Concepts..............................................................................................................85 6.6.1 Data Types........................................................................................................................85 6.6.1.1 array...........................................................................................................................85 6.6.1.2 vector..........................................................................................................................85 6.6.1.3 valarray.......................................................................................................................86 Contents 7 6.6.1.4 Associated control structure – for loop.....................................................................86 6.7 Problems.....................................................................................................................86 7 Control Structures...............................................................................................88 7.1 Compound Statement or Block..................................................................................88 7.2 Expression...................................................................................................................88 7.3 Boolean.......................................................................................................................88 7.4 if (expression) statement............................................................................................88 7.4.1 Example 1.........................................................................................................................88 7.4.2 Example 2.........................................................................................................................88 7.5 if (expression) statement; else statement;..................................................................89 7.5.1 Example 1.........................................................................................................................89 7.5.2 Example 2.........................................................................................................................89 7.6 switch (expression) statement....................................................................................89 7.6.1 Example 1.........................................................................................................................89 7.7 while (expression) statement......................................................................................90 7.7.1 Example 1.........................................................................................................................90 7.8 do statement while (expression);................................................................................90 7.8.1 Example 1.........................................................................................................................90 7.9 for (init-statement;expression 1; expression 2) statement.........................................91 7.9.1 Example 1.........................................................................................................................91 7.9.2 Example 2.........................................................................................................................92 7.10 break, continue, goto statements................................................................................92 7.11 Summary.....................................................................................................................92 7.12 Key Concepts..............................................................................................................93 7.12.1 logical expressions...........................................................................................................93 7.12.2 logical and relational operators........................................................................................93 7.12.3 A block of statements – { .;.;.}........................................................................................93 7.12.4 Control Statements...........................................................................................................93 7.12.4.1 the if expression statement........................................................................................93 7.12.4.2 the if expression statement else statement................................................................93 7.12.4.3 the switch statement...................................................................................................93 7.12.4.4 while expression statement........................................................................................93 7.12.4.5 do statement while expression...................................................................................93 7.12.4.6 the for () statement....................................................................................................93 7.12.4.7 break statement..........................................................................................................93 7.12.4.8 continue statement.....................................................................................................93 7.12.4.9 goto statement............................................................................................................93 7.13 Problems.....................................................................................................................93 7.14 Bibliography...............................................................................................................95 8 Pointers.................................................................................................................98 8.1 Example 1: Basic Pointer Usage................................................................................98 8.2 Example 2: Arrays and Pointers................................................................................99 8.3 Example 3: Pointers and Sentinels.............................................................................99 8.4 Example 4: Indiscriminate Pointer Usage................................................................100 8.5 Summary...................................................................................................................101 8.6 Key Concepts............................................................................................................101 8.6.1 * – pointer to..................................................................................................................101 8.6.2 & – address of................................................................................................................101 8.6.3 arrays and pointers.........................................................................................................101 8.7 Problems...................................................................................................................101 9 Functions ............................................................................................................104 9.1 Predefined Functions................................................................................................104 9.1.1 Trigonometric function usage........................................................................................104 9.1.2 Passing arguments of numeric type to standard maths functions.................................104 9.1.3 Functions in <maths.h>..................................................................................................105 9.2 User Defined Functions............................................................................................105 9.2.1 One d array as argument and one function....................................................................105 9.2.2 One function, vector as argument..................................................................................105 9.2.3 3 functions, one d array as argument.............................................................................106 9.2.4 Using 2d arrays as arguments........................................................................................107 9.2.5 Passing 2d dynamic arrays as arguments......................................................................108 9.2.6 Passing functions as arguments to other functions and procedure variables................109 8 Contents 9.3 Function Arguments.................................................................................................111 9.3.1 Swapping arguments – passing by address....................................................................111 9.3.2 Swapping arguments – passing by reference.................................................................111 9.3.3 Mayhem..........................................................................................................................112 9.4 C++ Standard Functions...........................................................................................112 9.5 Summary...................................................................................................................113 9.6 Key Concepts............................................................................................................113 9.6.1 Predefined functions.......................................................................................................113 9.6.2 User Defined functions..................................................................................................113 9.6.3 Basic syntax....................................................................................................................113 9.6.4 Parameter Passing...........................................................................................................113 9.6.4.1 Pass by value – copy made.....................................................................................113 9.6.4.2 Array as parameter – base address used.................................................................113 9.6.4.3 Pass by reference.....................................................................................................114 9.6.4.4 Pass by const reference............................................................................................114 9.7 Problems...................................................................................................................114 10 Classes – User Defined Data Types.................................................................116 10.1 Concrete Data Types................................................................................................116 10.1.1 Dates...............................................................................................................................116 10.1.2 Addresses........................................................................................................................117 10.1.3 Nested Data Types.........................................................................................................118 10.1.4 Reading user input using a singly linked list................................................................119 10.1.5 Reading user input using C++ style strings...................................................................120 10.2 Abstract Data Types.................................................................................................120 10.2.1 Dates...............................................................................................................................120 10.2.2 Addresses........................................................................................................................122 10.3 Sphere class..............................................................................................................124 10.3.1 translate_with_copy_sphere...........................................................................................127 10.3.2 translate_by_reference_sphere.......................................................................................127 10.3.3 translate_by_pointer_sphere...........................................................................................127 10.4 Constructors and Destructors...................................................................................128 10.4.1 Constructor/Destructor Example 1.................................................................................128 10.4.2 Constructor/Destructor Example 2.................................................................................129 10.4.3 Constructor/Destructor Example 3.................................................................................131 10.4.4 Constructor/Destructor Example 4.................................................................................132 10.4.5 Constructor/Destructor Example 5.................................................................................133 10.4.6 Constructor/Destructor Example 6.................................................................................136 10.4.7 Constructor/Destructor Recommendations....................................................................138 10.4.8 Memory Allocation and Deallocation – Leakage and Garbage Collection .................138 10.5 The C++ object Model – taken from the standard..................................................138 10.6 Summary...................................................................................................................139 10.7 Key Concepts............................................................................................................139 10.7.1 Concrete data types........................................................................................................139 10.7.1.1 Data public...............................................................................................................139 10.7.1.2 Functions public.......................................................................................................139 10.7.2 Abstract data types.........................................................................................................139 10.7.2.1 Data Private..............................................................................................................139 10.7.2.2 Functions public.......................................................................................................139 10.7.3 Constructors and Destructors.........................................................................................139 10.7.3.1 Simple constructor...................................................................................................139 10.7.3.2 Copy constructor......................................................................................................139 10.7.3.3 Overload assignment operator.................................................................................139 10.7.3.4 Destructor.................................................................................................................139 10.7.4 Basic Class Syntax.........................................................................................................139 10.8 Problems...................................................................................................................139 10.9 Bibliography.............................................................................................................140 11 Templates............................................................................................................142 11.1 Example 1 – Simple minimum................................................................................142 11.2 Example 2 – Hoare's Quicksort...............................................................................143 11.3 Other Issues..............................................................................................................144 11.4 Summary...................................................................................................................144 11.5 Key Concepts............................................................................................................144 Contents 9 11.5.1 Basic Syntax Example....................................................................................................144 11.6 Problems...................................................................................................................145 12 Operator and Function Overloading...............................................................148 12.1 Operator Overloading...............................................................................................148 12.1.1 Unary and Binary Operators..........................................................................................148 12.1.2 Operator Overloading 1: + , - , =...............................................................................149 12.1.3 Operator Overloading 2: [].............................................................................................153 12.1.4 Operator Overloading 3: ().............................................................................................153 12.1.5 Operator Overloading 4: << and >>.............................................................................154 12.1.6 Guidelines for Operator Overloading............................................................................155 12.2 Function Overloading...............................................................................................156 12.2.1 Overload Resolution.......................................................................................................156 12.2.2 Exact Matching...............................................................................................................156 12.2.3 Matching through promotion.........................................................................................158 12.2.4 Matching through Standard Conversion........................................................................158 12.2.5 Matching through User Defined Conversion.................................................................159 12.2.6 Guidelines for Function Overloading............................................................................160 12.3 Key Concepts............................................................................................................160 12.3.1 Function Overloading.....................................................................................................160 12.3.1.1 Functions distinguished by signature......................................................................160 12.3.2 Operator Overloading.....................................................................................................160 12.3.2.1 Unary operators........................................................................................................160 12.3.2.2 Binary operators.......................................................................................................160 12.3.2.3 Commutivity............................................................................................................160 12.4 Problems...................................................................................................................160 13 Virtual Functions and Abstract Data Types..................................................162 13.1 Virtual Function Example 1.....................................................................................162 13.2 Virtual Function Example 2.....................................................................................164 13.3 Summary...................................................................................................................166 13.4 Key Concepts............................................................................................................167 13.4.1 Virtual function..............................................................................................................167 13.4.2 Pure Virtual Function.....................................................................................................167 13.4.3 Private.............................................................................................................................167 13.4.4 Protected.........................................................................................................................167 13.4.5 Public..............................................................................................................................167 13.5 Problems...................................................................................................................167 14 Complete OO Example.....................................................................................170 14.1 Range Checked Array..............................................................................................170 14.1.1 Notes...............................................................................................................................172 14.1.1.1 Class array................................................................................................................172 14.1.1.2 Class checked_array................................................................................................172 14.2 Summary...................................................................................................................173 14.3 Where do I go next?.................................................................................................173 14.4 Problems...................................................................................................................173 15 Files and i/o........................................................................................................176 15.1 Numeric i/o: width and precision.............................................................................176 15.2 Numeric i/o: setting justification..............................................................................176 15.3 Numeric i/o: scientific notation................................................................................176 15.4 Numeric i/o: alternate number bases, octal and hexadecimal.................................177 15.5 File i/o: picking up the names from the command line..........................................177 15.6 File i/o: hard coded file names in constructors.......................................................178 15.7 File i/o: strings passed as arguments to constructors..............................................178 15.8 Summary...................................................................................................................178 15.9 Key Concepts............................................................................................................179 15.9.1 Setting the width............................................................................................................179 15.9.2 Setting the precision.......................................................................................................179 15.9.3 Justification.....................................................................................................................179 15.9.4 Reading from files..........................................................................................................179 15.9.5 Writing to files...............................................................................................................179 15.9.6 Picking up files from the command line.......................................................................179 15.9.7 Alternate number bases for numeric output..................................................................179 15.9.7.1 octal..........................................................................................................................179 10 Contents 15.9.7.2 decimal.....................................................................................................................179 15.9.7.3 hexadecimal.............................................................................................................179 15.10 Problems...................................................................................................................179 16 Errors and Exception Handling.......................................................................182 16.1 Linked List – Pascal.................................................................................................182 16.2 Linked List – Fortran 90..........................................................................................183 16.3 Linked List – C++, old C syntax.............................................................................183 16.4 Discussion.................................................................................................................185 16.5 Example 1 – Basic Syntax.......................................................................................185 16.6 Example 2 – Exception raised in a function............................................................185 16.7 Example 3 – Function with Exception Specification..............................................186 16.8 Example 5 – Exceptions and constructors and destructors.....................................188 16.9 Key Concepts............................................................................................................188 16.9.1 Basic Syntax...................................................................................................................188 16.10 Problems...................................................................................................................188 17 The Standard Template Library.....................................................................190 17.1 Library Organisation................................................................................................190 17.1.1 Some basic terminology.................................................................................................190 17.2 Containers.................................................................................................................190 17.2.1 vector..............................................................................................................................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 Miscellaneous Operations........................................................................................192 17.5 Constructors..............................................................................................................193 17.6 Algorithms................................................................................................................193 17.6.1 Non modifying sequence operations..............................................................................193 17.6.2 Modifying sequence operations.....................................................................................193 17.6.3 Sorted sequences............................................................................................................194 17.6.4 Set algorithms.................................................................................................................194 17.6.5 Heap operations..............................................................................................................195 17.6.6 Minimum and maximum................................................................................................195 17.6.7 Permutations...................................................................................................................195 17.7 Strings.......................................................................................................................195 17.8 Numerics...................................................................................................................195 17.8.1 complex..........................................................................................................................196 17.8.2 valarray...........................................................................................................................196 17.8.3 numeric...........................................................................................................................196 17.9 Complete list of C++ template classes....................................................................197 17.10 Useful Sources..........................................................................................................197 17.11 Summary...................................................................................................................197 17.12 Problems...................................................................................................................197 18 Miscellanea.........................................................................................................200 18.1 C++ as a Programming Language...........................................................................200 18.2 The Standard.............................................................................................................200 18.3 C++ Standard Definitions.........................................................................................201 18.3.0.1 argument...................................................................................................................201 18.3.0.2 dynamic type............................................................................................................201 18.3.0.3 implementation defined behaviour..........................................................................201 18.3.0.4 implementation limits..............................................................................................201 18.3.0.5 multibyte character..................................................................................................201 18.3.0.6 parameter..................................................................................................................201 18.3.0.7 signature...................................................................................................................201 18.3.0.8 static type.................................................................................................................201 18.3.0.9 undefined behaviour................................................................................................201 18.3.1 C++ Implementation Options: Free vs Hosted..............................................................202 18.3.2 The C++ Memory Model, taken from the standard......................................................202 Contents 11 18.4 Coda..........................................................................................................................202

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.