Friend Exceptions and More Friend classes Friend class

  • Slides: 39
Download presentation
Friend, Exceptions, and More • • Friend classes Friend class methods Nested classes Throwing

Friend, Exceptions, and More • • 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

Friends • Friend function – Allow to access the private and protected members of

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

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 member functions

Friend relationships • Program tvfm. h

Friend relationships • Program tvfm. h

Interactive remote controller

Interactive remote controller

Shared friends • Probe class – Programmable measuring device • Analyzer class – Programmable

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

Shared friends

Nested classes Suppose queue class declaration:

Nested classes Suppose queue class declaration:

Methods definitions

Methods definitions

More appropriate declaration

More appropriate declaration

Using constructor to rewrite This makes the code for enqueuer() a bit shorter and

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

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

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

Exceptions • Standard error stream – abort(); // cstdlib. h, doesn’t clear buffer –

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

The exception mechanism • Three components – Throwing an exception (throw) – Catching an

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

Program flow with exceptions

Program flow with exceptions

Using objects as exceptions • Program exc_mean. h, error 4. cpp

Using objects as exceptions • Program exc_mean. h, error 4. cpp

Unwinding the stack • Program error 5. cpp

Unwinding the stack • Program error 5. cpp

More exception features

More exception features

catch (…)

catch (…)

The exception class

The exception class

The stdexception class • Classes logic_error, runtime_error derived publicly from exception • Classes domain_error,

The stdexception 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 logic_error class

The bad_alloc exception and new • Class bad_alloc derived publicly from exception 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 •

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++,

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

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

dynamic_cast operator

 • Program rtti 1. cpp

• Program rtti 1. cpp

The typeid operator & type_info class • #include <typeinfo. h> if (typeid(Magnificent) == typeid(*pg))

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

Misusing RTTI Rewrite code

Misusing RTTI Rewrite code

Type cast operators

Type cast operators

Completing type cast • • dynamic_cast const_cast static_cast reinterpret_cast

Completing type cast • • dynamic_cast const_cast static_cast reinterpret_cast

dynamic_cast

dynamic_cast

const_cast • Program constcast. cpp

const_cast • Program constcast. cpp

static_cast

static_cast

reinterpret_cast • The following type cast is allowed in C but not in C++

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