Top Banner
Chapter 18 – Miscellaneous Topics
24

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

Jan 02, 2016

Download

Documents

Basil Curtis
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 18 – Miscellaneous Topics. Multiple File Programs u Makes possible to accommodate many programmers working on same project u More efficient to.

Chapter 18 – Miscellaneous Topics

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Bitwise Operators

AND & Inclusive OR | Exclusive OR ^

– Also called XOR Complement ~ (unary operator) Right shift >> Left shift <<

Lesson 18.2

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

Hexadecimal Notation

Lesson 18.2

Hexadecimal Binary Bit Pattern0 00001 00012 00103 00114 01005 01016 01107 01118 1000

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

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

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

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

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

Complement Operator ~

Reverses all bits of operand

~(1010) = 0101

Lesson 18.2

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

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

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

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

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

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

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

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

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

Opening Output File for Binary

ofstream out_ob (“name”, ios :: out | ios :: binary);

Lesson 18.3

programmer-chosen output file object namefilename

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

Opening Input File for Reading in Binary

ifstream in_ob (“name”, ios :: in | ios :: binary);

programmer-chosen input file object namefilename

Lesson 18.3

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

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

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

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

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

Closing the File

Close file after writing Can open later for reading again

Lesson 18.3

outfile.close ( )

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

Summary

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