1 / 51

Advanced Topics

13. Advanced Topics. Object-Oriented Programming Using C++ Second Edition. 13. Objectives. In this chapter, you will learn: About the binary system Why computers use the binary system How to use individual bits to store data How to combine bit fields to hold values larger than 1

callia
Télécharger la présentation

Advanced Topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 13 Advanced Topics Object-Oriented Programming Using C++ Second Edition

  2. 13 Objectives • In this chapter, you will learn: • About the binary system • Why computers use the binary system • How to use individual bits to store data • How to combine bit fields to hold values larger than 1 • How to convert a group of bit fields to an integer value

  3. 13 Objectives • In this chapter, you will learn: • How to use the logical bitwise AND operator with a mask • How to use the logical bitwise OR operator • How and why to shift bits • About recursion • How to use a recursive function to sort a list

  4. 13 Understanding the Binary System • Every piece of data stored on every computer in the world is stored as a series of 0s and 1s • This system of storage, called the binary system, has been used since the earliest computers were created because it is the cheapest way to store data • The numbers you are accustomed to using are organized in the decimal system • The decimal system is so named because the prefix “dec” means 10 and the decimal system uses 10 digits: 0 through 9

  5. 13 Representation of a Few Decimal Numbers

  6. 13 Understanding the Binary System • Constructing values in the binary system works the same way as in the decimal system, except that you have only two symbols to use, so each successive column represents a value only two times greater than the column to its right • You can construct any binary number’s decimal value by adding the column values of those columns that contain a 1

  7. 13 Representation of a Few Binary Numbers

  8. 13 Understanding Why Computers Use the Binary System • Every computer system uses a code to represent values, although different systems use different codes • Many personal computers use a system called ASCII (American Standard Code for Information Interchange), in which eight binary digits are used to represent a character • Many mainframe computer systems use a separate code named EBCDIC (Extended Binary Coded Decimal Interchange Code), in which, for example, the character A is 11000001

  9. 13 Understanding Why Computers Use the Binary System • Some C++ compilers also support Unicode, which is a 16-bit coding scheme • Computer professionals use the term bit as shorthand when they refer to a single binary digit • Eight binary digits are called a byte, which on many systems is the amount of storage required to store a single character such as an alphabetic letter or punctuation mark

  10. 13 Using Individual Bits to Store Data • You have several options for storing the values 0 and 1 within fields in a class you create • For example: • You can store 0 or 1 in a double field • You can store 0 or 1 in an integer field • You can store 0 or 1 in a single bit field • When you examine the output in Figure 13-3, you see that the DriverUsingInts object requires 20 bytes of storage—four bytes for each of its five integer fields

  11. 13 Driver UsingInts and DriverUsingBits Classes

  12. 13 Using Individual Bits to Store Data

  13. 13 Using Individual Bits to Store Data • In the steps shown on pages 494 to 496 of the textbook, you create a Driver class with two bit fields, and declare some objects that hold values in the fields

  14. 13 Combining Bit Fields to Hold Values Larger than 1 • When you want to store a 0 or 1, you can use a single bit • If you want to store larger values, you need more bits • Table 13-2 shows that you need at least two bits of storage to represent the value 2 or 3 • In the next set of steps referred to on pages 496 and 497 of the textbook, assume that you want to include a field within the Driver class you created earlier, and that you allow only three or fewer vehicles per Driver

  15. 13 Output of Driver2 Program

  16. 13 Converting a Group of Bit Fields to an Integer Value • When you store 0s and 1s in the bit fields for an Employee object, you can think of them as individual fields or as a single unit

  17. 13 Representation of Employee with Three Selected Attributes

  18. 13 The Employee Class

  19. 13 Converting a Group of Bit Fields to an Integer Value • Figure 13-9 contains a main() program that uses the Employee class shown in Figure 13-8 • An array of Employee objects is declared and, in a for loop, each Employee is assigned values from the keyboard

  20. 13 main() Program Using the Employee Class

  21. 13 Output of a Typical Run of the Employee Program

  22. 13 Using the Logical Bitwise AND Operator with a Mask • Comparing an object’s bit fields to a decimal value is efficient when you want to find objects with a specific stored pattern • C++ provides you with a special set of operators, called bitwise operators, that you can use in situations like the one just described • Bitwise operators let you manipulate individual bits of integer or character values • One such operator is the logical bitwise AND operator, which is written as a single ampersand (&)

  23. 13 Using the Logical Bitwise AND Operator with a Mask

  24. 13 Using the Logical Bitwise AND Operator with a Mask • A mask is a value whose only purpose is to filter values from other variables • Figure 13-3 shows three possible Employee objects and the result that ensues when you mask their bit values with an insuranceMask set to 00001110

  25. 13 Using the Logical Bitwise AND Operator with a Mask

  26. 13 Using the Logical Bitwise AND Operator with a Mask • In the program in Figure 13-14, using the mask improves program efficiency; you can select records of Employees who take all three types of insurance by making a single logical comparison

  27. 13 Using the Logical Bitwise AND Operator with a Mask • Figure 13-15 shows a typical run of the program

  28. 13 Using the Logical Bitwise OR Operator • The logical bitwise OR operator (|) compares the bits of its two operands, and produces a result in which each bit is a 1 if either of the operand’s corresponding bits is a 1 • Figure 13-16 shows an example that uses the logical bitwise OR operator • One way to use the logical bitwise OR is to turn on specific bits within an object

  29. 13 Using the Logical Bitwise OR Operator

  30. 13 Using the Logical Bitwise OR Operator

  31. 13 Using the Logical Bitwise OR Operator • Figure 13-18 shows a function you can add to the Employee class to assign an unsigned integer’s bit values to an Employee’s bit fields

  32. 13 Using the Logical Bitwise OR Operator • The program in Figure 13-19 establishes a newBenefitMask with a value of 66

  33. 13 Output of a Typical Execution of Employee3 Program

  34. 13 Shifting Bits • You can use the bitwise left shift operator (<<) to shift bits to the left and the bitwise right shift operator (>>) to shift bits to the right • For example, Figure 13-21 shows the bit representation of a byte, and the results after shifting one bit to the left, equivalent of using the expression result = value <<1;

  35. 13 Shifting Bits • The program in Figure 13-22 shows two ways of multiplying 5 by 2 to the third power • In the first part of the program, a variable named num is set to 5, and a loop is executed to multiply 2 by 2 by 2 to determine 2 to the third power before multiplying by 5 and displaying the answer • In the second part of the program, num is reset to 5 and the bitwise left shift operator performs the same task in one statement

  36. 13 Shifting Bits

  37. 13 Understanding Recursion • A function that calls itself a recursive function • Many programming languages do not allow a function to call itself, but C++ programmers can use recursion to produce some interesting effects • Figure 13-24 shows a simple example of recursion • When the main() program in Figure 13-24 calls the infinity() function, the stack receives the address to which the program should return within main(), but this program never returns to main()

  38. 13 Shifting Bits

  39. 13 Shifting Bits • Figure 13-26 shows a program that calls a recursive function which computes the sum of every integer, from 1 up to and including the function’s argument value

  40. 13 Output of Recursion2 Program

  41. 13 Nonrecursive Program that Computes a Sum

  42. 13 Using a Recursive Function to Sort a List • It is possible to quickly sort a list using a recursive sorting function • To use this sorting method, you employ a “divide and conquer” technique in which you select a point within a list that represents a middle position, and then divide the list into two sublists • Then you swap the positions of pairs of values until all the values in the first sublist are less than the value in the middle position, and all the values in the second sublist are more than the value in the middle position • Subsequently, each sublist is divided in half and rearranged so all the low values are in one sublist and all the high values are in the other

  43. 13 Using a Recursive Function to Sort a List

  44. 13 Using a Recursive Function to Sort a List • Figure 13-31 shows the splitList() function • The sort() function in Figure 13-32 is the recursive function • It accepts a list of numbers and a start and stop position

  45. 13 Using a Recursive Function to Sort a List

  46. 13 Using a Recursive Function to Sort a List • In the steps outlined on pages 520 and 521 of the textbook, you create a recursive drawing function so you can experiment with the way recursion works

  47. 13 Output of a Typical Run of the DrawingRecursion Program

  48. 13 Summary • Every piece of data stored on a computer uses the binary system • The only digits used are 0 and 1 • Computers use the binary system because two-way switches are the cheapest form of storage; most systems store eight bits in a byte • You can use a single bit for a field that holds a 0 or 1

  49. 13 Summary • When you want to store a small value that requires more storage space than a bit, but not as much as a byte or an integer, you increase the number following the colon in the declaration of the bit field • When you store 0s and 1s in the bit fields for an object, you can think of them as individual fields, or you can think of them as a single unit and assign the binary numbering system column values to each column

  50. 13 Summary • C++ provides you with bitwise operators that let you manipulate individual bits of integer or character values • The logical bitwise OR operator (|) compares the bits of its two operands and produces a result in which each bit is a 1 if either of the operand’s corresponding bits is a 1

More Related