Friend classes Friend class methods Nested classes Throwing exceptions, try blocks and catch blocks Exception classes Runtime type identification (RTTI)

Slides:



Advertisements
Similar presentations
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
Advertisements

Exceptions OO Software Design and Construction Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ 1 Exceptions exceptiona program.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 23 - Exception Handling Outline 23.1Introduction 23.2When Exception Handling Should Be Used 23.3Other.
Ch 11. Exception Handling Timothy Budd Oregon State University.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Exception Handling: A Deeper.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
C++ Exception Handling
1 CSC241: Object Oriented Programming Lecture No 28.
CS Advanced C++ Exception Handling Topic #5.
1 CS 204 Advance Programming Exception Handling in C++ Horton, pp. 239 – 247(the contents in the slides may be different than the book)
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Rossella Lau Lecture 9, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 9: Application with Exception Handling 
. Plab – Tirgul 11 RTTI, Binary files. RTTI – why? Problem: u Up-casting works fine.  Treating sub-class as base class Shape * s = new Circle(); u What.
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
Run-time type information (RTTI) and casts Consider classes for components and windows: class Component {... virtual void draw() {} }; class Window: public.
Run Time Type Information, Binary files. RTTI – why? Problem: Up-casting works fine. –Treating sub-class as base class Shape * s = new Circle(); What.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 - Exception Handling Outline 13.1Introduction 13.2When Exception Handling Should Be Used.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
1 CSC241: Object Oriented Programming Lecture No 27.
BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
HANDLING EXCEPTIONS Chapter 9. Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Exceptions and Program Correctness based on the original work by Dr. Roger deBry Version 1.1.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 23 - Exception Handling Outline 23.1Introduction.
Exception Handling in C++. Outline What exceptions are and when to use them Using try, catch and throw to detect, handle and indicate exceptions, respectively.
Exception Handling Outline 23.1 Introduction
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
Inheritance and Composition Reusing the code and functionality Unit - 04.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
LECTURE LECTURE 14 Exception Handling Textbook p
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Exception Handling How to handle the runtime errors.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
CHAPTER 18 C – C++ Section 1: Exceptions. Error Handling with Exceptions Forces you to defend yourself Separates error handling code from the source.
Chapter 2 Objects and Classes
C++ Lesson 1.
C ++ MULTIPLE CHOICE QUESTION
IS 0020 Program Design and Software Tools
Chapter 16 Exception Handling: A Deeper Look
Abstract Class Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for.
CS212: Object Oriented Analysis and Design
Exception Handling: A Deeper Look
Object-Oriented Programming (OOP) Lecture No. 45
Chapter 2 Objects and Classes
Chapter 14: Exception Handling
2.3 Testing Programs 12 – Program Correctness.
CS 204 Advance Programming Exception Handling in C++
Throwing exceptions.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
16 Exception Handling.
Throwing exceptions.
Presentation transcript:

Friend classes Friend class methods Nested classes Throwing exceptions, try blocks and catch blocks Exception classes Runtime type identification (RTTI) dynamic_cast and typeid static_cast, const_cast, and reinterpret_cast Friend, Exceptions, and More

Friends Friend function –Allow to access the private and protected members of the original class Friend class –Every member function in the friend class are allowed to access both private and protected members of the original class

Friend class TV and Remote –On/off –Channel setting –Volume setting –Antenna or cable tuning mode –TV tuner or A/V input Program tv.h, tv.cpp, use_tv.cpp

Friend member functions

Friend relationships Program tvfm.h

Interactive remote controller

Shared friends Probe class –Programmable measuring device Analyzer class –Programmable analyzing device Each has an internal clock and one would like to be able to synchronize the two clocks

Shared friends

Nested classes Suppose queue class declaration:

Methods definitions

More appropriate declaration

Using constructor to rewrite This makes the code for enqueuer() a bit shorter and a bit safer because it automates initialization rather than requiring the programmer to correctly remember what should be done

Nested classes and access Nested class is different from containment A nested class is declared controls the scope of the nested class. It establishes which parts of a program can create objects of that class Nested class object can be used in class A As with an class, the public, protected, and private sections of a nested class provide access control to class members

Scope properties for nested classes 二類別 Program queuetp.h, nested.cpp

Exceptions Standard error stream –abort(); // cstdlib.h, doesn’t clear buffer –cerr(); // standard error stream –exit(); // clear buffer, but no messages Program error1.cpp, error2.cpp

The exception mechanism Three components –Throwing an exception ( throw ) –Catching an exception with a handler ( catch ) –Using a try block ( try ) Program error3.cpp

Program flow with exceptions

Using objects as exceptions Program exc_mean.h, error4.cpp

Unwinding the stack Program error5.cpp

More exception features

catch (…)

The exception class

The stdexcept exception class Classes logic_error, runtime_error derived publicly from exception Classes domain_error, invalid_argument, length_error, out_of_bounds derived publicly from logic_error Classes range_error, overflow_error, underflow_error derived publicly from runtime_error

The logic_error class

The bad_alloc exception and new Class bad_alloc derived publicly from exception class Program newexcp.cpp The null pointer and new

Exceptions, classes, and inheritance One can derive one exception class from another One can incoporate exceptions into classes by nesting exception class declarations inside a class definition Such nested declarations can be inherited and can serve as base classes themselves Program sales.h, sales.cpp, use_sales.cpp

Runtime type identification (RTTI) RTTI is one of more recent additions to C++, which is not supported by many older implementations How can one tells what kind of object a base-class pointer points to? –Find the correct non-virtual functions in class –For the reasons of debugging

How does RTTI work? Three components supporting RTTI dynamic_cast operator –Generate a derived type from a pointer to a base type; return null pointer (0) when fail typeid operator –Return a value identifying the exact type of an object type_info structure –Store information about a particular type

dynamic_cast operator

Program rtti1.cpp

The typeid operator & type_info class #include if (typeid(Magnificent) == typeid(*pg)) … cout << “Now processing type “ << typeid(*pg).name() << endl; Program rtti2.cpp

Misusing RTTI Rewrite code

Type cast operators

Completing type cast dynamic_cast const_cast static_cast reinterpret_cast

dynamic_cast

const_cast Program constcast.cpp

static_cast

reinterpret_cast The following type cast is allowed in C but not in C++ implementations the char is too small to hold a pointer implementation