Data Abstraction: The Walls

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
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.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Abstract Data Types. Typical operations on data  Add data to a data collection  Remove data from a data collection  Ask questions about the data in.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Software Engineering Principles and C++ Classes
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 3 Data.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 3: Data Abstraction: The Walls Data Abstraction & Problem.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Introduction to Exception Handling and Defensive Programming.
Modularity Keeps the complexity of a large program manageable by systematically controlling the interaction of its components Isolates errors Eliminates.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 3: Data Abstraction: The Walls Data Abstraction & Problem.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Namespaces & Exceptions Adapted from Ch. 3 slides of Data Abstraction:
Data Structures Using C++1 Chapter 1 Software Engineering Principles and C++ Classes.
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Memory Management.
Chapter 2 Objects and Classes
C ++ MULTIPLE CHOICE QUESTION
Exception handling.
Java Exceptions a quick review….
Exception Handling in C++
Jim Fawcett CSE687-OnLine – Object Oriented Design Summer 2017
Data Abstraction: The Walls
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Abstract Data Types and Encapsulation Concepts
Why exception handling in C++?
About the Presentations
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming Using C++
EXCEPTION HANDLING.
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Introduction to Classes
Exceptions with Functions
Abstract Data Types and Encapsulation Concepts
Subprograms and Programmer Defined Data Type
Chapter 17 Templates and Exceptions Part 2
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling Chapter 9 Edited by JJ.
Exception Handling In Text: Chapter 14.
Exception Handling Oo28.
Classes and Objects.
CS360 Client/Server Programming Using Java
Introduction to Programming
Data Abstraction: The Walls
Exception Handling and Event Handling
CMSC 202 Exceptions.
Exception Handling.
ENERGY 211 / CME 211 Lecture 24 November 14, 2008.
Object Oriented Programming
Introduction to Classes and Objects
Presentation transcript:

Data Abstraction: The Walls Chapter 3

Chapter 3 -- Data Abstraction: The Walls This chapter elaborates on data abstraction as a technique for increasing the modularity of a program – for building “walls” between a program and its data structures. Only after you have clearly specified the operations of an abstract data type should you consider data structures for implementing it. This chapter explores implementation issues and introduces C++ classes as a way to hide the implementation of an ADT from its users CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls Abstract Data Types Modularity is a technique that keeps the complexity of a large program manageable by systematically controlling the interaction of its components. You should practice functional abstraction knowing what the function is to do, not how it will be done CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls The principle of information hiding involves not only hiding details, but also making them inaccessible from the outside. CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls This isolation cannot be total. Although Q does not know how T is performed, it must know what to ask T CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls Def: ADT Data Operations on that data Encapsulation CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls Specifying ADTs The ADT List Here is a UML diagram for a List CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls Implementing ADTs The choices you make at each level of implementation affect efficiency. For now our analysis will be intuitive, but Chapter 9 will introduce you to the quantitative techniques that you can use to weigh the trade-offs involved CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls C++ Classes C++ classes define a new datatype. By default, all members in a class are private – unless you designate them as public. For creation and destruction you have constructors and destructors. CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls C++ Namespaces Often, a solution to a problem will have groups of related classes and other declarations, such as functions, variables, types, and constants. C++ provides a mechanism for logically grouping these into a common declarative region known as a namespace. namespace namespaceName { // stuff here } CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls Example 1: namespace smallNamespace { int count = 0; void abc(); } Functions can be implemented directly in the namespace or appear elsewhere void smallNamespace::abc() //code here CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls You can access elements from outside the namespace by using the scope resolution operator. smallNamespace::count+=1; smallNamespace::abc(); Or you can have the following to use all the things using namespace smallNamespace; count +=1; abc(); Or you can use only one thing using smallNamespace::abc; smallNamespace::count +=1; CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls An Array-Based Implementation of the ADT List This material was covered last semester.7 CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls C++ Exceptions Many programming languages, including C++, support exceptions, which are a mechanism for handling errors. If you detect an error during execution, you can throw and exception. The code that deals with the exception is said to catch it or handle it. CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls Catching an exception To catch an exception, C++ provides try-catch blocks. Use try blocks for statements that can throw an exception try { statement(s); } Use a catch block for each type of exception that you handle catch(ExceptionClass identifier) A try block can have many catch blocks associated with it, since even a single statement might cause more than one type of exception. CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls When a statement in a try block causes an exception, the remainder of the try block is abandoned, and control passes to the statements in the catch block that correspond to the type of exception thrown Upon completion of the catch block, control resumes at the point following the last catch block. If there is no applicable catch block for an exception, abnormal termination usually occurs. Note: if an exception occurs in the middle of a try block, the destructors of all objects local to that block are called. CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls Throwing exceptions When you detect an error within a function, you can throw an exception by executing a statement with the following form: throw ExceptionClass(stringArgument); Here Exceptionclass is the type of exception you want to throw And stringArgument is an argument to the ExceptionClass constructor that provides a more detailed description of what may have caused the exception. When a throw statement executes, the remaining code in the function does not execute, and the exception is propagated back See Appendix A (pg A40-A47) for more details. CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls You can define your own exceptions. Usually, the C++ exception class exception, or one of its derived classes, is used as the base class for the exception To indicate the exceptions that will be thrown by a function, you include a throw clause in the function’s header as follows: void myMethod(int x) throw(BadArgException, MyException) { if(x==MAX) throw BadArgException(“BadArgException: reason”); // some code here … throw MyException(“MyException: reason”); } CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls The following is sample code for an exception #include <exception> #include <string> Using namespace std; Class ListException: public exception { public: ListException(const string & message=“”) : exception(message.c_str( )) { } }; CS 308 Chapter 3 -- Data Abstraction: The Walls

Chapter 3 -- Data Abstraction: The Walls CS 308 Chapter 3 -- Data Abstraction: The Walls