Friend classes Friend class methods Nested classes Throwing exceptions, try blocks and catch blocks Exception classes Runtime type identification (RTTI)
Post on 18-Jan-2018
222 Views
Preview:
DESCRIPTION
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 classesSuppose 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 <typeinfo.h>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
top related