Sie sind auf Seite 1von 24

Object Oriented Programming

Lecture #4 Elhanan Borenstein borens@tau.ac.il

copyrights Elhanan Borenstein

Agenda
A Crash Course on Exceptions Classes Some Additional Features Using Constructors for Casting const Objects and Functions static Variables and Functions Friendship Inner type/class

copyrights Elhanan Borenstein

A Crash Course
on

Exceptions

copyrights Elhanan Borenstein

Exceptions
Common runtime errors issues:
Memory Files Numeric operations I/O

What do these errors have in common ?

Programmer (Library, classes)

User (Application)

Detection

Handling
copyrights Elhanan Borenstein

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.
copyrights Elhanan Borenstein

Exceptions
Usage
Exception handling is implemented with 3 keywords:
throw when an error occurs 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. void FuncA () { . . throw }
void FuncB() { try { FuncA() . } catch () { } }
copyrights Elhanan Borenstein

Example: exm_exc

Exceptions
Exception Handling
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 exception type, the first will be used. catch () will can catch all types. Should be last!!! An exception can be thrown again (using throw command) A function can declare which exception it may throw: FuncA() or FuncA() throw(int,char*) or FuncA() throw()
copyrights Elhanan Borenstein

Exceptions
Any type can be thrown as exception. It is common to throw objects of a certain class family.
class CExp { public: void Print() { cout<<General Error; } }; class CExpFile { public: void Print() { cout<<File Error; } }; class CExpErrorCode { int Code; public: CExpErrorCode(int a_code) { } void Print() { cout<<Code<<Code; } }; . . . throw CExpErrorCode(23); . . .

catch(CExpErrorCode& e) { e.Print(); } . .

copyrights Elhanan Borenstein

Exceptions
Throwing Exceptions in Ctors and Dtors
Naturally, exceptions are very suitable for constructors (no return value!!!) If the Ctor did not terminate properly (an exception was thrown), the dtor 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.

copyrights Elhanan Borenstein

Classes
some additional features

copyrights Elhanan Borenstein

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

copyrights Elhanan Borenstein

Using Constructors for Casting


Using the keyword explicit
There are cases 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

copyrights Elhanan Borenstein

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

copyrights Elhanan Borenstein

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 members arent 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
copyrights Elhanan Borenstein

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.

copyrights Elhanan Borenstein

const Objects and Functions


mutable Data Members
Data members can be defined as mutable
i.e. : mutable int Number;

A mutable data member can be changes even if the object is const. Usage??

copyrights Elhanan Borenstein

static Variables and Functions


static Variables in Classes - Overview
A Static variable can be defined within in a function. (i.e. static int i;) C
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.

C++

It can be accessed from any member function of that class. ANDit is shared among all objects/instances of that class.

copyrights Elhanan Borenstein

static Variables and Functions


static Variables 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 defined 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 cannot be initialized in the initialization line.
copyrights Elhanan Borenstein

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

copyrights Elhanan Borenstein

Friendship Permissions
Friendship
As we recall, private members are accessible only to member functions of that class. The class can, however, allow certain functions or classes to access 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.

copyrights Elhanan Borenstein

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

copyrights Elhanan Borenstein

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: cline_friend

copyrights Elhanan Borenstein

Inner Types/Class
A class or an enum can be defined 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

copyrights Elhanan Borenstein

Questions?

copyrights Elhanan Borenstein

Das könnte Ihnen auch gefallen