0% found this document useful (0 votes)
10 views52 pages

Final Lecture

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views52 pages

Final Lecture

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Contents:

Exceptions
Templates
Object-oriented Systems Analysis And Design Using Uml
EXCEPTIONS
• Exceptions are run time anomalies or unusual conditions that a
program may encounter during execution.
• Conditions such as
• Division by zero
• Access to an array outside of its bounds
• Running out of memory
• Running out of disk space
• It was not a part of original C++.
• It is a new feature added to ANSI C++.
EXCEPTION HANDLING
• Exceptions are of 2 kinds

• Synchronous Exception:
• Out of rage
• Over flow

• Asynchronous Exception: Error that are caused by causes beyond the control
of the program
• Keyboard interrupts

• In C++ only synchronous exceptions can be handled.


EXCEPTION HANDLING (CONT…)

• Exception handling mechanism


• Find the problem (Hit the exception).

• Inform that an error has occurred (Throw the exception).

• Receive the error information (Catch the exception).

• Take corrective action (handle the exception).


EXCEPTION HANDLING MECHANISM

• It is basically build upon three keywords


try block
• try
Detects and throw
• throw an exception

• catch Exception Object

catch block

Catch and handle


the exception
EXCEPTION HANDLING MECHANISM (CONT…)
• The keyword try is used to preface a block of statements which may
generate exceptions.
• When an exception is detected, it is thrown using a throw statement
in the try block.
• A catch block defined by the keyword ‘catch’ catches the exception
and handles it appropriately.
• The catch block that catches an exception must immediately follow
the try block that throws the exception.
EXCEPTION HANDLING MECHANISM (CONT…)
• try
{
… // Block of statements
// which detect and throws an exception
throw exception;

}
• catch(type arg)
{



}
EXCEPTION HANDLING MECHANISM (CONT…)

• Exceptions are objects used to transmit information about a problem.

• If the type of the object thrown matches the arg type in the catch
statement, the catch block is executed.

• If they do not match, the program is aborted.


THROWING MECHANISM

• The throw statement can have one of the following 3 forms


• throw(exception)
• throw exception
• throw //used to re-throw a exception
• The operand object exception can be of any type, including constant.
• It is also possible to throw an object not intended for error handling.
• Throw point can be in a deeply nested scope within a try block or in a
deeply nested function call.
• In any case, control is transferred to the catch statement.
CATCHING MECHANISM

• The type indicates the type of exception the catch(type arg)


catch block handles. {
• The parameter arg is an optional …
parameter name. …

• The catch statement catches an exception
whose type matches with the type of the }
catch argument.
CATCHING MECHANISM (CONT…)

• If the parameter in the catch statement is named, then the parameter


can be used in the exception handling code.

• If a catch statement does not match the exception it is skipped.

• More than one catch statement can be associated with a try block.
CATCHING MECHANISM (CONT…)
try
{
throw exception;
}
catch(type1 arg)
{
// catch block 1
}
catch(type2 arg)
{
// catch block 2
}


catch(typeN arg)
{
// catch block N
}
CATCHING MECHANISM (CONT…)
• When an exception is thrown, the exception handlers are searched in
order for a match.
• The first handler that yields a match is executed.
• If several catch statement matches the type of an exception the first
handler that matches the exception type is executed.
• Catch all exception
catch (…)
{
// statement for processing all exceptions
}
RETHROWING AN EXCEPTION

• A handler may decide to rethrow the exception caught without processing it.

• In such a case we have to invoke throw without any arguments as shown below

throw;

• This causes the current exception to be thrown to the next enclosing try/catch
sequence and is caught by a catch statement listed after the enclosing try

block
Templates
• Function Templates
• Syntax, defining
• Compiler complications
• Class Templates
• Syntax
• Example: array template class
• Templates and Inheritance
• Example: partially-filled array template class

Copyright © 2006 Pearson Addison-


16-15
Wesley. All rights reserved.
Introduction
• C++ templates
• Allow very "general" definitions for functions and classes
• Type names are "parameters" instead of
actual types
• Precise definition determined at run-time

Copyright © 2006 Pearson Addison-


16-16
Wesley. All rights reserved.
Function Templates
• Typical function swapValues:
void swapValues(int& var1, int& var2)
{
int temp;
temp = var1;
var1 = var2;
var2 = temp;
}
• Applies only to variables of type int
• But code would work for any types!

Copyright © 2006 Pearson Addison-


16-17
Wesley. All rights reserved.
Function Templates vs. Overloading

• Could overload function for char’s:

void swapValues(char& var1, char& var2)


{
char temp;
temp = var1;
var1 = var2;
var2 = temp;
}
• But notice: code is nearly identical!
• Only difference is type used in 3 places

Copyright © 2006 Pearson Addison-


16-18
Wesley. All rights reserved.
Function Template Syntax
• Allow "swap values" of any type variables:
template<class T>
void swapValues(T& var1, T& var2)
{
T temp;
temp = var1;
var1 = var2;
var2 = temp;
}
• First line called "template prefix"
• Tells compiler what’s coming is "template"
• And that T is a type parameter

Copyright © 2006 Pearson Addison-


16-19
Wesley. All rights reserved.
Template Prefix
• Recall:
template<class T>
• In this usage, "class" means "type", or
"classification"
• Can be confused with other "known" use
of word "class"!
• C++ allows keyword typename in place of
keyword class here
• But most use class anyway

Copyright © 2006 Pearson Addison-


16-20
Wesley. All rights reserved.
Template Prefix 2
• Again:
template<class T>
• T can be replaced by any type
• Predefined or user-defined (like a C++ class type)
• In function definition body:
• T used like any other type
• Note: can use other than T, but T is
"traditional" usage

Copyright © 2006 Pearson Addison-


16-21
Wesley. All rights reserved.
Function Template Definition
• swapValues() function template is actually
large "collection" of definitions!
• A definition for each possible type!
• Compiler only generates definitions when
required
• But it’s "as if" you’d defined for all types
• Write one definition → works for all types
that might be needed

Copyright © 2006 Pearson Addison-


16-22
Wesley. All rights reserved.
Calling a Function Template
• Consider following call:
swapValues(int1, int2);
• C++ compiler "generates" function definition for two int parameters
using template
• Likewise for all other types
• Needn’t do anything "special" in call
• Required definition automatically generated

Copyright © 2006 Pearson Addison-


16-23
Wesley. All rights reserved.
Another Function Template
• Declaration/prototype:

template<class T>
void showStuff(int stuff1, T stuff2, T stuff3);
• Definition:

template<class T>
void showStuff(int stuff1, T stuff2, T stuff3)
{
cout << stuff1 << endl
<< stuff2 << endl
<< stuff3 << endl;
}

Copyright © 2006 Pearson Addison-


16-24
Wesley. All rights reserved.
Show Stuff Call
• Consider function call:
showStuff(2, 3.3, 4.4);
• Compiler generates function definition
• Replaces T with double
• Since second parameter is type double
• Displays:
2
3.3
4.4

Copyright © 2006 Pearson Addison-


16-25
Wesley. All rights reserved.
Compiler Complications
• Function declarations and definitions
• Typically we have them separate
• For templates → not supported on
most compilers!
• Safest to place template function
definition in file where invoked
• Many compilers require it appear 1st
• Often we #include all template definitions

Copyright © 2006 Pearson Addison-


16-26
Wesley. All rights reserved.
More Compiler Complications
• Check your compiler’s specific requirements
• Some need to set special options
• Some require special order of arrangement
of template definitions vs. other file items
• Most usable template program layout:
• Template definition in same file it’s used
• Ensure template definition precedes all uses
• Can #include it

Copyright © 2006 Pearson Addison-


16-27
Wesley. All rights reserved.
Multiple Type Parameters
• Can have:
template<class T1, class T2>
• Not typical
• Usually only need one "replaceable" type
• Cannot have "unused"
template parameters
• Each must be "used" in definition
• Error otherwise!

Copyright © 2006 Pearson Addison-


16-28
Wesley. All rights reserved.
Algorithm Abstraction
• Refers to implementing templates
• Express algorithms in "general" way:
• Algorithm applies to variables of any type
• Ignore incidental detail
• Concentrate on substantive parts
of algorithm
• Function templates are one way C++
supports algorithm abstraction

Copyright © 2006 Pearson Addison-


16-29
Wesley. All rights reserved.
Defining Templates Strategies
• Develop function normally
• Using actual data types
• Completely debug "ordinary" function
• Then convert to template
• Replace type names with type parameter
as needed
• Advantages:
• Easier to solve "concrete" case
• Deal with algorithm, not template syntax

Copyright © 2006 Pearson Addison-


16-30
Wesley. All rights reserved.
Inappropriate Types in Templates
• Can use any type in template for which
code makes "sense"
• Code must behave in appropriate way
• e.g., swapValues() template function
• Cannot use type for which assignment operator isn’t defined
• Example: an array:
int a[10], b[10];
swapValues(a, b);
• Arrays cannot be "assigned"!

Copyright © 2006 Pearson Addison-


16-31
Wesley. All rights reserved.
Class Templates
• Can also "generalize" classes
template<class T>
• Can also apply to class definition
• All instances of T in class definition replaced by type parameter
• Just like for function templates!
• Once template defined, can declare
objects of the class

Copyright © 2006 Pearson Addison-


16-32
Wesley. All rights reserved.
Class Template Definition
template<class T>
class Pair
{
public:
Pair();
Pair(T firstVal, T secondVal);
void setFirst(T newVal);
void setSecond(T newVal);
T getFirst() const;
T getSecond() const;
private:
T first; T second;
};

Copyright © 2006 Pearson Addison-


16-33
Wesley. All rights reserved.
Template Class Pair Members
template<class T>
Pair<T>::Pair(T firstVal, T secondVal)
{
first = firstVal;
second = secondVal;
}

template<class T>
void Pair<T>::setFirst(T newVal)
{
first = newVal;
}

Copyright © 2006 Pearson Addison-


16-34
Wesley. All rights reserved.
Template Class Pair
• Objects of class have "pair" of values of
type T
• Can then declare objects:
Pair<int> score;
Pair<char> seats;
• Objects then used like any other objects
• Example uses:
score.setFirst(3);
score.setSecond(0);

Copyright © 2006 Pearson Addison-


16-35
Wesley. All rights reserved.
Pair Member Function Definitions
• Notice in member function definitions:
• Each definition is itself a "template"
• Requires template prefix before
each definition
• Class name before :: is Pair<T>
• Not just "Pair"
• But constructor name is just "Pair"
• Destructor name is also just "~Pair"

Copyright © 2006 Pearson Addison-


16-36
Wesley. All rights reserved.
Class Templates as Parameters
• Consider:
int addUP(const Pair<int>& the Pair);
• The type (int) is supplied to be used for T
in defining this class type parameter
• It "happens" to be call-by-reference here
• Again: template types can be used
anywhere standard types can

Copyright © 2006 Pearson Addison-


16-37
Wesley. All rights reserved.
Class Templates
Within Function Templates
• Rather than defining new overload:
template<class T>
T addUp(const Pair<T>& the Pair);

//Precondition: Operator + is defined for values of


type T
//Returns sum of two values in the Pair
• Function now applies to all kinds
of numbers

Copyright © 2006 Pearson Addison-


16-38
Wesley. All rights reserved.
Restrictions on Type Parameter
• Only "reasonable" types can be substituted
for T
• Consider:
• Assignment operator must be "well-behaved"
• Copy constructor must also work
• If T involves pointers, then destructor must
be suitable!
• Similar issues as function templates

Copyright © 2006 Pearson Addison-


16-39
Wesley. All rights reserved.
Type Definitions
• Can define new "class type name"
• To represent specialized class template name
• Example:

typedef Pair<int> PairOfInt;


• Name PairOfInt now used to declare
objects of type Pair<int> :

PairOfInt pair1, pair2;


• Name can also be used as parameter,
or anywhere else type name allowed

Copyright © 2006 Pearson Addison-


16-40
Wesley. All rights reserved.
Friends and Templates
• Friend functions can be used with
template classes
• Same as with ordinary classes
• Simply requires type parameter
where appropriate
• Very common to have friends of
template classes
• Especially for operator overloads (as
we’ve seen)

Copyright © 2006 Pearson Addison-


16-41
Wesley. All rights reserved.
Predefined Template Classes
• Recall vector class
• It’s a template class!
• Another: basic_string template class
• Deals with strings of "any-type" elements
• e.g.,
basic_string<char> basic_string<double>
basic_string<YourClass>

Copyright © 2006 Pearson Addison-


16-42
Wesley. All rights reserved.
basic_string Template Class
• Already used it!
• Recall "string"
• It’s an alternate name for basic_string<char>
• All member functions behave similarly for
basic_string<T>
• basic_string defined in library <string>
• Definition is in std namespace

Copyright © 2006 Pearson Addison-


16-43
Wesley. All rights reserved.
Templates and Inheritance
• Nothing new here
• Derived template classes
• Can derive from template or
nontemplate class
• Derived class is then naturally a
template class
• Syntax same as ordinary class derived
from ordinary class

Copyright © 2006 Pearson Addison-


16-44
Wesley. All rights reserved.
Summary
• Function templates
• Define functions with parameter for a type
• Class templates
• Define class with parameter for subparts of class
• Predefined vector and basic_string
classes are template classes
• Can define template class derived from
a template base class

Copyright © 2006 Pearson Addison-


16-45
Wesley. All rights reserved.
OBJECT-ORIENTED SYSTEMS
ANALYSIS AND DESIGN USING
UML
The steps in the UML
development process.
Explain the basic concept of UML and list its
steps in development process
• Comprehend the concepts of unified modeling language (UML), the
standard approach for modeling a system in the object-oriented
world.
• Apply the steps used in UML to break down the system into a use
case model and then a class model.
• Diagram systems with the UML toolset so they can be described and
properly designed.
• Document and communicate the newly modeled object-oriented
system to users and other analysts.
Define UML and its components: Things,
Relationships, and Diagrams.
An overall view of UML diagrams showing how each diagram
leads to the development of other UML diagrams. Describe the
relationships between the UML diagrams
Design the class diagram that describe the
inheritance
Class Diagram Syntax

You might also like