Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Friend classes Friend class methods Nested classes Throwing exceptions, try blocks and catch blocks Exception classes Runtime type identification (RTTI)"— Presentation transcript:

1 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

2 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

3 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

4 Friend member functions

5 Friend relationships Program tvfm.h

6 Interactive remote controller

7 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

8 Shared friends

9 Nested classes Suppose queue class declaration:

10 Methods definitions

11 More appropriate declaration

12 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

13 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

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

15 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

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

17 Program flow with exceptions

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

19 Unwinding the stack Program error5.cpp

20 More exception features

21

22 catch (…)

23 The exception class

24 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

25 The logic_error class

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

27 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

28 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

29 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

30 dynamic_cast operator

31 Program rtti1.cpp

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

33 Misusing RTTI Rewrite code

34 Type cast operators

35 Completing type cast dynamic_cast const_cast static_cast reinterpret_cast

36 dynamic_cast

37 const_cast Program constcast.cpp

38 static_cast

39 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


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

Similar presentations


Ads by Google