Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Abstraction: The Walls

Similar presentations


Presentation on theme: "Data Abstraction: The Walls"— Presentation transcript:

1 Data Abstraction: The Walls
Chapter 3

2 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

3 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

4 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

5 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

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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

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


Download ppt "Data Abstraction: The Walls"

Similar presentations


Ads by Google