PPL-Unit 3 Part 2
PPL-Unit 3 Part 2
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.
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'
...