BAB VIII
Abstract Data Types
Abstract Data Types or ADT is a user-defined data types.
Also, can be explained this way, we have a primitive data
types (e.g. char, float). But sometimes we want to store the data as one (e.g.
Names, Address, telephone number). We then can declare an abstract data types
to handle that.
In an ADT, we can create our own data types using the
existing primitive data types or even object, and name the ADT whatever we
want.
The class is the encapsulation device
· A class is a
type
· All of the
class instances of a class share a single copy of the member functions
· Each
instance of a class has its own copy of the class data members
· Instances
can be static, stack dynamic, or heap dynamic
· Information
Hiding
o Private clause for
hidden entities
o Public clause for
interface entities
o Protected clause
for inheritance
·
Constructors:
o Functions to
initialize the data members of instances (they do not create the objects)
o May also allocate
storage if part of the object is heap-dynamic
o Can include
parameters to provide parameterization of the objects
o Implicitly called
when an instance is created
o Can be explicitly
called
o Name is the same
as the class name
· Destructors
o Functions to
cleanup after an instance is destroyed; usually just to reclaim heap storage
o Implicitly called
when the object’s lifetime ends
o Can be explicitly
called
o Name is the class
name, preceded by a tilde (~)
· Example in
C++
class Stack {
private:
int *stackPtr,
maxLen, topPtr;
public:
Stack() { // a
constructor
stackPtr = new int [100];
maxLen = 99;
topPtr = -1;
};
~Stack ()
{delete [] stackPtr;};
void push (int
number) {
if (topSub
== maxLen)
cerr
<< ″Error in push - stack is full\n″;
else
stackPtr[++topSub] = number;
};
void pop ()
{…};
int top () {…};
int empty ()
{…};
}
· Friend
functions or classes - to provide access to private members to some unrelated
units or functions
·
Necessary in C++
Encapsulation Constructs
· Large programs
have two special needs:
o Some means of
organization, other than simply division into subprograms
o Some means of
partial compilation (compilation units that are smaller than the whole program)
· Obvious
solution: a grouping of subprograms that are logically related into a unit that
can be separately compiled (compilation units)
· Such
collections are called encapsulation
Nested Subprograms
· Organizing
programs by nesting subprogram definitions inside the logically larger subprograms
that use them
· Nested
subprograms are supported in Ada, Fortran 95+, Python, JavaScript, and Ruby
Encapsulation in C
· Files
containing one or more subprograms can be independently compiled
· The
interface is placed in a header file
· Problem: the
linker does not check types between a header and associated implementation
· #include
preprocessor specification – used to include header files in applications
Encapsulation in C++
· Can define
header and code files, similar to those of C
· Or, classes
can be used for encapsulation
o The class is used
as the interface (prototypes)
o The member
definitions are defined in a separate file
· Friends
provide a way to grant access to private members of a class
Naming Encapsulations
· Large
programs define many global names; need a way to divide into logical groupings
· A naming
encapsulation is used to create a new scope for names
· C++ Namespaces
o Can place each
library in its own namespace and qualify names used outside with the namespace
o C# also includes
namespaces
· Java
Packages
o Packages can
contain more than one class definition; classes in a package are partial friends
o Clients of a
package can use fully qualified name or use the import declaration
· Ruby classes
are name encapsulations, but Ruby also has modules
· Typically
encapsulate collections of constants and methods
· Modules
cannot be instantiated or subclassed, and they cannot define variables
· Methods
defined in a module must include the module’s name
· Access to
the contents of a module is requested with the require method
Tidak ada komentar:
Posting Komentar