Object Oriented Programming Elhanan Borenstein Lecture #4.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
1 Handling Exceptions COSC 1567 C++ Programming Lecture 11.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Object Oriented Programming Elhanan Borenstein Lecture #12 copyrights © Elhanan Borenstein.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
Object Oriented Programming Elhanan Borenstein Lecture #9 copyrights © Elhanan Borenstein.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Programming Languages and Paradigms Object-Oriented Programming.
Object Oriented Programming Elhanan Borenstein Lecture #6.
Chapter 12: Adding Functionality to Your Classes.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Object Oriented Programming
CIS 270—Application Development II Chapter 13—Exception Handling.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Learners Support Publications Classes and Objects.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Programming in Java CSCI-2220 Object Oriented Programming.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Object Oriented Programming Elhanan Borenstein Lecture #5.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
C++ 程序语言设计 Chapter 12: Dynamic Object Creation. Outline  Object creation process  Overloading new & delete.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
FIT Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Object Oriented Programming Elhanan Borenstein Lecture #7.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
The Object-Oriented Thought Process Chapter 03
Object Oriented Programming
Class: Special Topics Copy Constructors Static members Friends this
CS212: Object Oriented Analysis and Design
Andy Wang Object Oriented Programming in C++ COP 3330
Classes and Objects.
Andy Wang Object Oriented Programming in C++ COP 3330
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
More C++ Classes Systems Programming.
SPL – PS3 C++ Classes.
Presentation transcript:

Object Oriented Programming Elhanan Borenstein 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

A Crash Course about Exceptions

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

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

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

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

Classes some additional features

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

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”

“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

“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

“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

“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

“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

“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

“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

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

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

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

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

Questions?