Transcript PowerPoint

Final Review
Andy Wang
Object Oriented Programming in C++
COP 3330
Bitwise Operators

Built-in operators that allow accessing and manipulating
of individual bits

Necessary because smallest variables that can be
created are at least 1 byte

Accessing individual bits can be useful for making more
efficient algorithms, or using less storage space
The Bitwise Operators

Bitwise &.


Bitwise |


Performs the | operation on individual bits
Bitwise ^ (exclusive OR)


Performs the & operation on individual bits (1 is true, 0 is
false)
XOR is true if there is exactly one true and one false
Complement ~

Reverses the bits of a variable (1 -> 0, 0 -> 1)
The Bitwise Operators

<< (left shift)


Shifts the bits of a variable to the left
>> (right shift)

Shifts the bits of a variable to the right
Accessing Individual Bits

Understand the concept of a bit mask and how to create
one

Understand how to do these basic operations on a single
bit from a variable, without changing the other bits
stored in the variable


Set a bit to 1

Unset a bit (set to 0)

Flip a bit to its opposite

Query a bit (find out what it is—1 or 0)
You should be able to identify or derive how the above
operations are done on a single bit, with a combination
of an appropriate operation and a bit mask
Templates

Function templates

Functions that can work with multiple parameter types

Compiler builds a separate function for each needed type

An easy way to create overloaded functions without
writing individual version

Operations performed inside the function need to be valid
for types used in the calls
Class Templates

Extension of function template idea

Allows creation of a generic class, where the type of
item stored can vary

Again, operations used inside the class need to be valid
for any type used to instantiate the class

Each member function written as a function template
Template Declarations and
Object Instantiations

Use keyword template, along with template parameters
in angle brackets <>
template<class T>
template<typename T>

Examples
stack<double> myStack;
stack<int> stack2(10:
Data Structures

Basic data structure types

Stack—first in last out structure

Queue—first in first out structure

Vector—storage of a list using array-based storage

Linked-list—storage of a list in a linear format using selfreferential objects


A self-referential object contains data, along with one+
pointers, which point to other objects of the same type

Each node of a linked list stores a piece of data, and points to
the next node in the list
Tree—non-linear storage of a set of data using selfreferential objects

Each node in a tree has 2+ pointers to other nodes

Binary tree is good for soring and searching
Data Structures

Implementing basic data structures

Encapsulation of data structure inside an object means
details can be handled internally

Outside access through simpler interface

Often involves pointers and dynamic memory allocation

Templates are commonly used, to make data structures
more general

Some structures can be implemented with others

Through inheritance or composition

A stack can be implemented with a linked list of a vector
Exception Handling

A method of error-handling

Good for processing errors that must be handled in
places other than where they occurred

Good for handling errors from libraries or other
components

Should not be used for general program control
(confusing to the reader
Syntax

try { }


Label used for a block that encloses an area where
exception might be thrown
throw

Used to throw an exception

Can throw an item like a variable or an object


throw DivideByZeroException()
Also used to build a throw list

void function(int x) thorw (ThisException, ThatException)
Syntax

catch { }

The catch blocks immediately follow the try blocks

Can have more than one

Each catch can take one parameter, indicating the type of
exception

Special catch block catch(…) { }

Will catch any thrown exception

Useful for a default case—a “catch all”
Recursion

Recursive function

A function that calls itself

Recursion vs. iteration

Some example algorithms

Factorial, Fibonnaci, GCD, sorting, binary search
Miscellaneous

Conditional compilation

#define SYMBOL


#ifdef SYMBOL


Do something
#ifndef SYMBOL


Brings the symbol into existence
Do something
#endif
Code Writing Test Format


Specifications

Main program

Sample execution output
Your job

Write the declarations and definition files
Example Main Program
#include <iostream>
#include “flex.h”
using namespace std;
int main() {
Flex a, b(“Merry”), c(“Christmas”);
cout << a << ‘,’ << b << ‘,’ << c << endl;
b.cat(a);
cout << b << endl;
b.cat(c);
cout << b << endl;
c.cat(c); c.cat(c);
cout << c << endl;
return 0;
}
Example Output
* *,*Merry*,*Christmas*
*Merry *
*Merry Christmas*
*ChristmasChristmasChristmasChristmas*
Specifications

Objects of class Flex allow a variable length string to be
maintained

When the constructor for Flex is provided a c-string as a
parameter, the Flex object created will have that string
value.

If no parameter is provided, a default string consisting of
exactly one space should be created.

Flex should have an overload of the output operator
that will display the string surrounded by stars

Flex also has a void function, cat, having one reference
parameter of type Flex.

The function cat should append the string in that
parameter to the end of the Flex object invoking cat
Specifications

There is no established bound on the size of a Flex
object, so dynamic storage allocation should be used

All Flex member data is private

Show the content in flex.h and flex.cpp

It is okay to use the cstring library functions