unit 3 - wordpress.com€¦ · 3.1 encapsulation in c++ class is a unit of modularity in c++...
TRANSCRIPT
Unit 3
Structuring the program
Index
• Software design method
• Concepts in support of modularity
– Encapsulation Interface and implementation
– Separate and independent compilation Libraries of modules
• Language features for programming in the large
– Program organization Grouping of units
– Abstract data types classes and modules
• Generic units
– Generic data structures Generic algorithms
– Generic modules Higher levels of genericity
• Programming paradigms
– Introduction to programming paradigms
– Introduction to four main Programming paradigms- procedural, Object oriented,
functional, and logic & rule based
• Study of Java as Object oriented programming language.
About Chapter
• This chapter deals strictly with issues of programming in the large.
• We describe the basic concepts for structuring large programs
(encapsulation, interfaces, information hiding) and the mechanisms
provided by languages to support it (packaging, separate compilation).
• We also consider the concept of genericity in building software
component libraries.
• The production of large programs presents challenging problems that do
not arise when developing smaller programs. The same methods and
techniques that work well with small programs just do ’t scale up.
• Two fundamental principles—abstraction and modularity—underlie all
approaches to programming in the large.
• Abstraction allows us to understand and analyze the problem by
concentrating on its important aspects. Modularity allows us to design and
build the program from smaller pieces called modules.
A problem of large size impose some restrictions on solution strategy
and following requirements :
• The system has to function correctly.
• The syste is lo g-lived.
• During its lifetime, the system undergoes considerable modification.
• Because of the magnitude of the problem, many people—tens or
hundreds— are involved in the development of the system.
1. Software design method
• Complexities of programming in large can be dealt with composition of
large program out of smaller units, called as modules.
• A good design is composed of modules that interact with one another in
well-defined and controlled ways.
• Each module can be designed, understood, and validated independently of
the other modules.
• The Goal of software design methods is decompose large system into
modules.
• Programming in the large addresses the problem of modular system
decomposition, and programming in the small refers to the production of
individual modules.
• Modularity can be achieved by using Information hiding. Module hides
design decision as its secret.
System designed using highly independent modules supports following
requirements of large programming :
• Independent modules form the basis of work assignment to individual
team members. Teams can work more independently with more
independent modules.
• The correctness of the entire system may be based on the correctness of
the individual modules.
• Defects in the system may be repaired. Modification isolated to individual
modules enhance system easily.
2. Concepts in support of modularity
• Modularity provide abstraction. A good module is that which
provide useful abstraction.
• It will provide mechanism to interact with other modules and
mechanism to access required information only.
• In this section we will see various functionalities that support
modularity like encapsulation, interface, compilation mechanisms,
defining library of module, an so on.
• Programs can be built in terms of constituent modules by using
programming language concepts and facilities.
• This chapter deals with programming language concepts and
facilities to divide program into subparts (modules), relationship
among those modules and the extent to which program
decompositions can mirror the decomposition of the design.
2.1 Encapsulation
• Definition : To group together the program components that combine to
provide a service and to make only the relevant aspects visible to user.
• It supports implementation of Information hiding modules.
• Through the use of encapsulation a module is described using : the
specification and the implementation.
• The specification describe access method for modules service and the
implementation describe internal structure of module.
Eg: Dictionary implementation in an program. A pair of string
components can be input for insert, delete and search operation (any one
input required). Here encapsulation ensure that the internal structure of
the dictionary unit is hidden from the clients.
• Different languages provide different encapsulation facilities. Eg: File is
encapsulation unit in C language. Typically, entities declared at the head of
fie are visible in that file and can be made available in other files.
Eg: extern int max; //external (stored) variable to be used locally
class dict {
public:
dict(); //to initialize a dictionary
~dict(); //to remove a dictionary
void insert (char* c, int i);
int lookup(char* c);
remove (char* c);
private:
struct node {
node* next;
char* name;
int id
};
node* root;
};
In C++, class construct is used to hide
information about implementation of
program unit.
Only public function/entities are visible to
user and private entities are not.
• Encapsulated types
Eg : The built-in types of a language.
(Representation of instance is hidden and
only legal operations are permitted)
2.2 Interface and implementation
• A module exports some of the encapsulated entities using interface. The collection of interfaces of the exported entities is used as module interface.
• Client can request service through module’s interface. The interface provide syntax to request module service.
• A service provider exports a set of entities to its clients. A client module imports those entities to be able to use the services of the provider module.
• Languages have implicit and explicit mechanism to import and export entities. Languages differ in entities supported to be export.
• Eg : Simplest interface can be int max(int& a, int& b); //pass two references for int value and return int value.
• Ideally, the interface would specify the semantics and the requirements on parameters (for example that they must be positive integers).
• In C++, where the unit of encapsulation is a class, the interface to the class consists of the interfaces of all the member functions of the class that are available to clients as well as any other entities,
• Ada treats the separation of interface and implementation quite strictly. In
Ada, the unit of encapsulation is a package. A package encapsulates a set
of entities such as procedures, functions, variables, and types.
• The Ada package supports encapsulation by requiring the interface of a
package (called package specification) to be declared separately from the
implementation of the package (called package body).
• Example for package specification and package body is explained on next
slide:
Package Specification :
package Package_With_Body is
type Basic_Record is private;
procedure Set_A (This : in out Basic_Record;
An_A : in Integer);
function Get_A (This : Basic_Record) return Integer;
private
type Basic_Record is
record
A : Integer;
end record ;
pragma Pure_Function (Get_A); -- not a standard Ada pragma
pragma Inline (Get_A);
pragma Inline (Set_A);
end Package_With_Body;
• Package body :
package body Package_With_Body is
procedure Set_A (This : in out Basic_Record; An_A : in Integer) is
begin
This.A := An_A;
end Set_A;
function Get_A (This : Basic_Record) return Integer is
begin
return This.A;
end Get_A;
end Package_With_Body;
2.3 Separate and Independent Compilation
• The capability of compiling parts of a program without compiling the whole
program is essential to the construction of large software systems.
• Thus languages that are designed for such applications must allow this kind of
compilation. With such a capability, only the modules of a system that are
being changed need to be recompiled during development or maintenance.
Newly compiled and previously compiled units are collected by a program
called linker, which is a part of the operating system. Without this capability,
every change to a system would require a complete recompilation. In a large
system, this is costly.
• The term separate compilation means that compilation unit can be compiled at
different times, but their compilations are not independent of each other if
either accesses or uses any entities of the other.
– To provide reliable separate compilation of a unit, the compiler must have
access to information about program entities (variables, types and sub
programs, including their interfaces) that the units uses but that are
declared elsewhere.
– Languages: Ada, Modula-2, FORTRAN 90
• With independent compilation, program units can be compiled without
information about any other program units.
– An important characteristic of independent compilation is that the
interfaces between the separately compiled units are not checked for
consistency.
– Eg : The interface of FORTRAN 77 subroutine is its parameter list.
When a subroutine is independently compiled, the types of its
parameter are not stored with the compiled code or in a library.
– Languages : C, FORTRAN 77
– Some languages provide neither separate nor independent compilation,
meaning that the only compilation unit is a complete program.
FORTRAN II and the original version of pascal are such languages.
This is a severe restriction on the language, making it virtually
unusable for industrial applications.
2.4 Libraries of Modules
• Eg : namespace in C++
• It is possible to Group similar or related modules in a single unit called as
Library.
• a library is a collection of non-volatile resources used by computer
programs, often to develop software.
• These may include configuration data, documentation, help data, message
templates, pre-written code and subroutines, classes, values or type
specifications.
• Library code is organized in such a way that it can be used by multiple
programs that have no connection to each other, while code that is part of
a program is organized to be used only within that one program.
3. Language features for programming in
the large
• So far we discussed concept of programming in large isolated from
programming languages.
• All programming languages provide features for decomposing programs
into smaller and largely autonomous units. We refer to such units as
physical modules.
• logical module denote a module identified at the design stage. A logical
module represents an abstraction identified at the design stage by the
designer.
• A logical module may be implemented by one or more physical modules.
• The closer the relationship between the physical modules and logical
modules is, the better the physical program organization reflects the
logical design structure.
• Here we will discuss relevant aspects of programming languages with
respect to following points:
Module encapsulation: What is the unit of modularity and
encapsulation supported by the language, and how well does it support
different programming paradigms?
Separation of interface from implementation: What is the
relationship among modules that form a program? What entities may be
exported and imported by a module?
Program organization and module groupings: How independently
can physical modules be implemented and compiled? What are the
visibility and access control mechanisms supported by the language?
• The class construct of C++ provides a unit of logical modularity that
supports the implementation of information hiding modules and abstract
data types. Combined with templates, classes may be used to implement
generic abstract data types. The class provides encapsulation and control
over interfaces.
3.1 Encapsulation in C++
• Class is a unit of modularity in C++
• Purposes of class :
– Define new data types
– Define an encapsulated unit
• Class entities can be either public, or private. They can be protected as
well.
• The client must create an instance of the class, called an object, and use
that object to use the services offered by a class.
• Both classes and functions may be generic, supporting a generic
programming style.
• Assignment : Explain encapsulation in PASCAL, ADA, ML and C languages.
3.1.1 Program organization
• Classes define the abstractions from which the program is to be composed.
• stacks are declared in the same way that variables of language-defined types are declared.
• C++ supports the development of independent modules (but does not enforce it):
1. A class’s interface and implementation may be separated and even compiled separately from each other. The implementation must include the interface definition and therefore must be compiled after the interface file exists.
2. Client modules may be compiled with access to only the interface modules of the service providers and not their implementation modules.
3. Any names defined in a class are local to the class unless explicitly declared to be public. Even so, client modules must use the class name to gain access to the names internal to the class.
3.1.2 Grouping of units
• In C++, Fundamental programming unit is class. C++ has several
mechanisms for relating classes to each other. First, classes may be
nested. But as we are dealing with small programs, we will see other two
mechanism, frie d functions and namespaces.
• Friend functions : A class in C++ defines a user-defined type. As a result,
the operations it defines as public are operations on objects of that type.
Some operations do not naturally belong to one object or another.
Eg: arithmetic operations on objects of complex class, multiplication
of vector with matrix (operation is part of neither vector class nor matrix
class).
Module based languages pack such entities in single package.
A class based language like C++ adopt different solutions. C++ allow
access to private data by using Frie d Fu ctio .
Eg :
class complex {
public:
complex(double r, double i ){
re = r;
im = I;
}
friend complex operator+ (complex, complex);
friend complex operator- (complex, complex);
friend complex operator* (complex, complex);
friend complex operator/ (complex, complex);
private:
double re, im;
};
• Namespaces : In C and in C++, the unit of global naming is a file.
– Any names defined at the outer level of a file are known globally by
default.
– What if two libraries provide two classes with the same name? How
can a client use both of those classes? How can a library provider add a
new service to its library and be sure that the new name of the service
does not conflict with any existing uses of the clients?
– The solution of C++ is to partition the global name space into a smaller
groups; each group is called a namespace.
– Advantages : Library provider become independent of other library
providers, own library can be updated without any interference, new
releases can co-exist with old releases.
4. Generic units
• So far we have discussed development of large programs with modularity.
• We can evaluate the suitability of a language for programming in the
large is whether it provides language mechanisms that enable the
construction of independent components, the packaging together of
related components, the use and combination of multiple libraries by
clients, etc.
• In this section, we concentrate on genericity as a mechanism for building
individual modules that are general and thus usable in many contexts by
many clients.
• There are two main generic units in programming language : Generic Data
Structures and Generic algorithms.
• Combination of Generic Data Structures and Generic algorithms defines
Generic Modules.
4.1 Generic Data Structures
template <class T1, class T2>
class pair
{
public:
T1 first;
T2 second;
pair (T1 x, T2 y) : first(x), second(y)
{ }
};
• To use a template data structure, C++ requires explicit instantiation of the
structure. Eg : pair<int,int>
4.2 Generic Algorithms
template <class T>
void swap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
• For functions, on the other hand, explicit instantiation is not necessary.
The compiler will infer the instance required and generate it automatically.
• Examination of the body of swap shows that the parameters passed must
support assignment, that is, to be able to be passed and to be able to be
assigned.
• The implicit parameter requirements in C++ are made explicit in Ada
generic functions. Swap function in swap can be defined as
generic
type T is private;
procedure swap (x, y: T) is
begin
temp: T = x;
x = y;
y = temp;
end swap;
4.3 Generic Module
• Explain Generic Modules with example.
• Def:- Approach/method to solve problem and an approach to
programming language design
There are 4 types of programming paradigm – • Imperative or procedural programming
• Object oriented programming
• Functional programming
• Logic and rule based Programming
5. Programming Paradigm
Imperative or
Procedural
Object
oriented
Functional Logic and rule
based
ALGOL Smalltalk LISP Prolog
COBOL Simula Haskell
ADA C++ APL
C Java
PASCAL
FORTRAN
5.1 Procedural or Imperative Programming
• Imperative, Latin word, means to command. Hence this language is
command driven or statement oriented language.
• Program consist of sequence of statements.
• Values are updated in memory after execution of statement.
• Features of these types of languages are : variables, assignment
statements, and iterations.
Advantages :
• Implementation is simple
• Low memory utilization
Disadvantage :
• Complex problems can not be implemented
• Parallel programming is not possible
• Less productive group of languages
eg:
result = []
i = 0
start:
numPeople = length(people)
if i >= numPeople goto end
p = people[i]
nameLength = length(p.name)
if nameLength <= 5 goto next
upperName = toUpper(p.name)
addToList(result, upperName)
next:
i = i + 1
goto start
end:
return sort(result)
5.2 Object Oriented Programming
• Everything is object
• These languages provide modularity to programming using class. All data and functions are bound within class.
• Various features of these languages are : data abstraction, encapsulation, inheritance, polymorphism, and so on...
Advantages :
• Improved software development productivity (using modules)
• Modular programming approach
• Implementation details are hidden and protected using abstraction
• Modification done on module without affecting rest implementation
• Bug finding is easy
• OOP provide good framework for code library
• Higher quality software
• Code reuse and recycling
Disadvantage :
• Steep learning curve : the thought process may take time to get used in by
people.
• Larger program size : More lines of codes as compared to procedural
language
• Slower program
• Not suitable for all types of problem : some problems lend themselves
well to functional programming language, logic-programming style, or
procedure based programming style
• Members are hidden so non-accessible by other objects
5.3 Functional Programming
• In functional programming control flow is expressed by combining
function calls, rather than by assigning values to variables.
• The computations are expressed in the form of functions.
• This paradigm consider values as single entities.
• Values are un-modifiable.
• Functions are applied to values
• Complex functions are developed using already designed simple functions
(i.e. simple to complex function development)
Advantages:
• Programs are easy to understand
• Functions can be reused
• Large programs can be designed using number of functions
Disadvantages:
• Less efficient
• Memory and time required for execution is more compared to other
languages
• Commercial software can not be designed with this approach
5.4 Logic and rule based Programming
• Assignment : Explain logic and rule based programming
paradigm. State its advantages and disadvantages.