COMP-446-Notes - Software Development with C+ Lecture Notes...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Software Development with C ++ Lecture Notes Peter Grogono These notes may be copied for students who are taking either of these courses: COMP 6441 Advanced Programming Practices COMP 446 Designing Programs with C++ . © Peter Grogono, 2005 Department of Computer Science and Software Engineering Concordia University, Montreal, Quebec
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CONTENTS ii Contents 1 Getting Started 1 1.1 Hello, world! ...................................... 1 1.2 Compiling C ++ ......................................... 5 1.2.1 The compilation process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.2.2 Compiling with Visual C ++ .NET . ......................... 7 1 .2 .3 Comp i l ingw i thgcc . ................................. 8 1.3 Hello, world! —TheDe ta i l s............................. 8 1 .4 Name space s........................................... 10 1 .5 S t r ing s .............................................. 12 1 .6 AP re t tyF rame . ........................................ 14 2 Testing and Looping 17 2 .1 Cond i t ion s............................................ 17 2 .2 Cond i t iona lExp s s s ................................... 19 2 .3 Loop s............................................... 2.4 Example: Computing the Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2 .5 Coun t ing. ............................................ 24 2 .6 LoopDe s ign. .......................................... 25 2 .6 .1 Coun t ingL ine s..................................... 26 2.6.2 Finding Roots by Bisection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.6.3 Maximum Subsequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2 .7 A s se r t 32 2 .8 OhNo t ion. 34 3 Batches of Data 36 3 .1 Lva lue sandRva s...................................... 36 3 .2 Pa s s ingA rgumen t s ...................................... 3 .3 Read ingDa ta . 39 3 .4 Read ingf roma± le . ..................................... 42 3 .4 .1 W r i t ingtoa± le. .................................... 43 3 .2 S t reamS te 44 3.4.3 Summary of file streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3.5 Storing Data: STL Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 3 .5 .1 Summa ryo fSTL. ................................... 51
Background image of page 2
CONTENTS iii 4 Application: Grading a class 54 4 .1 P rob lemS ta temen t ...................................... 54 4 .2 F i r s tve r s ion. .......................................... 4 .3 P rog ramS t ruc tu re. ...................................... 62 4 .3 .1 Heade rF i le s....................................... 4.3.2 Implementation Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 4.4 Structuring the grading program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 4.4.1 Translation unit Student ............................. 65 4.4.2 Translation unit median .............................. 4.4.3 Translation unit for the main program . . . . . . . . . . . . . . . . . . . . . . 65 4 .5 Dependenc ie s.......................................... 66 4 .6 S t r ing sandcha rac te r s .................................... 69 5 Pointers and Iterators 72 5.1 Pointers and Dynamic Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 5 .1 .1 S tackA l loca t ion . ................................... 73 5 .2 HeapA l t 75 5 .3 Ano teonnu l lpo in r s................................ 76 5 .2 I ra to r s ............................................. 77 5 .2 .1 K ind so fi r.................................... 78 5 .2 U s ingi r s..................................... 80 5 .3 RangeFunc t ion s.................................... 82 6 Template Programming 84 6 .1 Temp la tefunc t s ...................................... 84 6 .1 TypePa rame r 6 .2 M i s s ingfunc t s ................................... 87 6 .3 Conve r s ionfa i lu .................................. 88 6.1.4 Non-type Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 6 .2 Temp tec s se s........................................ 90 6.2.1 A template class for vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 6.2.2 class or typename ? ............................... 93 6 .3 Comp l ica t s ......................................... 94 6.3.1 Compiling template code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CONTENTS iv 6.3.2 Template return types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 6.3.3 Template Specialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 6 .3 .4 De fau l tA rgumen t s .................................. 98 6 .4 T reet rave r sa l.......................................... 99 6 .4 .1 O rgan iz ingthecode. ................................. 107 7 Designing classes 109 7 .1 Cons t ruc to r s .......................................... 111 7 .2 De s t r............................................ 116 7 .3 Ope ra r s ............................................ 117 7.3.1 Assignment ( operator= ) ............................. 7 .2 A r i thme t ic. ....................................... 120 7 .3 Compa r i son. ...................................... 122 7 .4 Inpu tandou tpu t ................................... 124 7 .4 Conve r s ion s........................................... 125 7 .5 Acce s so r 126 7 .6 Mu ta r s............................................. 127 7 .7 Odd sandend s......................................... 128 7.7.1 Indexing ( operator[] ) .............................. 7.7.2 Calling ( operator() ) ............................... 129 7.7.3 Explicit constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 7 .7 .4 F r iend s.......................................... 131 8 Inheritance and templates 134 8 .1 Wha ti sinhe r i tance ?...................................... 134 8 .1 .1 S l ic ing . ......................................... 137 8.1.2 Constructors and destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 8 s ign ingaba sec la s s .................................... 138 8.2.1 virtual func t s ................................. 139 8.2.2 Don’t redefine non-virtual functions . . . . . . . . . . . . . . . . . . . . . . . 142 8.2.3 protected a t t r ibu te s ............................... 143 8.2.4 A base class for bankers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 8 .3 De s ingade r ivedc s 146 8.3.1 A derived class for bankers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 8.3.2 Additional base class functions . . . . . . . . . . . . . . . . . . . . . . . . . . 150 8.4 Designing a template class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 8 .5 No teoni r s........................................ 157
Background image of page 4
CONTENTS v 9 When things go wrong 161 9.1 Design by contract versus defensive coding . . . . . . . . . . . . . . . . . . . . . . . 161 9 .2 Excep t ion s............................................ 163 9 .3 Manag
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 255

COMP-446-Notes - Software Development with C+ Lecture Notes...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online