0% found this document useful (0 votes)
21 views32 pages

PPL-Unit 3 Part 2

Uploaded by

thirumal536
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views32 pages

PPL-Unit 3 Part 2

Uploaded by

thirumal536
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Principles of programming languages

Unit-III: Abstract Data types: The concept of


abstraction , introductions to data abstraction,
language examples,parameterized ADT,
encapsulation constructs, naming encapsulations
The Concept of Abstraction
•An abstraction is a view or representation of an entity
that includes only the most significant attributes.
• In a general sense, abstraction allows one to collect
instances of entities into groups in which their common
attributes need not be
considered.
•The two fundamental kinds of abstraction in
contemporary programming languages are process
abstraction and data abstraction.
•The concept of process abstraction is among the
oldest in programming language design .
•All subprograms are process abstractions because
they provide a way for a program to specify a process,
without providing the details of how it performs its
task(at least in the calling program).
•For example:sortInt(list, listLen)
The Concept of Abstraction
The widespread use of data abstraction necessarily
followed that of process abstraction because an
integral and essential part of every data abstraction is
its operations, which are defined as process
abstractions.
Introduction to Data
Abstraction
•The evolution of data abstraction began in 1960 with
the first version of COBOL, which included the record
data structure.
• The C-based languages have structs, which are also
records.
•An abstract data type is a data structure, in the form
of a record, but which includes subprograms that
manipulate its data.
•Syntactically, an abstract data type is an enclosure
that includes only the data representation of one
specific data type and the subprograms that provide
the operations for that type.
•Through access controls, unnecessary details of the
type can be hidden from units outside the enclosure
that use the type.
Introduction to Data
Abstraction
User-Defined Abstract Data Types
A user-defined abstract data type should provide the
same characteristics as
those of language-defined types,:
(1) a type definition that allows program units to
declare variables of the type but hides the
representation of objects of the type
(2) a set of operations for manipulating objects of the
type
An abstract data type is a data type that satisfies the
following conditions:
• The representation of objects of the type is hidden
from the program units that use the type, so the only
direct operations possible on those objects are those
provided in the type’s definition.
• The declarations of the type and the protocols of the
Introduction to Data
Abstraction
of the operations. Also, other program units are allowed
to create variables of the defined type.
•There are several benefits of information hiding. One
of these is increased reliability
•Another benefit of information hiding is it reduces the
range of code and number of variables
•Information hiding also makes name conflicts less
likely, because the scope of variables is smaller.
•An Example
•A stack is a widely applicable data structure that
stores some number of data elements and only allows
access to the data element at one of its ends, the top.
•Suppose an abstract data type is to be constructed for
a stack that has the following abstract operations:
Introduction to Data
Abstraction
create(stack) Creates and possibly initializes
a stack object
destroy(stack) Deallocates the storage for the
stack
empty(stack) A predicate (or Boolean)
function that returns
true if the specified stack is empty
and false
otherwise
push(stack, element) Pushes the specified element
on the specified
stack
pop(stack) Removes the top element from the
specified
stack
top(stack) Returns a copy of the top element
Language Examples
Abstract Data Types in Ada
•Ada provides an encapsulation construct that can be
used to define a single abstract data type, including
the ability to hide its representation.
•Encapsulation
•The encapsulating constructs in Ada are called
packages.
•A package can have two parts, each of which is also
is called a package.
•These are called the package specification, which
provides the interface of the encapsulation and the
body package, which provides the implementation
•A package specification and its associated body
package share the same name.
•The reserved word body in a package header identifies
it as being a
Language Examples
Information Hiding
•The designer of an Ada package that defines a data
type can choose to make the type entirely visible to
clients or provide only the interface information.
•There are two approaches to hiding the representation
from
clients in the package specification
•The representation of the type appears in a part of the
specification called the private part, which is
introduced by the reserved wordprivate.
•The private clause is always at the end of the package
specification.
•The private clause is visible to the compiler but not to
client program units.
The second way to hide the representation is to define
the abstract datatype as a pointer and provide the
Language Examples
Types that are declared to be private are called private
types
An alternative to private types is a more restricted
form: limited privatetypes. Nonpointer limited private
types are described in the private section of a package
specification
An Example
The following is the package specification for a stack
abstract data type:
package Stack_Pack is
-- The visible entities, or public interface
type Stack_Type is limited private;
Max_Size : constant := 100;
function Empty(Stk : in Stack_Type) return
Boolean;
procedure Push(Stk : in out Stack_Type;
Language Examples
The part that is hidden from clients
private
type List_Type is array (1..Max_Size) of Integer;
type Stack_Type is
record
List : List_Type;
Topsub : Integer range 0..Max_Size := 0;
end record;
end Stack_Pack;
Language Examples
Abstract Data Types in C++:
Encapsulation:
•The data defined in a C++ class are called data
members; the functions (methods) defined in a class
are called member functions. Data members and
member functions appear in two categories: class and
instance.
•Class members are associated with the class; instance
members are associated with the instances of the
class.
•Class instances can be static, stack dynamic, or heap
dynamic. If static or stack dynamic, they are
referenced directly with value variables
•Heap dynamic class instances are created with the
new operator and destroyed with the delete
operator
Language Examples
•When both the header and the body of a member
function appear in the class definition, the member
function is implicitly inlined.

Information Hiding:
•A C++ class can contain both hidden and visible
entities
•Entities that are to be hidden are placed in a private
clause and visible or public entities appear in a public
clause.

Constructors and Destructors:


•C++ allows the user to include constructor functions
in class definitions, which are used to initialize the data
members of newly created objects.
•A constructor has the same name as the class whose
Language Examples
A C++ class can also include a function called a
destructor, which is
implicitly called when the lifetime of an instance of the
class ends.
The name of a destructor is the class’s name, preceded
by a tilde (~).
An Example
Our examle of a C++ abstract data type , stack:
#include <iostream.h>
class Stack {
private: //** These members are visible only to
other
//** members and friends
int *stackPtr;
int maxLen;
int topSub;
Language Examples
topSub = -1;
}
~Stack() {delete [] stackPtr;}; //** A destructor
void push(int number) {
if (topSub == maxLen)
cerr << "Error in push--stack is full\n";
else stackPtr[++topSub] = number;
}
void pop() {
if (empty())
cerr << "Error in pop--stack is empty\n";
else topSub--;
}
int top() {
if (empty())
cerr << "Error in top--stack is empty\n";
Language Examples
return (stackPtr[topSub]);
}
int empty() {return (topSub == -1);}
}
The following is an example program that uses the
Stack abstract data
type:
void main() {
int topOne;
Stack stk; //** Create an instance of the Stack class
stk.push(42);
stk.push(17);
topOne = stk.top();
stk.pop();
...
}
Language Examples
Abstract Data Types in Java
•Java support for abstract data types is similar to that
of C++. There are, however, a few important
differences.
• All objects are allocated from the heap and accessed
through reference variables.
•Methods in Java must be defined completely in a class.
A method body must appear with its corresponding
method header.
•Definitions are hidden from clients by declaring them
to be private.
An Example
The following is a Java class definition for our stack
example:
class StackClass {
private int [] stackRef;
Language Examples
maxLen = 99;
topIndex = -1;
}
public void push(int number) {
if (topIndex == maxLen)
System.out.println("Error in push—stack is full");
else stackRef[++topIndex] = number;
}
public void pop() {
if (empty())
System.out.println("Error in pop—stack is empty");
else --topIndex;
}
public int top() {
if (empty()) {
System.out.println("Error in top—stack is empty");
Language Examples
return 9999;
}
else
return (stackRef[topIndex]);
}
public boolean empty() {return (topIndex == -
1);}
}
An example class that uses StackClass follows:
public class TstStack {
public static void main(String[] args) {
StackClass myStack = new StackClass();
myStack.push(42);
myStack.push(29);
System.out.println("29 is: " + myStack.top());
myStack.pop();
Language Examples
Stack.pop();
myStack.pop(); // Produces an error message
}
}
Language Examples
Abstract Data Types in C#
Encapsulation
•C# includes both classes and structs, which are nearly
identical constructs.
•The only difference is that the default accessmodifier
for class is private, whereas for structs it is public.
•In C#, structs are, in a sense, lightweight classes.
Information Hiding
C# uses the private and protected access
modifiers exactly as they are used in Java.
For example, consider the following simple class and
client code:
public class Weather {
public int DegreeDays { //** DegreeDays is a
property
get {
Language Examples
set {
if(value < 0 || value > 30)
Console.WriteLine(
"Value is out of range: {0}", value);
else
degreeDays = value;
}
Parameterized Abstract Data
Types
parameterized abstract data types are supported
Ada,C++, Java 5.0, and C# 2005.
Ada
•Packages can also be generic, so we can construct
generic, or parameterized abstract data types.
•The Ada stack abstract data type suffers two
restrictions:
(1) Stacks of its type can store only integer type
elements, and
(2) the stacks can have only up to 100 elements.
•Both of these restrictions can be eliminated by using a
generic package, which can be instantiated for other
element types and any desirable size.
Example:
generic
Max_Size : Positive; -- A generic parameter for stack
Parameterized Abstract Data
Types
C++:
C++ also supports parameterized abstract data types.
C++ stack class generic in the stack size, only the
constructor
function needs to be changed.
Stack(int size) {
stackPtr = new int [size];
maxLen = size - 1;
topSub = -1;
}
Java 5.0
•Java 5.0 supports a form of parameterized abstract
data types in which the generic parameters must be
classes.
•The most common generic types are collection types,
such as LinkedList and ArrayList, which were in the Java
Parameterized Abstract Data
Types
Example:
//* Create an ArrayList object
ArrayList myArray = new ArrayList();
//* Create an element
myArray.add(0, new Integer(47));

C# 2005:
•Generic classes were added to C# in its 2005 version.
The five predefined generic collections are Array, List,
Stack, Queue, and Dictionary.
•As with Java 5.0, users can define generic classes in
C# 2005.
Encapsulation Constructs
•Encapsulations are often placed in libraries and made
available for reuse in programs other than those for
which they were written.
Encapsulation in C
•C does not provide complete support for both abstract
data types and multiple-type encapsulations .
•In C, a collection of related functions and data
definitions can be placed in a file, which can be
independently compiled.
•Such a file, which acts as a library,has an
implementation of its entities.
•The interface to such a file, including data, type, and
function declarations, is placed in a separate file called
a header file.
•The header file, in source form, and the compiled
version of the implementation file are furnished to
Encapsulation Constructs
Encapsulation in C++
•C++ provides two different kinds of encapsulation—
header and implementation files can be defined as in
C, or class headers and definitions can be defined.
•Because of the complex interplay of C++ templates
and separate compilation, the header files of C++
template libraries often include complete definitions of
resources.
Ada Packages
•Ada package specifications can include any number of
data and subprogram declarations in their public and
private sections. Therefore, they can include interfaces
for any number of abstract data types, as well as any
other program resources.
•So, the package is a multiple-type encapsulation
construct.
Naming Encapsulations
•Naming encapsulations define name scopes that assist
in avoiding these name conflicts.
•Each library can create its own naming encapsulation
to prevent
its names from conflicting with the names defined in
other libraries or in client code.
•naming encapsulations supported by C++, Java, Ada,
and Ruby.

C++ Namespaces
•C++ includes a specification, namespace, that helps
programs manage the problem of global namespaces.
• One can place each library in its own namespace and
qualify the names in the program with the name of the
namespace when the names are used outside that
namespace.
Naming Encapsulations
•The implementation file for the stack abstract data
type could reference the names declared in the header
file with the scope resolution operator, ::, as in
myStackSpace::topSub
•Client code can gain access to the names in the
namespace of the header file of a library in three
different ways.
•One way is to qualify the names from the library with
the name of the namespace
myStackSpace::topSub
•The other two approaches use the using directive.
using myStackSpace::topSub;
using namespace myStackSpace;
Naming Encapsulations
Java Packages
•Java includes a naming encapsulation construct: the
package.
Entities without access modifiers are said to have
package scope, because they are visible throughout
the package
Example Package with a package declaration,
package stkpkg;
•The clients of a package can reference the types
defined in the package usingfully qualified names. For
example, if the package stkpkg has a class named
myStack, that class can be referenced in a client of
stkpkg as stkpkg.myStack.
import stkpkg.myStack;
•For example, if we wanted to import all of the types in
stkpkg, we could use the following:
Naming Encapsulations
Ada Packages
Ada packages also define namespaces. Visibility to a
package from a program unit is gained with the with
clause.
Example:
with Ada.Text_IO;
To access the names in Ada.Text_IO without
qualification, the use clause can be used, as in
use Ada.Text_IO;

Ruby Modules
•Ruby classes serve as namespace encapsulations
•Ruby has an additional naming encapsulation, called a
module
•Modules typically define collections of methods and
constants.
Naming Encapsulations
For example, consider the following skeletal module
definition:
module MyStuff
PI = 3.14159265
def MyStuff.mymethod1(p1)
...
end
def MyStuff.mymethod2(p2)
...
end
End
a program that wantsto use the constant and methods
of MyStuff must gain access to the module. This is
done with the require method
require 'myStuffMod'
...

You might also like