200 likes | 219 Views
Data Abstraction: The Walls. Chapter 3. 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.
E N D
Data Abstraction: The Walls Chapter 3
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 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 Chapter 3 -- Data Abstraction: The Walls
The principle of information hiding involves not only hiding details, but also making them inaccessible from the outside. 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 Chapter 3 -- Data Abstraction: The Walls
Def: ADT • Data • Operations on that data • Encapsulation Chapter 3 -- Data Abstraction: The Walls
Specifying ADTs • The ADT List • Here is a UML diagram for a List 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 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. 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 } 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 } 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; • abc(); Chapter 3 -- Data Abstraction: The Walls
An Array-Based Implementation of the ADT List • This material was covered last semester.7 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. 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) • { • statement(s); • } • 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
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. 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. 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”); } 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( )) { } }; Chapter 3 -- Data Abstraction: The Walls