Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming COP3330 / CGS5409

Similar presentations


Presentation on theme: "Object Oriented Programming COP3330 / CGS5409"— Presentation transcript:

1 Object Oriented Programming COP3330 / CGS5409
Recitation Week 12 Object Oriented Programming COP3330 / CGS5409

2 Today’s Recitation Exception Handling Bitwise Operators

3 Exception Handling Many erroneous situations could occur during program execution Usually related to things like user input Exception Handling is a type of error- checking, available in many programming languages

4 Exception Handling Exception - some sort of problem or error that occurs during a program's execution Exception handler - a piece of code that resolves an exception situation Typical error-checking often intermixed with the tasks of a program (if-statements, etc) Exception handlers are intended to be separate from the main tasks

5 Why Exception Handling?
Exception handling can improve a program's fault tolerance Exception handlers are separate from main tasks of a program Can improve readability and modifiability

6 Why Exception Handling?
This doesn't mean that exception handlers should be used in all cases! Sometimes conventional error-checking is more appropriate Exception handling best for problems that occur infrequently

7 When to use Exception Handlers
Exception handling is good for situations in which the error doesn't need to be handled in the same block in which it occurred Errors that will result in termination of the program, for instance, would fall into this category

8 Using Exception Handlers
Reserved words in C++: try, throw, catch try blocks Consists of keyword try and a block of code { } inside set braces Encloses the statements that might cause exceptions catch blocks One or more catch blocks follow a try block. (also code enclosed in { } set braces) Each catch block is an exception handler A catch block has a single parameter (with type listed)

9 Using Exception Handlers
If an exception occurs in a try block The try block immediately ends The program attempts to match the exception to one of the catch handlers (based on type of item thrown) If a match is found, the code in the catch block executes Maximum of one catch block will be matched, if any Program control resumes after the last catch block If no exceptions occur in a try block, the catch blocks are skipped!

10 Using Exception Handlers
The point where an exception occurs is called the throw point Keyword throw used to "throw" a specific kind of exception to be caught In C++, there is a standard library with pre-built exception classes. The primary base class is called exception, and comes from here: #include <exception> using std::exception;

11 Simple Exception Example
#include <iostream> using namespace std; int main() { int cookies, people; double cpp; try { cout << "Enter number of people: "; cin >> people; cout << "Enter number of cookies: "; cin >> cookies; if (cookies == 0) throw people; else if (cookies < 0) throw static_cast<double>(people);

12 Simple Exception Example cont.
cpp = cookies/static_cast<double>(people); cout << cookies << " cookies.\n" << people << " people.\n" << "You have " << cpp << " cookies per person.\n"; } catch(int e) { cout << e << " people, and no cookies!\nGo buy some cookies!\n"; catch(double t) cout << "Second catch block type double -- do we reach it?\n"; cout << "End of program.\n"; return 0;

13 Bitwise Operators Memory is made up of bits and bytes
A bit is the smallest unit of storage in a computer. It stores a 0 or a 1. A byte consists of 8 bits, and is special because it is usually the smallest unit of directly addressable storage. This means - it is the smallest item that we can create a variable out of. Addresses in memory are usually applied to bytes.

14 Bitwise Operators The smallest built-in data type is the char, which on most systems today is 1 byte. So... what if we want to access individual bits? Is this possible? Yes -- but not directly. We must use the bitwise operators to act at the bit level. Caution: Bitwise operations may be machine- dependent! (Big/Little ENDIAN)

15 Bitwise Operators Operator Name Arity Description & Bitwise AND Binary
Similar to the && operator, but on a bit-by-bit basis.  Bits in the result set to 1 if corresponding operand bits are both 1, and set to 0 otherwise | Bitwise OR (inclusive) Similar to the || operator, but on a bit-by-bit basis.  Bits in the result set to 1 if at least one of the corresponding bits in the two operands is 1.  0 otherwise. ^ Bitwise OR (exclusive) Shifts the bits of the first operand to the left, by the number of bits specified in the second operand. Right fill with 0 bits.

16 Bitwise Operators Operator Name Arity Description << Left Shift
Binary Shifts the bits of the first operand to the left, by the number of bits specified in the second operand. Right fill with 0 bits. >> Right Shift Shifts the bits of the first operand to the right, by the number of bits specified in the second operand. Left fill depends on the machine. Usually based on the sign (fill with 0's for positive numbers, 1's for negatives). ~ Complement Unary Flips the bits in the operand. Similar to negation. (All 1's become 0's, and all 0's become 1's).

17 Bitwise Operators Also, there are shortcut assignment operators, similar to arithmetic operators: x &= y means x = x & y x |= y means x = x | y x ^= y means x = x ^ y x <<= y means x = x << y x >>= y means x = x >> y

18 Bitwise AND Example (x & y)
Suppose we have the following code: short x = 6891; short y = 11318; And let's assume that we are using a machine in which a short is 2 bytes (which is 16 bits). The binary representations of x and y, then, are: x: y: Suppose we did the operation (x & y). Then we perform the AND operation on the individual bits: result: // this is the value 2082

19 Bitwise OR Examples (x | y) (x ^y)
Suppose we have the following code: short x = 6891; short y = 11318; Bitwise OR operation (x | y): x: y: result: // this is the value 16127 Bitwise exclusive OR operation (x ^ y): result: // this is the value 14045

20 Bitwise Shift & Complement Examples >>, <<, ~
Here is a bitwise left shift, performed on x (x << 2): x: shifted: // this is the value 27564 Here is a bitwise right shift, performed on y (y >> 4): y: shifted: // this is the value 707 And here is the complement of x (~x) ~x: // this is the value -6892

21 Bitwise Code Example #include <iostream> #include <iomanip> using std::cout; using std::endl; int main() { short x = 6891; short y = 11318; cout << "x = " << x << "\ny = " << y << endl; cout << "x & y = " << (x & y) << endl; cout << "x | y = " << (x | y) << endl; cout << "x ^ y = " << (x ^ y) << endl; cout << "x << 2 = " << (x << 2) << endl; cout << "y >> 4 = " << (y >> 4) << endl; cout << "~x = " << ~x << endl; }

22 Questions?


Download ppt "Object Oriented Programming COP3330 / CGS5409"

Similar presentations


Ads by Google