Chapter 18 – Miscellaneous Topics
Jan 02, 2016
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 low-level 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 ~ (unary operator) Right shift >> Left shift <<
Lesson 18.2
Hexadecimal Notation
Lesson 18.2
Hexadecimal Binary Bit Pattern0 00001 00012 00103 00114 01005 01016 01107 01118 1000
bitwise And / inclusive OR
Work similar to counterparts && and ||
1 & 1 = 1, all others evaluate to 01 & 0 = 0, 0 & 1 = 0, 0 & 0 = 0
0 | 0 = 0, all others evaluate to 10 | 1 = 1, 1 | 0 = 1, 1 | 1 = 1
Lesson 18.2
Example (Hex 3 with Hex 6)
Lesson 18.2
Bitwise AND Bitwise ORhex 3 0 0 1 1 hex 3 0 0 1 1 & |hex 6 0 1 1 0 hex 6 0 1 1 0 = =
&&&& | | | |
0 0 1 0 0 1 1 1hex 2 hex 7
Complement Operator ~
Reverses all bits of operand
~(1010) = 0101
Lesson 18.2
Bitwise Exclusive OR ^
Evaluate to 0 if both operands the same
0 ^ 0 = 0 1 ^ 1 = 0
0 ^ 1 = 1 1 ^ 0 = 1
Lesson 18.2
Bitwise Shift Operators
Move all bits in cell either right or left directions
Add clear bits in shift
1 0 1 1 0
0
>>
1
<<
1 0 1 1 0
0 1
Lesson 18.2
Comments
Can shift more than one bit Bitwise operators can only be used on
integer data types All systems do NOT use same bitwise
representations– May get different results
Lesson 18.2
Binary Files
Efficient since do not have to convert from ASCII
Saves execution time Not “human friendly”, text files better Cannot be used with editing software Cannot be printed
Lesson 18.3
Opening Output File for Binary
ofstream out_ob (“name”, ios :: out | ios :: binary);
Lesson 18.3
programmer-chosen output file object namefilename
Opening Input File for Reading in Binary
ifstream in_ob (“name”, ios :: in | ios :: binary);
programmer-chosen input file object namefilename
Lesson 18.3
Writing to File in Binary
out_ob.write (reinterpret_cast<char*> (address), num_bytes);
Lesson 18.3
Programmer-chosen output file object nameAddress of beginning of memory cells being written to file
Number of bytes to be copied from memory to file
Reading From a Binary File
in.ob.read (reinterpret_cast<char*> (address), num_bytes);
Lesson 18.3
Programmer-chosen input file object nameAddress of beginning of memory cells to which bytes are copied
Number of bytes to be copied from file to memoryOperator that makes C++ interpret address to represent beginning of an array of characters
Closing the File
Close file after writing Can open later for reading again
Lesson 18.3
outfile.close ( )
Summary
Benefits of multiple file organization How to use header files How to use bitwise operators How to work with binary files