Transcription of Advanced Programming with C++
1 Advanced Programming with C++Tim Love,Cambridge University,C++ notes for engineers,adapted by Hugh Murrell,for Computer Science 12, 2011 AbstractThis document aims to provide people who have done an introductoryprogramming course the skills required to write C++ A second Programming language .. Web pages and Text books .. The UKZN Teaching labs .. Microsoft Windows .. Linux .. Major project for 2010 ..52 C++ Programming Keywords .. Built-in Types and Enumerations .. Operators .. Selection.
2 If .. switch .. Loops .. while, do .. for .. Aggregation .. arrays .. structures and classes .. Pointers .. Functions .. Declarations .. Memory Allocation .. Scope and Namespaces .. Exceptions ..151 CONTENTSCONTENTS3 Object-orientated Classes .. Class member privacy .. Friend classes and functions .. A complete example .. Derived classes .. Static members .. Const members .. Static Const members .. Overriding behaviour .. Templates ..194 The Standard Iterators.
3 Strings .. Iterators .. Size .. Routines .. vector .. Queues .. list .. map .. bitset .. valarray .. Algorithms .. Set algorithms .. Using member functions .. Predicates .. Creating predicates .. Adapters ..325 More on Virtual members .. Abstract Classes .. Redefining operators .. A class definition example .. Redefining [ ] .. Redefining () .. Redefining->..386 Simple I/O .. Formatting .. Stream Iterators .. Output of User-Defined types .. Input of User-Defined types.
4 String streams ..417 Odds and Casts .. Limits .. Performance .. Maths .. Hardware Interfacing: bit operations and explicit addresses ..462 Advanced Programming with C++1 Using routines from other languages .. C .. Fortran .. bad habits to give up .. Non-essentials (Things you probably don t need to know yet) .. virtual inheritance .. typeid .. virtual destructors .. Auto pointers .. operator functions .. union .. Non-type template parameters ..508 Program Style .. Makefiles.
5 Debugging .. Common Difficulties and Mistakes ..539 CImg, an image processing library for C++ library structure .. , Hello world .. compiling aCImgapplication ..5610 Glossary5711 References5812 Program , A binary search tree for .. , Atemplatebinary search ..611 A second Programming languageThis document is aimed at those who have done two years of Java Programming atUKZN. These Java courses introduced all loop constructions and many and data structures were also introduced. All of this was done in an objectoriented setting using Java some issues such as software housekeeping ( Makefiles) were glossedover and hidden behind IDEs.
6 Others, such as templates and many things werenot touched upon at all. Some of these newer things are easy conceptually, andfundamental to modern C++ Programming , though implementation details can becomplicated. The pay-off comes when you use the Standard Library - a librarywhich exploits Advanced C++ features to make life easy for the Programming with C++1 INTRODUCTIONC++ can be used in various ways: procedural (like Fortran or C) or Object-orientated (like Java). This document covers the techniques to support each ofthese Programming paradigms, dealing with Namespaces Using classes to build new classes - inheritance Abstract Classes and Templates Using the Standard Library Techniques useful when writing big, project-sized Web pages and Text booksTo aid readability, the examples in the text are mostly code fragments.
7 Full exam-ples will be posted online at the module s homepage: murrellh/apSome complete program examples are given in the in the appendix to this of these have been adapted from examples on theIra Pohlwebsite: pohl/Many more complete examples are posted few Programming exercises are suggested in this document. Any computing bookwill offer otherwise stated, the page numbers in the margin refer to:Bjarne Stroustrup,The C++ Programming Language(third edition), AT&T, can get this book from the library or buy your own copy from are also many free books on C++ on the web.
8 For extra reading materialyou can download from the Advanced Programming webpage:Frank Brokken,C++ Annotations(version ) The UKZN Teaching labsAdvanced Programming students will either useG21or the honours lab. Studentscan code under Microsoft Windows or Microsoft WindowsHere your compiler will beMinGwand your IDE will beCode::Blocks. TheCodeBlocksManualcan be downloaded from the course Programming with C++2 C++ Programming LinuxHere your compiler isg++and your IDE Major project for 2010At the end of this module you will construct a program that will be written writtenusing a large library of C++ this major project you will try to simulate water flowing over a terrain.
9 As C++doesn t have any graphics routines for this project you will make use of the imageprocessing libraryCImgto do our graphics program must initialise aterrainand then dump water on it. At each iterationthe water will flow downhill and the terrain with its current water profile will C++ Programming KeywordsYou haven t been introduced to all of these keywords before. Many you ll never re included here for completeness because you can t use them for variablenames, andeq asm auto bitand bitorbool break case catch char classcompl const constcast continue default deletedo double dynamiccast else enum explicitexport extern false float for friendgoto if inline int long mutablenamespace new not noteq operator ororeq private protected public register reinterpretcastreturn short signed sizeof
10 Static staticcaststruct switch template this throw truetry typedef typeid typename union unsignedusing virtual void volatile wchart whilexor Built-in Types and EnumerationsThere are integral types (char,short,int,long) which can be intended to holdsigned (the default) or unsigned values. So for example unsigned char c createsa one-byte variable big enough to hold any integer from 0 to 255. There are alsofloating point types (float,double,long double).