Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 12  Namespaces  Exceptions  Type casting  Pre-processor directives.

Similar presentations


Presentation on theme: "Lecture 12  Namespaces  Exceptions  Type casting  Pre-processor directives."— Presentation transcript:

1 Lecture 12  Namespaces  Exceptions  Type casting  Pre-processor directives

2 Namespaces  :: – The scope operator allows us to access elements within a namespace or known class/struct/enum  A namespace can encapsulate functions, classes, structs and enums  A namespace allows elements with the same name to exist  Anything not in a namespace is part of the global namespace

3

4 Exceptions  Exceptions can be used to catch runtime errors  Exceptions are held within a try {} catch(int e) {} block.  The catch block can be used to identify the exception that occurs through a function argument  (…) ellipsis can be used to catch any exception (default exception handling  The catch value is thrown using the throw keyword

5 Exceptions cont.  The C++ standard library provides an exception base class using #include  It has a virtual function called ‘what’ which returns a char*  This class can be used to throw detailed exception messages defined in the derived classes

6 Standard exceptions exceptiondescription bad_alloc thrown by new on allocation failure bad_cast thrown by dynamic_cast when it fails in a dynamic cast bad_exception thrown by certain dynamic exception specifiers bad_typeid thrown by typeid bad_function_callthrown by empty function objects bad_weak_ptr thrown by shared_ptr when passed a bad weak_ptr

7

8 Basic exceptions

9 Using standard lib exceptions

10 Custom exceptions

11 Preprocessor  Preceded by a #  There is no end line statement (;)  Can only extend through multiple lines using a backslash  #define fact(f,n) for (f=1; (n); (n)--)\ f*=n;  Is examined before compilation

12 Preprocessor  #define – defines a name as a value  #undef – removes that definition  #ifdef IDENTIFIER_NAME – If it is defined this code can be compiled  #ifndef – If the identifier is not defined, run this code  #if #elif  #error used to throw a compile error

13 Preprocessor cont.  #include – used to include files  #line [“filename”] #line 13 “myfile.txt”  #pragma – compiler specific macros, GCC generally avoids #pragma directives but includes some for general compatibility

14

15 Always defined macros  __FILE__ – name of source file being compiled  __DATE__ Mmm dd yyyy of compilation start  __TIME__ hh:mm:ss time of compilation start  __cplusplus

16 Technical demo 9/10 th of April Course representation Ball (moving) Ball wall collisions Ball obstacle collisions Ball hole collision Putting stick/indicator Putting stick interaction with ball Software engineering Data structures use OpenGL feature use Variations on standard behaviour GUI Extras


Download ppt "Lecture 12  Namespaces  Exceptions  Type casting  Pre-processor directives."

Similar presentations


Ads by Google