Lecture Programming in C++ - Chapter 18: Miscellaneous topics. On completion of this chapter students will know how to: Benefits of multiple file organization, how to use header files, how to use bitwise operators, how to work with binary files.
Chapter 18 – Miscellaneous Topics Multiple File Programs Makes possible to accommodate many programmers working on same project More efficient to manage large amount of source code Can incorporate class libraries in separate files from independent vendors Lesson 18.1 Using Header Files Header file is source code file Inserted into program source code before program compiled Can create own header files Option of having separate files for class definitions, implementations, and main – Placed in different header files and use #include Lesson 18.1 Working With Header Files Creating header file – Source code file with extension .h Including header files – #include “name.h” Header file contents – Put each class definition and each class implementation in separate files Lesson 18.1 Header File Contents Three lines needed – Two at top and one at bottom – Messages to preprocessor and not compiled #ifndef name_h #define name_h … source code … #endif Lesson 18.1 Preprocessor Directives #ifdef True if identifier defined in previous preprocessor #if True block executed if constant expression following is true #else Forms false block for #if #endif Marks end of conditional inclusion #ifndef True if identifier NOT defined previously #elif Works similar to “else if” #undef Undefines previously defined identifier Lesson 18.1 Another Method More efficient connecting files with IDE – Integrated Development Environment Save correctly working files as object code Link after all changes have been recompiled Lesson 18.1 Bitwise Manipulations Very lowlevel operations Manipulate individual bits (1s and 0s) C++ provides bitwise operators – Six operators Bit values – set if value is 1 – clear if value is 0 Lesson 18.2 Why Use Bitwise Operators To control peripherals To use as flags – Use individual bits instead of integers File encryption Array handling for any array that has members with only two possible states Lesson 18.2 Bitwise Operators AND Inclusive OR Exclusive OR & | ^ – Also called XOR Complement Right shift Left shift Lesson 18.2 ~ >> >