Final II Flashcards
An object consists of 3 things:
- Name
- Member Data
- Member Functions
An objects name is:
The variable name we give it.
Member data is:
The data that describes an object; Attributes, or the state of the object.
Member functions are:
Behavior aspects of an object (functions related to the object itself)
What is an object?
An encapsulation of data along with functions that act upon that data.
What is a class?
A blueprint for building objects; A user defined type.
A class description consists of:
A declaration and a definition. Usually split into separate files.
An object is a single instance of:
A class. You can create many objects from the same type of class.
What does DDU stand for?
Declare, Define, Use.
Declare (DDU)
Declaration of class (usually) in a header file; Gives an interface.
A variable declaration gives:
The type
A function declaration:
Tells how to use it, without bothering with how it works.
A class declaration:
Shows what an object will look like and what its available functions are, implementation details are not needed.
Define (DDU)
Definition of class members in implementation file.
T/F
The definition of a class doesn’t need to be seen by the user of the class (interface)?
True
A function definition:
The code that makes a function work (the function body)
A class definition:
Consists of definitions of its members.
Use (DDU)
Usage of class by building objects, through its interface (declaration).
The user of a function is:
A programmer, who makes calls to the function (without needing to know implementation details)
The user of a class is:
A programmer, who uses it by creating objects and calling the available functions for those objects.
3 Protection (access) levels in a class:
1) Public
2) Private
3) Protected
Public:
Can be accessed from inside or outside of the object.
Private:
Can only be used by the object itself.
Protected:
Protection label for use in base/derived classes; Members declared as protected can be accesses by the class itself and by derived classes only (not anywhere else)
The interface of an object is essentially comprised of:
The public section of the class.
The user of an objects is:
Some other portion of code (other classes, functions, main program)
Providing functions in the public area that handle all necessary actions on an object helps to:
Make the interface as simple as possible.
T/F
There are set rules on what gets made public and what gets made private?
False
Standard practice for member data of a class:
Make it private, to protect the data.
Functions that do not need to be part of the interface can be made:
Private
4 reasons for data hiding:
1) Makes interface simpler for user.
2) Principal of least privilege (need-to-know).
3) More secure. Less chance for misuse (accidental or malicious).
4) Class implementation easy to change without affecting other modules that use it.
Class declaration format:
class <classname></classname>
{
public:
(public data & functions)
private:
(private data & functions)
};
2 principles of good class design:
1) Decide upon semantic meaning of this type of object, along with what combinations of data values constitute valid meaning,
2) Guarantee that an object is always in a valid semantic state, regardless of what features user calls upon.
2 examples to guarantee valid semantic state of an object:
1) In a Circle class, don’t allow for a negative radius.
2) In a Temperature class, don’t allow a temperature to be below absolute zero.
2 ways to ensure an object is always in a valid semantic state:
1) In the public section, only put the members that the user needs to call upon directly.
2) In the private section, put things that the user does not need to use directly (this typically includes most member data)
By hiding member data in the private section:
The builder can control how the data items are updated, with validation/error-checking in the functions.
2 Primary categories of member functions:
1) Mutators
2) Accessors
Mutators:
Member functions that can change the state of an object.
Accessors:
Member functions that will not change the state of an object
Accessors should always be declared with:
const
Setter:
A common form of a mutator; A function that takes data in through parameters in order to set them into private variables on the inside.
Getter:
A common form of an accessor; A function that returns some internal data to the caller, usually by value (as a copy) or in some other read-only way.
Constructor:
A special member function of a class whose purpose is typically to initialize the member data of an object
Why have a constructor:
To ensure initialization that is not left up to the user; To set an object to a valid semantic state immediately upon creation.
3 properties of a Constructor
1) Has the same name as the class, no return type.
2) Automatically invoked upon creating an object.
3) Can have parameters but doesn’t have to;
Default constructor:
A constructor with no parameters.
T/F
A constructor is a function and you can define it to do anything you want.
True
Example call to default constructor:
Circle c1;
Example call to constructor with parameters:
Circle c1(51.4);
T/F
Circle c1(); calls the default constructor for the Circle class?
False, this declares a function named c3 that returns a Circle object.
How to make a constructor with optional parameters:
Use a default value on the parameter you want to make optional.
Example of a constructor with an optional parameter:
Fraction (int n, int d = 1);
Destructor:
A special function whose typical job is to do any clean-up tasks (usually involving memory allocation) that are needed before an object is deallocated.
3 properties of destructors:
1) Looks like the default constructor, but with a ~ in front.
2) Cannot have parameters, so there can only be one per class.
3) Called automatically, right before an object is deallocated by the system (usually when it goes out of scope)
T/F
Like the constructor, a destructor is a function and can do other things, if desired?
True.
Example declaration of a destructor:
~Fraction();
::
Scope resolution operator.
What is the scope resolution operator used for?
For specifying which class a member belongs to when we define it separately from its declaration inside the class.
To call a member function of an existing object use:
The dot operator (.)
-Syntax: f1.Show();
A class module normally consists of these two things:
1) A header file
2) An implementation file
Header file:
Contains the declaration of the class (without implementation details).
Usual form: filename.h
Implementation file:
Contains the implementations (definitions) of the class headers.
Usual form: filename.cpp
T/F
Filenames have to be the same as the class name:
False
Well-chosen filenames can:
Help identify the contents or purpose of a file.
2 major stages of compiling a program:
1) Compile stage
2) Linking stage
Compile stage consists of these 5 things
1) Checking syntax for correctness.
2) Variables and function calls checked to insure that correct declarations were made (“declare before use”).
3) Matching function calls to prototypes (doesn’t match calls to definitions yet).
4) Type checking on variable usage.
5) Translation into object code
Linking stage consists of these 2 things:
1) Linking object code files together, (usually) into an executable program.
2) Matches function calls to their definitions, checks to make sure it has only one definition for every function that is called).
T/F
The end result of the linking stage is usually an executable program?
True (but could be other target type like a DLL).
2 reasons for separate compiling and linking stages:
1) Changes to a file require only that file to be re-compiled, along with re-linking.
2) Libraries are often distributed in a pre-compiled format, so trying to #include a .cpp file would not be feasible.
CS account (g++) command for invoking compile stage only:
g++ -c filename.cpp
2 general rules for compilation and debugging:
1) Don’t #include .cpp files.
2) #include header files to satisfy “declare before use”.
What does it mean for a library to be distributed in a pre-compiled format?
The programmer only has header file and object code, not full implementation code)
3 Types of errors when compiling:
1) Compilation errors
2) Linker errors
3) Run-time errors
Compilation errors:
Usually syntax errors, undeclared variables and functions, improper function calls.
Linker errors:
Usually involve undefined functions or multiply-defined functions or symbols.
2 varieties of run-time errors:
1) Fatal: cause a program to crash during execution.
2) Non-fatal (logical): don’t crash the program, but produce erroneous results.
T/F
Compilation and linker errors result in a failed compilation of the program, while run-time errors occur while the program is running (after successful compilation)?
True
T/F
Compiler errors usually provides a filename and line number indicating where it ran into trouble, for each reported error?
True
4 tips for debugging compiler errors:
1) Always start at the top of the list of errors. Fix first error, then recompile and see what is left.
2) If a list of errors is too long, compile and debug one file at a time.
3) When searching for an error, start with the indicated line number, but also look in the vicinity (usually previous lines) for the possible error.
4) Compile portions of programs as you go, don’t wait until the program is fully written to do the first compile.
T/F
Linker errors are also reported by the compiler, but do not usually contain line numbers.
True, because the linker works on object code, not on the original source code.
3 tips for debugging linker errors:
1) Learn what kinds of problems cause linker errors (usually problems with agreement between definitions and calls).
2) Usually specify some kind of symbol (used by the compiler), which often resembles a function or variable name. This is usually a good clue.
3) Learn about good compilation techniques and pre-processor directives that help avoid linker errors.
T/F
Run-time errors must be tested while running a fully-compiled program?
True
2 tips for fixing run-time errors:
1) To catch fatal errors, try to wedge the mistake between extra printout statements to locate the cause.
2) To catch logic errors, place extra printout statements in code while testing (to be removed in final version). Especially, print out values of internal variables to locate computation problems.
Friend function:
Non-member functions that are given access to a class’ private section.
friend:
Keyword that allows a class to grant full access to an outside entity.
Full access means access to all the the class’ members, including private section.
Where are friend functions declared?
Inside the class definition block.
Is a friend function public or private?
Neither, by definition it is not a member of the class.
Does it matter where in the class definition block a friend function is placed?
No, because it is not a member of the class.
Example declaration of friend function:
friend bool Equals(Fraction x, Fraction y);
T/F
The keyword friend goes on the function definition in the implementation file?
False
T/F
The keyword friend goes on the function definition in the implementation file?
False
Is the scope resolution operator (::) used when defining a friend function?
No, it’s not needed since they are not members of a class.
When using a member function instead of a friend, for a function that works on two or more objects:
One of the objects must be the calling object.
When is it convenient to use a friend function instead of a member function.
When a function works on two or more objects.
Example call to a member function that works on two or more objects:
if ( f1.Equals(f2) );
How many parameters does a member function that works on two objects have?
2
Example of member function that works on two objects:
bool Equals(Fraction f) const;
In a member function working on two objects, to ensure the calling object isn’t changed by the function you should:
Declare the member version as a const member function.
Conversion constructor:
A constructor with one parameter that allows for automatic type conversion from from parameter type to class type.
T/F
The conversion constructor will be invoked automatically in all places where automatic type conversion applies?
True
The conversion constructor will be invoked automatically in all places where automatic type conversion applies?
True
T/F
If a constructor with two parameters has an optional parameter, it counts as constructor with one parameter, and is therefore a conversion constructor?
True
Explicit call to constructor:
Fraction f1;
f1 = Fraction(4);
Implicit call to conversion constructor:
Fraction f2;
f2 = 10;
Example of a conversion constructor declaration:
Fraction(int n);
In the event that a constructor with a single parameter should not be used for automatic type conversions, this feature can be suppressed by:
Using the keyword explicit on the constructor declaration.
explicit means:
Only explicit calls to the constructor can be used.
Everywhere the keyword const applies in code it does 3 things:
1) Expresses an intent on the part of the programmer, which the complier enforces (something is not allowed to change, within some scope)
2) Expresses the intent more clearly to a user (in this case, another portion of code–i.e. maybe another programmer)
3) Affects how certain items can be used. Sometimes this is the difference between using L-values and R-values in calls to functions. Sometimes this affects what other items can be used (whether they are also const or not)
T/F
Pass-by-value vs. Pass-by-reference works the same on objects as it does with built-in types?
True
If an object is passed by value:
A copy of the object is made. Any R-value can be sent on the call.
If an object is passed by reference (without const):
No copy is made. Only an L-value can be sent on the call.
When an object is passed by const reference:
No copy is made but the object cannot be changed through the reference.
2 reasons to pass an object by const reference:
1) Less overhead
2) Since objects are sometimes large, it is often desirable to not have to make a copy.
Passing an object by const reference (const reference parameter) protects against:
Changing the object through the reference.
Example using pass by const reference parameters:
friend Fraction Add(const Fraction& f1, const Fraction& f2);
4 uses of const:
1) const reference parameters in member and friend functions
2) const member functions
3) const objects
4) const member data
Const member functions:
Means the function may not change the calling object itself.
T/F
Using const on a member function can only be done to member functions of a class (not stand-alone functions)?
True
Const member functions protect against:
Changing member data of the calling object. (Object remains in same state before and after call)
How to use const member functions:
Place the keyword const at the end of the member function prototype and on the definition.
T/F
Accessor member functions should always be const?
True
Example of const member function declaration:
int GetNumerator() const;
T/F
Primitive type variable declared as const must be initialized on the same line that they are declared.
True
Const objects:
After the constructor runs for to initialize it, the object’s state (internal data) cannot be changed.
Compiler rule to ensure that a const object cannot be changed:
A const object may only call const member functions.
T/F
Attempting to call a non-const member function on a const object results in a compiler error?
True
T/F
Const objects must also be initialized on the same line that they are declared?
True
T/F
Member data of a class can also be declared const?
True
T/F
You can initialize member data variables on their declaration lines in a class definition block?
False
In order to declare and initialize member data variables in one step (like for const member data) use:
An initialization list.
Operator Overloading:
The creation of new versions of built-in operators for use with user-defined types.
5 rules of operator overloading
1) Cannot change its precedence
2) Cannot change its associativity
3) Cannot change its arity (number of operands)
4) It is not possible to create new operators, only new versions of existing ones
5) Operator meaning on the built-in types cannot be changed
T/F
It is legal to mix and match types for operators (like a Fraction object + a Mixed object)?
True
To declare an operator overload function:
Needs name, return type, and parameter list.
The name of an operator overload function is:
A conjunction of the keyword operator and the operator symbol.
T/F
Some operators can be written as member functions, some as outside functions, and some can be written as either?
True
T/F
When written as stand-alone functions, it’s common to make operator overloads friends of a class:
True
For binary operators:
1) Written as stand-alone function, both operands are passed as parameters.
2) Written as member function, first operand is calling object, second is received parameter.
2 qualities of unary operators:
1) As stand-alone function, the operand is received as a parameter.
2) As a member function, the operand is the calling object. (With a few small exceptions, like post-increment)
T/F
The overloaded insertion («) and extraction (») operators have to be done as stand-alone functions, if they are to work like the built-in versions:
True, if they were members, they would have to be inside the classes ostream and istream.
For overloaded insertion («) and extraction (») operators:
Return type and first parameter are stream objects, which must be passed by reference.
For overloaded extraction (») operator:
Pass the object (second parameter) by reference.
For overloaded insertion («) operator:
Pass the object by value or by const reference (better)
Aggregation/Composition:
“Has-a” relationship between objects
How is Aggregation/Composition implemented?
By embedding an object of one class type (or a pointer or reference) inside as member data of another class type.
T/F
Some developers use the term composition to refer to a stronger form of aggregation, where the embedded objects typically would not exist outside of the container object.
True
“Tool building”
The idea of objects that are components of larger objects.
Aggregation/Composition is useful for:
Embedding objects into a “manager class”, to manage communication between components.
Declare an array of 20 Fraction objects:
Fraction rationals[20];
T/F
Normal declaration of an array of objects uses the default constructor for each object in the array:
True
-Ex: Fraction nums[4];
To specify different constructor for different array items, use:
An initializer set (must use explicit constructor calls)
Fraction nums[3] = { Fraction(2, 4), Fraction(5), Fraction()};
Use a fraction object in an array named rationals to call the Show function for the object in index 2 of the array.
rationals[2].Show();
T/F
Arrays can not be used as member data of a class:
False
To keep track of how many items are in an array, use:
Tracking variable
What needs to be added into member functions when using an array as member data of a class?
Error checking and boundary protection.
What kind of relationship is an array of Card objects embedded inside Deck class?
“has-a” relationship
Two types of memory allocation:
1) Static: Compile time. Size and types known in advance.
2) Dynamic: Run time. Sizes and amounts can be set while program is running.
What is the ‘new’ keyword used for?
Used to allocate dynamic space; returns the address of the allocated item. Store in a pointer. (always use a type after new)
Example of dynamically allocated integer variable:
int * ptr = new int;
T/F
new can only be used to dynamically basic variable types:
False, can be used to allocate basic variables, objects, and arrays.
Dynamically allocate an array of ints:
int * list = new int[size];
T/F
You can pass in parameters to constructors on dynamically allocated objects:
True
What is the ‘delete’ keyword used for?
Deallocates dynamically allocated memory. Apply to the pointer and it deallocates the target.
Syntax for deallocating the dynamic data of a simple variable contained in a pointer called ‘ptr’:
delete ptr;
Deallocation of a dynamic array pointed to by a pointer called ‘nums’:
delete [] nums;
T/F
When dynamically allocating an object, the default constructor is invoked unless parameters are added:
True
What operator is this and what does it do:
->
Arrow operator, like dot operator but for pointers.
p->Show(); equivalent to (*p).Show();
4 steps to dynamically resizing an array:
1) Dynamically create a new array of the desired size (need another pointer)
2) Copy data from the old array to the new one (use for loop)
3) Deallocate the old array.
4) Change the pointer so the new array has the right name.
4 steps to use dynamic memory allocation inside a class:
1) Declare pointer as member data
2) Always initialize pointers in the constructor (use null pointer if value is not needed yet)
3) Use new inside member functions to allocate space
4) Use correct cleanup in destructor (delete)
For good class design when using dynamic memory allocation:
Separate memory management tasks from functionality/algorithmic tasks whenever possible.
What are the four automatics:
1) Constructor
2) Destructor
3) Copy constructor
4) Assignment operator=
What makes the automatics special?
If you do not explicitly define one, a default version will be automatically made by the compiler.
3 qualities of automatics:
1) Default version of constructor and destructor are empty
2) Not every class has a default constructor (no parameters), only if no constructor is provided
3) Default version of copy constructor and assignment operator make a shallow copy
What is a shallow copy:
Pointers and references are copied verbatim. They end up pointing to the same attached data
Pass by address:
When & is used in regular statements (not reference declarations), in means “address of”
-Ex: int n;
cout «_space;&n; (Prints the address of n)
Can be used to attach data to pointers
-Ex: int n;
int * p;
p = &n;
Deep Copy:
Makes copies of attached data as well (which is external from the physical “object”)
When is a deep copy needed?
When dynamic allocation is done from inside a class, if copies of objects are to be allowed.
How and when is the copy constructor invoked (2 ways)?
Automatically, when a new copy of an object is created. Copies are made when:
1) An object is declare and initialized to another object’s value in the same statement
2) An object is passed into (or returned from) a function by value
Parameter for copy constructor:
Always has one parameter, which is of the same type of the class. It is always passed by reference. (making the parameter const not required but usually a good idea)
When is the assignment operator invoked?
When one object is assigned to another.
T/F
The assignment operator is written as a stand-alone function:
False, written as a member function
4 steps to defining the assignment operator:
1) Similar to copy constructor in the making of a deep copy
2) Needs to return *this (calling object), by reference. Return enables cascading of =
3) May have previously attached data to delete first
4) May need to protect from self-assignment
2 qualities of ‘this’ (used from a member function):
1) Pointer to the current calling object
2) *this would represent a name for the calling object (the actual object)
C-string:
Implemented as a null terminated character array (No built in string type in C)
-String literals in code are implemented as constant C-strings(“Hello World”)
T/F
Every character array is a c-string:
False, only when terminated with the null character
4 downsides of C-strings
1) Fixed length, when declared as normal c-style array
2) C-string name really just acts as a pointer, since it’s the name of an array
3) Overflow of c-string boundary not automatically detected, including in library functions
4) Less intuitive function calls for common operations
One of the larger pitfalls of C-strings
Library functions for dealing with C-strings are usually based on the expectation of a null-character to stop the loop processing the c-sting
When using a class to build a string type, use these 5 things:
1) Internal implementation with character arrays
2) Dynamic memory allocation for variable length strings
3) Destructor, copy constructor, and assignment operator (in the event of internal dynamic allocation)
4) Conversion constructor for converting other types to strings
5) Operator overloads for more intuitive notations (insertion & extraction, comparison, operator+ for concatenation)
The inheritance relationship:
1) “is-a” relationship
2) Base classes and derived classes (derived class inherits from base class)
3) Represents idea of supertypes and subtypes (categories and subcategories)
Format to declare a derived class
class derivedName : public baseName
(the word “public” causes derivedClassName to be publicly derived from base class; protection levels in derived class are same as in base class)
3 features of a constructor for inheritance relationship
1) When a derived object is created, the base class constructor runs too
2) Constructor definitions run in top-to-bottom order. Base class constructor first
3) Destructor runs in reverse order (derived class first, base last)
For default constructor in inheritance relationship:
The derived class constructor automatically calls the parent constructor
3 features of a constructor with parameters in inheritance relationship:
1) Parameters can be passed in when declaring a derived object
2) Parameters are distributed up to the base constructors through initialization list (with explicit calls to parent constructor)
3) Constructor bodies still run in normal order (base class first)
193 / 229
T/F
Derived classes only inherit public data and functions from base class:
False, they inherit all data and functions
Can you still write a new version of an existing base class function for the derived class?
Yes, using function overriding
1) Both base and derived class can have their own version of a function with the same prototype
2) Can distinguish between them with class name and scope resolution operator
Multiple inheritance
A class can inherit properties from more than one base class
Example of multiple inheritance
Old versions of ifstream and ofstream
1) ifstream derived from istream and fstreambase
2) ofstream derived from ostream and fstreambase
Important pointer property:
Pointers to base class types can point at derived objects (Similarly, a base class reference variable can refer to a derived object)
2 benefits of pointer properties to user:
1) Simplifies storage, putting many base pointers into one container (heterogenous list)
2) Ability to write more versatile functions, with base pointer (or reference) parameters. User can send pointers to various derived objects on the call
Virtual function:
Base class function
declare with virtual keyword
What does declaring a function as virtual do?
Changes the binding of the call to the function definition from static (compile time) to dynamic (run time)
- Function is bound dynamically
A virtual function is called through:
A base class pointer, then the derived version of the function can run (the target of the pointer)
2 reasons why virtual functions are needed:
1) Compiler can only make decisions based on the type of the calling object or pointer
2) Because of the pointer property (base pointer pointing at derived object)
Placing =0 on a function declaration means:
The function will not be defined (makes a virtual function a pure virtual function)
Abstract class:
A class with at least one pure virtual function; Abstract classes cannot be instantiated (cannot build an object of this type)
Polymorphism:
With OOP, refers to the use of the base pointer to child object property, along with virtual functions and function overriding to make the appropriate calls
3 properties of bitwise operators:
1) Built-in operators that allow accessing and manipulating of individual bits
2) Necessary since smallest variables that can be created are at least one byte
3) Accessing individual bits can be useful for making more efficient algorithms or using less storage space
4 properties of function templates:
1) Functions that can work with multiple parameter types
2) Compiler builds a separate function for each needed type, based on the calls used
3) Easy way to create overloaded functions without writing individual versions
4) Operations performed inside the function need to be valid for types used in the calls
4 properties of class templates:
1) Extension of function template idea
2) Allows creation of a “generic” class, where the type of item stored can vary
3) Again, operations used inside the class need to be valid for any type used to instantiate the class
4) Each member function is written as a function template
To declare a template:
Use template keyword along with parameters in angle brackets <>
template < class T >
template < typename T >
Sample declaration of template objects of type Stack.
Stack< double > myStack;
Stack< int > stack2(10);
What are Data Structures used for?
Created to store data in useful ways, often beyond the built-in mechanisms (like arrays)
5 properties of a Stack:
1) First In Last Out (FILO) structure
2) push: function to insert data into stack
3) pop: function to delete data item from stack
4) Inserts and deletes always from the same end
5) Application areas include compilers, operating systems, handling of program memory (nested function calls)
5 properties of a Queue:
1) First In First Out (FIFO) structure, similar to waiting in line
2) enqueue: function to insert data into queue
3) dequeue: function to delete data item from queue
4) Inserts and deletes always from opposite ends
5) Application areas include print job scheduling, operating systems (process scheduling)
3 properties of a Vector:
1) Storage of a list using array based storage internally; stores data items of the same type (Use dynamic allocation and handle boundary issues of array with error checking for out of bounds indices)
2) Advantages: random access - i.e. quick locating of data if index is unknown
3) Disadvantages: Inserts and deletes are slower, since they require shifting of elements to consecutive array slots
Sequence (or list):
Idea of a container that stores a sequence of data, in a specific order, possibly with duplicates (Ex: list of test grades)
What is a Set:
A collection of data that does not have any duplicates, and order may or may not matter (Ex: Venn diagram)
4 properties of a Linked List:
1) Storage of a list in a linear format using self-referential objects
2) Each node of a linked list stores a piece of data, and points to the next node in the list (nodes can be anywhere in memory, are generally allocated dynamically)
3) Advantages: Inserts and deletes are fast. Require only creation of a new node and changing a few pointers
4) Disadvantages: No random access. Possible to build indexing, but locating an element requires walking through the list.
Self-referential object:
Contains data, along with one or more pointers to the next node in the list (the nodes used for Linked Lists qualify as self-referential objects)
The advantages of the array (vector) are generally the disadvantages of:
The linked list, and vice versa.
4 common features of implementing basic data structures:
1) Encapsulation of data structures inside an object means details can be handled internally, outside access through simple interface.
2) Often involves pointers and dynamic memory allocation (inside the data structure)
3) Templates are commonly used, to make data structures more general, useful with more than one type of data
4) Some structures can be implemented with others, through inheritance or composition (A stack can be implemented with a linked list or a vector, for example)
3 properties of a Recursive function:
1) A function that calls itself
2) Involves extra activation records on function call stack
3) Infinite recursion will cause activation records to overrun the stack (and crash the program), so a way out needs to be provided to a recursive function to prevent it infinitely calling itself.
5 example recursive algorithms:
Factorial, Fibonacci, GCD, sorting, binary search
3 properties of Exception handling:
1) A method of error handling
2) Good for processing errors that must be handled in places other than where they occurred
3) Good for handling errors from widely used libraries or other components
4) Should not be used for general program control (confusing to the reader)
Basic exception handling syntax:
try: label used for a block that encloses an area where exceptions might be thrown
- try { }
throw: used to throw an exception. Can “throw” an item like a variable or an object (like special exception objects)
- throw DivideByZeroException(): (Also used to build a throw list)
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 to be caught.
Special catch Block: (Fill in later???)
4 directives of Conditional compilation
define SYMBOL: simply brings the symbol into existence
2 things Conditional compilation statements can do:
1) Be used like if statements, at the compiler level
2) Used to check if symbols are defined, and compiles sections of code based on the result
Example of conditional compilation:
ifndef _FRACTION_H
#define _FRACTION_H
…
// conditionally compiled code goes here
…
#endif