Data Abstraction The Walls Chapter 3 CS 308
Data Abstraction: The Walls Chapter 3 CS 308 Chapter 3 -- Data Abstraction: The Walls
n 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. n Only after you have clearly specified the operations of an abstract data type should you consider data structures for implementing it. n 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 2
Abstract Data Types Modularity is a technique that keeps the complexity of a large program manageable by systematically controlling the interaction of its components. n You should practice functional abstraction n n CS 308 knowing what the function is to do, not how it will be done Chapter 3 -- Data Abstraction: The Walls 3
n 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 4
n This isolation cannot be total. n CS 308 Although Q does not know how T is performed, it must know what to ask T Chapter 3 -- Data Abstraction: The Walls 5
n Def: ADT Data n Operations on that data n Encapsulation n CS 308 Chapter 3 -- Data Abstraction: The Walls 6
Specifying ADTs n The ADT List n CS 308 Here is a UML diagram for a List Chapter 3 -- Data Abstraction: The Walls 7
Implementing ADTs n 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 8
C++ Classes n n CS 308 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. Chapter 3 -- Data Abstraction: The Walls 9
C++ Namespaces n n n 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. Name { // stuff here } CS 308 Chapter 3 -- Data Abstraction: The Walls 10
n Example 1: namespace small. Namespace { int count = 0; void abc(); } n Functions can be implemented directly in the namespace or appear elsewhere void small. Namespace: : abc() { //code here } CS 308 Chapter 3 -- Data Abstraction: The Walls 11
n You can access elements from outside the namespace by using the scope resolution operator. n n n Or you can have the following to use all the things n n using namespace small. Namespace; count +=1; abc(); Or you can use only one thing n n n CS 308 small. Namespace: : count+=1; small. Namespace: : abc(); using small. Namespace: : abc; small. Namespace: : count +=1; abc(); Chapter 3 -- Data Abstraction: The Walls 12
An Array-Based Implementation of the ADT List n n CS 308 This material was covered last semester. 7 Chapter 3 -- Data Abstraction: The Walls 13
C++ Exceptions n n Many programming languages, including C++, support exceptions, which are a mechanism for handling errors. n If you detect an error during execution, you can throw and exception. n The code that deals with the exception is said to catch it or handle it. CS 308 Chapter 3 -- Data Abstraction: The Walls 14
n Catching an exception n CS 308 To catch an exception, C++ provides try-catch blocks. n Use try blocks for statements that can throw an exception n try n { n statement(s); n } n Use a catch block for each type of exception that you handle n catch(Exception. Class identifier) n { n statement(s); n } n A try block can have many catch blocks associated with it, since even a single statement might cause more than one type of exception. Chapter 3 -- Data Abstraction: The Walls 15
n When a statement in a try block causes an exception, n n n CS 308 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. n If there is no applicable catch block for an exception, abnormal termination usually occurs. n Note: if an exception occurs in the middle of a try block, the destructors of all objects local to that block are called. Chapter 3 -- Data Abstraction: The Walls 16
n Throwing exceptions n When you detect an error within a function, you can throw an exception by executing a statement with the following form: n throw Exception. Class(string. Argument); n Here Exceptionclass is the type of exception you want to throw And string. Argument is an argument to the Exception. Class constructor that provides a more detailed description of what may have caused the exception. n n When a throw statement executes, the remaining code in the function does not execute, and the exception is propagated back n CS 308 See Appendix A (pg A 40 -A 47) for more details. Chapter 3 -- Data Abstraction: The Walls 17
n You can define your own exceptions. n n 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 my. Method(int x) throw(Bad. Arg. Exception, My. Exception) { if(x==MAX) throw Bad. Arg. Exception(“Bad. Arg. Exception: reason”); // some code here … throw My. Exception(“My. Exception: reason”); } CS 308 Chapter 3 -- Data Abstraction: The Walls 18
n The following is sample code for an exception #include <exception> #include <string> Using namespace std; Class List. Exception: public exception { public: List. Exception(const string & message=“”) : exception(message. c_str( )) {} }; CS 308 Chapter 3 -- Data Abstraction: The Walls 19
CS 308 Chapter 3 -- Data Abstraction: The Walls 20
- Slides: 20