The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.

Here you can download the for the course CS304-Object Oriented Programming. You can easily read the handouts from the given pdf reader or download the file on just single click on the button given below.

Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by haxohaw528, 2022-05-17 00:40:17

CS304 handouts 1-45

Here you can download the for the course CS304-Object Oriented Programming. You can easily read the handouts from the given pdf reader or download the file on just single click on the button given below.

Object Oriented Programming (CS304) VU

Exception in Initialization List

Exception due to constructor of any contained object or the constructor of a parent
class can be caught in the member initialization list

Example

Student::Student (String aName) : name(aName)
/*The constructor of String can throw a exception*/
{

...
}

Exception in Initialization List

The programmer may want to catch the exception and perform some action to rectify
the problem

Example

Student::Student (String aName)
try : name(aName) {
...
}
catch(…) {
}

Exceptions in Destructors

Exception should not leave the destructor, because when a destructor is running it
means that we have a stack unwinding going on that has run this destructor to delete
this object if this exception will be allowed to run it will run another stack unwinding
mechanism and this will be called leaving the exception from destructor and is not
allowed. C++ allows running only one stack unwinding process at a time.
If a destructor is called due to stack unwinding, and an exception leaves the
destructor then the function std::terminate() is called, which by default calls the
std::abort()

Example

class Exception;
class Complex{


public:

~Complex(){
throw Exception();

}
};
int main(){

try{

© Virtual University of Pakistan 351

Object Oriented Programming (CS304) VU

Complex obj;
throw Exception();

}
catch(…){
}
return 0;
}

// The program will terminate abnormally

So add catch exception in destructor itself as shown below, in this case single stack
unwinding process may handle the situation.

Example

Complex::~Complex()
{

try{
throw Exception();

}
catch(…){
}
}

Exception Specification

• Program can specify the list of exceptions a function is allowed to throw
• This list is also called throw list
• If we write empty list then the function wont be able to throw any exception
Syntax

void Function1() {…}
void Function2() throw () {…}
void Function3( ) throw (Exception1, …){}

Here,

• Function1 can throw any exception
• Function2 cannot throw any Exception
• Function3 can throw any exception of type Exception1 or any class derived

from it

Exception Specification

• If a function throws exception other then specified in the throw list then the
function unexpected is called

352 © Virtual University of Pakistan

Object Oriented Programming (CS304) VU

• The function unexpected calls the function terminate and terminates the
program

• If programmer wants to handle such cases also then he must provide a
handler function and tell the compiler to call handler using set_unexpected

Course Review

We have studied the following topics in this course,

Object Orientation
• What is an object
• Object-Oriented Model
o Information Hiding
o Encapsulation
o Abstraction
• Classes

Object Orientation

• Inheritance
o Generalization
o Sub-Typing
o Specialization

• “IS-A” relationship
• Abstract classes
• Concrete classes

Object Orientation

• Multiple inheritance
• Types of association

o Simple association
o Composition
o Aggregation
• Polymorphism

Classes – C++ Constructs

• Classes
o Data members
o Member functions

• Access specifier
• Constructors
• Copy Constructors
• Destructors

Classes – C++ Constructs

• this pointer

© Virtual University of Pakistan 353

Object Oriented Programming (CS304) VU

• Constant objects
• Static data member
• Static member function
• Dynamic allocation

Classes – C++ Constructs

• Friend classes
• Friend functions
• Operator overloading

o Binary operator
o Unary operator
o operator []
o Type conversion

Inheritance – C++ Constructs

• Public inheritance
• Private inheritance
• Protected inheritance
• Overriding
• Class hierarchy

Polymorphism – C++ Constructs

• Static type vs. dynamic type
• Virtual function
• Virtual destructor
• V-tables
• Multiple inheritance
• Virtual inheritance

Templates – C++ Constructs

• Generic programming
• Classes template
• Function templates
• Generic algorithm
• Templates specialization

o Partial Specialization
o Complete specialization

Templates – C++ Constructs

• Inheritance and templates
• Friends and templates
• STL

o Containers
o Iterators

354 © Virtual University of Pakistan

Object Oriented Programming (CS304) VU

o Algorithms

Writing Reliable Programs

• Error handling techniques
o Abnormal termination
o Graceful termination
o Return the illegal value
o Return error code from a function
o Exception handling

Note:

Please give your suggestions/comments about this document at the email address
[email protected], in the following format

Page No.# Issue/Error

© Virtual University of Pakistan 355


Click to View FlipBook Version