Top Banner
• 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
39

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

Jan 18, 2018

Download

Documents

Letitia Porter

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
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Friend classes Friend class methods Nested classes Throwing exceptions, try blocks and catch blocks Exception classes Runtime type identification (RTTI)

• 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

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

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

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

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

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

Friend member functions

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

Friend relationships

• Program tvfm.h

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

Interactive remote controller

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

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

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

Shared friends

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

Nested classesSuppose queue class declaration:

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

Methods definitions

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

More appropriate declaration

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

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

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

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

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

Scope properties for nested classes

二類別

• Program queuetp.h, nested.cpp

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

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

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

The exception mechanism• Three components

– Throwing an exception (throw)– Catching an exception with a handler

(catch)– Using a try block (try)

• Program error3.cpp

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

Program flow with exceptions

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

Using objects as exceptions

• Program exc_mean.h, error4.cpp

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

Unwinding the stack

• Program error5.cpp

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

More exception features

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

catch (…)

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

The exception class

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

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

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

The logic_error class

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

The bad_alloc exception and new

• Class bad_alloc derived publicly from exception class

• Program newexcp.cpp• The null pointer and new

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

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

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

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

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

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

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

dynamic_cast operator

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

• Program rtti1.cpp

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

The typeid operator & type_info class

• #include <typeinfo.h>if (typeid(Magnificent) == typeid(*pg))

…cout << “Now processing type “ <<

typeid(*pg).name() << endl;• Program rtti2.cpp

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

Misusing RTTI

Rewrite code

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

Type cast operators

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

Completing type cast

• dynamic_cast• const_cast• static_cast• reinterpret_cast

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

dynamic_cast

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

const_cast

• Program constcast.cpp

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

static_cast

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

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