Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 18 – Miscellaneous Topics. Multiple File Programs u Makes possible to accommodate many programmers working on same project u More efficient to.

Similar presentations


Presentation on theme: "Chapter 18 – Miscellaneous Topics. Multiple File Programs u Makes possible to accommodate many programmers working on same project u More efficient to."— Presentation transcript:

1 Chapter 18 – Miscellaneous Topics

2 Multiple File Programs u Makes possible to accommodate many programmers working on same project u More efficient to manage large amount of source code u Can incorporate class libraries in separate files from independent vendors Lesson 18.1

3 Using Header Files u Header file is source code file u Inserted into program source code before program compiled u Can create own header files u Option of having separate files for class definitions, implementations, and main –Placed in different header files and use #include Lesson 18.1

4 Working With Header Files u Creating header file –Source code file with extension.h u Including header files –#include “name.h” u Header file contents –Put each class definition and each class implementation in separate files Lesson 18.1

5 Header File Contents u 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

6 Preprocessor Directives #ifdefTrue if identifier defined in previous preprocessor #ifTrue block executed if constant expression following is true #elseForms false block for #if #endifMarks end of conditional inclusion #ifndefTrue if identifier NOT defined previously #elifWorks similar to “else if” #undefUndefines previously defined identifier Lesson 18.1

7 Another Method u More efficient connecting files with IDE –Integrated Development Environment u Save correctly working files as object code u Link after all changes have been recompiled Lesson 18.1

8 Bitwise Manipulations u Very low-level operations u Manipulate individual bits (1s and 0s) u C++ provides bitwise operators –Six operators u Bit values –set if value is 1 –clear if value is 0 Lesson 18.2

9 Why Use Bitwise Operators u To control peripherals u To use as flags –Use individual bits instead of integers u File encryption u Array handling for any array that has members with only two possible states Lesson 18.2

10 Bitwise Operators u AND& u Inclusive OR| u Exclusive OR^ –Also called XOR u Complement~(unary operator) u Right shift>> u Left shift<< Lesson 18.2

11 Hexadecimal Notation Lesson 18.2 HexadecimalBinary Bit Pattern 00000 10001 20010 30011 40100 50101 60110 70111 81000

12 bitwise And / inclusive OR u Work similar to counterparts && and || u 1 & 1 = 1, all others evaluate to 0 1 & 0 = 0, 0 & 1 = 0, 0 & 0 = 0 u 0 | 0 = 0, all others evaluate to 1 0 | 1 = 1, 1 | 0 = 1, 1 | 1 = 1 Lesson 18.2

13 Example (Hex 3 with Hex 6) Lesson 18.2 Bitwise AND Bitwise OR hex 3 0 0 1 1 & | hex 6 0 1 1 0 = = & &&& | | | | 00100111hex 2hex 7

14 Complement Operator ~ u Reverses all bits of operand ~(1010) = 0101 Lesson 18.2

15 Bitwise Exclusive OR ^ u Evaluate to 0 if both operands the same 0 ^ 0 = 01 ^ 1 = 0 0 ^ 1 = 11 ^ 0 = 1 Lesson 18.2

16 Bitwise Shift Operators u Move all bits in cell either right or left directions u Add clear bits in shift 1 0 1 1 0 0 >> 1 << 1 0 1 1 0 0 1 Lesson 18.2

17 Comments u Can shift more than one bit u Bitwise operators can only be used on integer data types u All systems do NOT use same bitwise representations –May get different results Lesson 18.2

18 Binary Files u Efficient since do not have to convert from ASCII u Saves execution time u Not “human friendly”, text files better u Cannot be used with editing software u Cannot be printed Lesson 18.3

19 Opening Output File for Binary ofstream out_ob (“name”, ios :: out | ios :: binary); Lesson 18.3 programmer-chosen output file object name filename

20 Opening Input File for Reading in Binary ifstream in_ob (“name”, ios :: in | ios :: binary); programmer-chosen input file object name filename Lesson 18.3

21 Writing to File in Binary out_ob.write (reinterpret_cast (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

22 Reading From a Binary File in.ob.read (reinterpret_cast (address), num_bytes); Lesson 18.3 Programmer-chosen input file object name Address of beginning of memory cells to which bytes are copied Number of bytes to be copied from file to memory Operator that makes C++ interpret address to represent beginning of an array of characters

23 Closing the File u Close file after writing u Can open later for reading again Lesson 18.3 outfile.close ( )

24 Summary u Benefits of multiple file organization u How to use header files u How to use bitwise operators u How to work with binary files


Download ppt "Chapter 18 – Miscellaneous Topics. Multiple File Programs u Makes possible to accommodate many programmers working on same project u More efficient to."

Similar presentations


Ads by Google