Anda di halaman 1dari 38

Encapsulation

Problem Defined
class Program
{
public class Account()
{
private float accountbalance=500;
------
------
-------
so on
}
public static void main()
{
//If someone change the value of accountbalance here.
Account myaccount=new Account();
myaccount.accountbalance=500000;
float mybalance=myaccount.accountbalance;
print(mybalance);
// Here we will get the changed value instead of getting orignal value.
}
}
Problem Solution
Solution to this problem is encapsulation.
Also known as information hiding.
It is basic feature of all the languages.
Help in creating section data type for programmers.
Section data types are those which are used by
programmers without known its internal behavior.
Different concepts for languages
A language mechanism for restricting access to some
of object component.
A language construct facility bundling of data with
methods operating on data. In other words,
wrapping up of data and functions into a single unit.
Problem Solution for Example
class Program
{
public class Account()
{
private decimal accountbalance=500;
public decimal CheckBalance()
{
return accountbalance;
}
}
public static void main()
{
Account myaccount=new Account();
float mybalance=myaccount.CheckBalance();
}
}
Various Mechanism
Four basic mechanism exist to provide programmer
with ability to create new data types and operation
on the type:
1) Structured Data or Data Structures.

2) Sub Program.

3) Type Declaration.

4) Inheritance.
1) Structured Data
Creating complex data objects out of elementary
data object defined by language designs.
A data structure is a data object that contain other
data object as its elements or components.
Eg: Array, List and Record
Specification of Data Structure Types
Number of components
May be fixed sized (Array and Record).
May be variable (Stack, tables and Link List).
Types of each components.
Homogeneous (Array and files).
Non-homogeneous (Record).
Names to be used for selecting components
It is selection mechanism required to access components. Say
access mechanism.
Example : Array

Specification of Data Structure Types Cont.
Maximum number of components
For variable size data type we can fix length by specifying the
numbers of components in it.
Organization of components
Liner ( One dimensional).
Vector( Pointer Based).
Operations on Data Structures
Component selection operation
Random Selection
Sequential Selection
Whole data structure operation
Can take whole data structure as input and produce result in
form of similar type data structure. Example Addition of array.
Insertion and deletion of component.
Creation and destruction of data structure.

Storage Representation
It includes:
Storage for component of data structure
An optional descriptor for storing attributes of data structure.
Methods:
Sequentially.
Linked.
Sequential method used for fixed sized (Array).
Linked used for variable size (Linked List).
Storage Representation
Actual Implementation
For Sequential
One method is serial access(For Loop).
Second method is random access. For this, we need base
address and size of component to find the offset.
Offset = I * component size;
I location number to access.
Component size according to data type.

Accessing components of array
Actual Implementation Cont.
For linked
We need to know all the pointer values if we have to jump to
some point.
We must know the current position of the pointer and its
correspondence pointer value.
Storage management in Data Structures
The life time of any data object begins when the
binding of object to particular location is made.
The lifetime end when the binding of object to
storage block is dissolved.
For data structure of variable size, individual
components of the structure have their own lifetime,
that is determined when they are inserted or get
deleted from the data structure.

Storage management in Data Structures Cont.
Access path: At the time when data object is created,
an access path to the data object must also be created
so that the data object can be accessed by operations
in the program.
After work has done access path is destroyed. It can
be destroyed in various ways:
Assign a new value to the data object.
By return from subprogram into main program.
Storage management problem issue
Two main problems in storage management arise
because of data paths are:
Garbage: Even when access paths have been destroyed the
data object may exist in memory, these data objects are called
as garbage. As access paths have been destroyed, no program
can access data objects, there is wastage of memory.
Dangling references: A dangling reference is an access path
that continuous to exist after the lifetime of the associated data
object. After data object life is finished it restore the memory
for further re-allocation, but it may not destroy the access path
leading to create dangling references. May help in breach data
security.
Declarations and type checking for DS
Data structures are more complex, thus required
more information to specify while declaring them.
Data type
Number of dimensions.
Number of components.
Subscript naming.
Data type of each component.
Example: float A[20];
Array, one, 20, 0-19, float.
Type checking
Existence of selected component: If we try to access
out of bound values that does not exist may lead to
error.
Type of selected component: A selection sequence
may define a complex path. Problems occurs while
type checking.
Vector and Array
A vector is a data structure composed of a fixed
number of components of same type organized as a
simple linear structure.
A vector is one dimensional.
An array is two dimensional. It has components
stored in rectangular grid of rows and columns. We
can access any element using row and column
subscript.
Vector attributes
Number of components.
Data type of each component.
Subscript to be used to access each component.
We already have discussed these.
Operations on Vector
The operation that select a component from a vector
is called as subscripting.
It is usually written as vector name followed by
subscript of component to be selected.
Example : V[2];
Other operations may include:
Creation
Destroy
Update
Assign one to other (Whole DS operations).

Implementation
Implementation Continues
Multidimensional array
An array can be of two dimensional or three
dimensional.
Syntax:
datatype name [dimentiona 1][dimention 2];
Example:
float hello[3][4];
Operations are same as vectors.
Actual Implementation
Slices
A slice is sub-structure of the an array that is array.
PL/I was the first language to implement slices.

Records(Structure)
Fixed number of components with different data types.
Component of record can be heterogeneous.
Component of record are named with symbolic names
rather than subscript.
Example:
struct EmployeeType
{ int ID;
int Age;
float Salary;
char Dept;
}Obj;
Operations on Record
Various attributes to be declared in prior declaration:
Number of components (4).
Data type of each component (int , float, char).
Selector used to name each component( ).
struct EmployeeType
{ int ID;
int Age;
float Salary;
char Dept;
}Obj;
In this we access the elements using object i.e. Obj.
Obj.Id;
Obj.Dept;
Implemention
The storage representation is done using a single
block.
The element with maximum storage is taken as base
to construct that block.

Array of records
We can also use array of record as a data structure.
Example:
struct EmployeeType
{
int ID;
int Age;
float Salary;
char Dept;
}Obj[500];
Here, we can store information of 500 employee.
Multi-Level Record
Declaration Style:
1 Employee,
2 Name,
3 Last CHARACTER (10),
3 Middle CHARACTER (15),
3 First CHARACTER (15),
2 Age FIXED(2),
2 Address,
3 Street
4 Number FIXED(5),
4 St-Name CHARACTER(20),
3 City CHARACTER(15),
3 State CHARACTER(15),
3 Zip FIXED(5);
Storage representation for record
Variant Record
It provide us the facility to apply case on records.

Lists
A data structure of an ordered sequence of data
structures is usually termed as List.
Specification:
Lists are rarely fixed length.
Lists are rarely homogeneous.
Types:
Stacks(Selection, Insertion and Deletion fixed to one end LIF))
Queues(Insertion and deletion on different ends).
Trees
Directed Graph

Sets
A set is a data object containing unordered collection
of distinct values.
Basic operations on set.
Membership: Is data X is member of set S.(X belongs to S).
Insertion and deletion of single values: We can only insert the
data value X in set S if X is not already in set. Similarly, we can
only delete X if its member of S.
Union(Create new set of S1 and S2 with duplicate deleted).
Intersection( Have members both belongs to S1 And S2).
Difference of Sets(Set containing values in S1 but not in S2).
Will be Uploaded On
Id: csedaviet2014@gmail.com
Password: donotchangeit

Anda mungkin juga menyukai