Chapter 3: Data Abstraction
- Abstraction, modularity, information hiding
- Abstract data types
- Example-1: List ADT
- Example-2: Sorted list ADT
- C++ Classes
- C++ Namespaces
- C++ Exceptions
EECS 268 Programming II 1
Chapter 3: Data Abstraction Abstraction, modularity, information - - PowerPoint PPT Presentation
Chapter 3: Data Abstraction Abstraction, modularity, information hiding Abstract data types Example-1: List ADT Example-2: Sorted list ADT C++ Classes C++ Namespaces C++ Exceptions EECS 268 Programming II 1 Modularity
EECS 268 Programming II 1
2 EECS 268 Programming II
3 EECS 268 Programming II
4 EECS 268 Programming II
5 EECS 268 Programming II
6
Figure 3-2 A slit in the wall
EECS 268 Programming II
7 EECS 268 Programming II
8
Figure 3-4 A wall of ADT operations isolates a data structure from the program that uses it
EECS 268 Programming II
9 EECS 268 Programming II
10 EECS 268 Programming II
11 EECS 268 Programming II
12 EECS 268 Programming II
13 EECS 268 Programming II
see Table on pages 128-129
14 EECS 268 Programming II
15 EECS 268 Programming II
16 EECS 268 Programming II
17 EECS 268 Programming II
18
Figure 3-13 (a) Deletion causes a gap; (b) fill gap by shifting
19 EECS 268 Programming II
20 EECS 268 Programming II
21 EECS 268 Programming II
see Table on pages 133-134
22 EECS 268 Programming II
EECS 268 Programming II 23
Figure 3-8 ADT operations provide access to a data structure
24
Figure 3-9 Violating the wall of ADT operations
EECS 268 Programming II
25 EECS 268 Programming II
EECS 268 Programming II 26
Figure 3-10 An object’s data and methods are encapsulated
27 EECS 268 Programming II
28 EECS 268 Programming II
29 EECS 268 Programming II
30 EECS 268 Programming II
/** @file Sphere.h */ const double PI = 3.14159; class Sphere { public: Sphere(); // Default constructor Sphere(double initialRadius); // Constructor void setRadius(double newRadius); double getRadius() const; // can’t change data members double getDiameter() const; double getCircumference() const; double getArea() const; double getVolume() const; void displayStatistics() const; private: double theRadius; // data members should be private }; // end Sphere
31 EECS 268 Programming II
/** @file Sphere.cpp */ #include <iostream> #include "Sphere.h" // header file using namespace std; Sphere::Sphere() : theRadius(1.0) { } // end default constructor Sphere::Sphere(double initialRadius) { if (initialRadius > 0) theRadius = initialRadius; else theRadius = 1.0; } // end constructor
32 EECS 268 Programming II
void Sphere::setRadius(double newRadius) { if (newRadius > 0) theRadius = newRadius; else theRadius = 1.0; } // end setRadius
33 EECS 268 Programming II
double Sphere::getRadius() const { return theRadius; } // end getRadius . . . double Sphere::getArea() const { return 4.0 * PI * theRadius * theRadius; } // end getArea . . .
34 EECS 268 Programming II
#include <iostream> #include "Sphere.h" // header file using namespace std; int main() // the client { Sphere unitSphere; Sphere mySphere(5.1); cout << mySphere.getDiameter() << endl; . . . } // end main
35 EECS 268 Programming II
36 EECS 268 Programming II
37 EECS 268 Programming II
#include “Sphere.h” enum Color {RED, BLUE, GREEN, YELLOW}; class ColoredSphere: public Sphere { public: … Color getColor() const;
…
private:
Color c; } // end ColoredSphere
38 EECS 268 Programming II
39 EECS 268 Programming II
40
see C3-namespace.cpp
EECS 268 Programming II
41 EECS 268 Programming II
42 EECS 268 Programming II
43
see C3-exceptions.cpp
EECS 268 Programming II
void myMethod(int x) throw(MyException) { if (. . .) throw MyException(“MyException: …”); . . . } // end myMethod
44 EECS 268 Programming II
#include <stdexcept> #include <string> using namespace std; class ListIndexOutOfRangeException : public out_of_range { public: ListIndexOutOfRangeException(const string & message = “”)
: out_of_range(message.c_str()) {}
}; // end ListException
45 EECS 268 Programming II
#include <stdexcept> #include <string> using namespace std; class ListException : public logic_error { public: ListException(const string & message = “”)
: logic_error(message.c_str()) {}
}; // end ListException
46 EECS 268 Programming II
/** @file ListAexcept.h */
#include “ListException.h” #include “ListIndexOutOfRangeException.h”
. . . class List { public: . . . void insert(int index, const ListItemType& newItem) throw(ListIndexOutOfRangeException, ListException); . . . } // end List
47 EECS 268 Programming II
/** @file ListAexcept.cpp */ void List::insert(int index, const ListItemType& newItem) throw(ListIndexOutOfRangeException, ListException); { if (size > MAX_LIST) throw ListException(“ListException: ” + “List full on insert”); . . . } // end insert
48 EECS 268 Programming II
49 EECS 268 Programming II
50 EECS 268 Programming II