1 / 22

Object Oriented Programming

Object Oriented Programming. Elhanan Borenstein borens@tau.ac.il. Lecture #4. Agenda. A Crash Course about Exceptions Classes – Some Additional Features Using Constructors for Casting const Objects and Functions static Variables and Functions Friendship Inner type/class namesapce.

skapp
Download Presentation

Object Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object OrientedProgramming Elhanan Borenstein borens@tau.ac.il Lecture #4

  2. Agenda • A Crash Course about Exceptions Classes – Some Additional Features • Using Constructors for Casting • const Objects and Functions • static Variables and Functions • Friendship • Inner type/class • namesapce

  3. A Crash CourseaboutExceptions

  4. Exceptions Motivation • As there may be certain actions which can cause run-time problems, we need some sort of mechanism to report and handle these potential errors. • We can use the return-value mechanism. This mechanism have several problems: • We may forget to check the return value. • There may not be an available value we can use. Example!!! • We have to check the return value for each and every function separately. • The solution: Exceptions!!! • The function does not use the return value but rather throws an exception. • The calling code will include a special block to handle exceptions.

  5. Exceptions Usage • Exception handling is implemented with 3 keywords: • throw – when an error occurred in a function it will use “throw” to throw a certain object (of any type). • try – the “problematic” function call, will be nested within a “try” block. • catch – one or more catch blocks should immediately follow the try block. if the function throws an exception, this block will handle it. • Example: exm_exc

  6. Exceptions Exception Handling • Any type can be thrown as exception. It is common to throw object of certain class family. • If the exception is not caught, it will percolate up, (function after function) until reaching the main (where it will abort). • Conclusions: Somewhere, sometime, someone is going to pay!!! • No casting takes place!!! • If more than one catch block fits the excpetion type, the first will be used. • catch (…) will can catch all types. Should be last!!! • An exception can be throws again (using “throw” command) • A function can declare which exception it may throw.

  7. Exceptions Throwing Exceptions in C’tors and D’tors • Naturally, exceptions are very suitable to constructors (no return value!!!) • If the C’tor did not terminate properly (an exception was thrown), the d’tor will not be activated. Allocated memory should be released before throwing the exception or in the catch block. • More on that when we get to inheritance and polymorphism.

  8. Classessome additional features

  9. Using Constructors for Casting Object Auto Casting • When the parameter we pass to a function is not of the same type as the function expects, the compiler: • If possible - perform auto casting • Otherwise - complication error • The same is true for a function that expects an object. • The casting in this case will be done using the object constructor. • Example: cpoint_cast

  10. Using Constructors for Casting Using the keyword “explicit” • There are case were (auto) casting does not seem appropriate. It may cause confusion and various bugs… • Example: cpolygon_bad • We can forbid a Constructor to be used for casting (unless explicitly instructed) by adding the keyword “explicit”. • Example: cpolygon_good

  11. “const” Objects and Functions const Objects and Function Arguments • A function argument can be defined as “const”. It will ensure that the function will not perform any modifications to the object (although it was sent ByRef). • An object can also be defined as “const”. It will ensure that no one will change any of its values (members) after it was constructed. • A const object will always have a constructor. WHY? • A const object can be sent as an argument only to functions that defined the argument as const (although non-const objects can be sent as well). • Example: cpoint_const

  12. “const” Objects and Functions const Member Functions • In the previous slide, we saw how to force a function not to change objects passed to it as arguments, but… • what about member functions? • They have direct access to the object data members!!! • The object is not passed as an argument. • Can the compiler check that data member aren’t being changed? • The programmer has to indicate which member functions do not change the object data members (using the keyword “const”after the function name). Only these functions can be activated on a const object. • Example: cpoint_const_method

  13. “const” Objects and Functions const Member Functions – Some Notes • The keyword “const” should obviously appear on both the function prototype and implementation. • The keyword “const” should appear at the end of the function declaration line. WHY? • Two identical functions can be overloaded if one is defined as const. The const function will be used when activating the member function of a const object. • A few notes about compilation errors.

  14. “const” Objects and Functions mutable Data Members • Data members can be defined as “mutable” • A mutable data member can be changes even if the object is const. • Usage??

  15. “static” Variables and Functions static Variable in Classes - Overview • A Static variable can be defined within in a function. • It will be defined throughout the application executing. • It can be accessed only from within the function. • AND… the same instance of the variable will be used in each call to the function. • A static variable can also be defined within a class. • It will be defined throughout the application executing. • It can be accessed from any member function of that class. • AND…it is shared among all objects of that class.

  16. “static” Variables and Functions static Variable in Classes – Some Details • The static variable deceleration in the class does not account for memory allocation. Thus: • It will not be included in the object size. • The variable should be define and initialized somewhere within the cpp code. • including the header file will allow accessing this variable in other files though. • Example: student • A couple of notes: • Since the static variable is defined regardless of the object, it can be used as a default value in the constructor. • But….it can not be initialized in the init line.

  17. “static” Variables and Functions static Functions • Since the static variable exists from the moment the application starts (and does not really belong to any object), it can be accessed even if no object exists, through its full name. • If the static variable is private – we must use a public method to access it. • The function should be defined as static (which will guarantee it will not access any non-static data members… • and thus, it can be activated without an object, through its full name. • Example: student2

  18. Friendship Permissions Friendship • As we recall, private members are accessible only to member functions of that class. • The class can, however, permit certain functions or classes access to its private members using the keyword “friend”. • This mechanism should be used wisely as it somewhat violates the concepts of OOP • Friend permissions can be given to global functions or classes.

  19. Friendship Permissions Friend Global Functions • A function can be declared within the class as a “friend”. • This function will be a global function(!!!) and can access private data members. • A friend function cannot be defined as const. WHY? • Example: cpoint_friend

  20. Friendship Permissions Friend Classes • A different class can be declared within the class as a “friend”. • This class will be able to access (and change) private data members. • Example: cpoint_friend

  21. Inner Types/Class • A class or an enum can be define within another class. • When will we use inner classes? • inner types/class can be defined as either private or public. • A public inner class can be used anywhere in the application by using its full name: • ext_class::inner_class • ext_class::inner_class::func(…) • Example – link list

  22. Questions?

More Related