object oriented programming elhanan borenstein [email protected] lecture #4

22
Object Oriented Programming Elhanan Borenstein [email protected] Lecture #4

Upload: kory-ball

Post on 30-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

Object OrientedProgramming

Elhanan Borenstein

[email protected]

Lecture #4

Page 2: 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

Page 3: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

A Crash Courseabout

Exceptions

Page 4: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

Exceptions

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.

Motivation

Page 5: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

Exceptions

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

Usage

Page 6: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

Exceptions

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.

Exception Handling

Page 7: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

Exceptions

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.

Throwing Exceptions in C’tors and D’tors

Page 8: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

Classessome additional features

Page 9: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

Using Constructors for 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

Object Auto Casting

Page 10: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

Using Constructors for Casting

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

Using the keyword “explicit”

Page 11: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

“const” Objects and Functions

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

const Objects and Function Arguments

Page 12: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

“const” Objects and 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

const Member Functions

Page 13: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

“const” Objects and Functions

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.

const Member Functions – Some Notes

Page 14: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

“const” Objects and Functions

Data members can be defined as “mutable” A mutable data member can be changes even if the

object is const. Usage??

mutable Data Members

Page 15: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

“static” Variables and Functions

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.

static Variable in Classes - Overview

Page 16: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

“static” Variables and Functions

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.

static Variable in Classes – Some Details

Page 17: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

“static” Variables and 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

static Functions

Page 18: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

Friendship Permissions

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.

Friendship

Page 19: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

Friendship Permissions

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

Friend Global Functions

Page 20: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

Friendship Permissions

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

Friend Classes

Page 21: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

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

Page 22: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #4

Questions?