encapsulation presentation

Upload: preetvirdi

Post on 02-Jun-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Encapsulation Presentation

    1/38

    Encapsulation

  • 8/11/2019 Encapsulation Presentation

    2/38

    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.

    }

    }

  • 8/11/2019 Encapsulation Presentation

    3/38

    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 byprogrammers without known its internal behavior.

  • 8/11/2019 Encapsulation Presentation

    4/38

    Different concepts for languages

    A language mechanism for restricting access to someof 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.

  • 8/11/2019 Encapsulation Presentation

    5/38

    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();

    }

    }

  • 8/11/2019 Encapsulation Presentation

    6/38

    Various Mechanism

    Four basic mechanism exist to provide programmerwith ability to create new data types and operationon the type:

    1) Structured Data or Data Structures.

    2) Sub Program.

    3) Type Declaration.

    4) Inheritance.

  • 8/11/2019 Encapsulation Presentation

    7/38

    1) Structured Data

    Creating complex data objects out of elementarydata 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

  • 8/11/2019 Encapsulation Presentation

    8/38

    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. Sayaccess mechanism.

    Example : Array

  • 8/11/2019 Encapsulation Presentation

    9/38

  • 8/11/2019 Encapsulation Presentation

    10/38

    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.

  • 8/11/2019 Encapsulation Presentation

    11/38

    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).

  • 8/11/2019 Encapsulation Presentation

    12/38

    Storage Representation

  • 8/11/2019 Encapsulation Presentation

    13/38

    Actual Implementation

    For Sequential

    One method is serial access(For Loop).

    Second method is random access. For this, we need baseaddress and size of component to find the offset.

    Offset = I * component size;

    I location number to access.

    Component size according to data type.

  • 8/11/2019 Encapsulation Presentation

    14/38

    Accessing components of array

  • 8/11/2019 Encapsulation Presentation

    15/38

    Actual Implementation Cont.

    For linked

    We need to know all the pointer values if we have to jump tosome point.

    We must know the current position of the pointer and its

    correspondence pointer value.

  • 8/11/2019 Encapsulation Presentation

    16/38

    Storage management in Data Structures

    The life time of any data object begins when thebinding 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.

  • 8/11/2019 Encapsulation Presentation

    17/38

    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 createdso that the data object can be accessed by operationsin the program.

    After work has done access path is destroyed. It canbe destroyed in various ways: Assign a new value to the data object.

    By return from subprogram into main program.

  • 8/11/2019 Encapsulation Presentation

    18/38

    Storage management problem issue

    Two main problems in storage management arisebecause of data paths are:

    Garbage: Even when access paths have been destroyed thedata object may exist in memory, these data objects are called

    as garbage. As access paths have been destroyed, no programcan access data objects, there is wastage of memory.

    Dangling references: A dangling reference is an access paththat continuous to exist after the lifetime of the associated data

    object. After data object life is finished it restore the memoryfor further re-allocation, but it may not destroy the access pathleading to create dangling references. May help in breach datasecurity.

  • 8/11/2019 Encapsulation Presentation

    19/38

    Declarations and type checking for DS

    Data structures are more complex, thus requiredmore 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.

  • 8/11/2019 Encapsulation Presentation

    20/38

    Type checking

    Existence of selected component: If we try to accessout of bound values that does not exist may lead toerror.

    Type of selected component: A selection sequencemay define a complex path. Problems occurs whiletype checking.

  • 8/11/2019 Encapsulation Presentation

    21/38

    Vector and Array

    A vector is a data structure composed of a fixednumber of components of same type organized as asimple linear structure.

    A vector is one dimensional. An array is two dimensional. It has components

    stored in rectangular grid of rows and columns. Wecan access any element using row and column

    subscript.

  • 8/11/2019 Encapsulation Presentation

    22/38

    Vector attributes

    Number of components.

    Data type of each component.

    Subscript to be used to access each component.

    We already have discussed these.

  • 8/11/2019 Encapsulation Presentation

    23/38

    Operations on Vector

    The operation that select a component from a vectoris 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).

  • 8/11/2019 Encapsulation Presentation

    24/38

    Implementation

  • 8/11/2019 Encapsulation Presentation

    25/38

    Implementation Continues

  • 8/11/2019 Encapsulation Presentation

    26/38

    Multidimensional array

    An array can be of two dimensional or threedimensional.

    Syntax:datatype name [dimentiona 1][dimention 2];

    Example:float hello[3][4];

    Operations are same as vectors.

  • 8/11/2019 Encapsulation Presentation

    27/38

    Actual Implementation

  • 8/11/2019 Encapsulation Presentation

    28/38

    Slices

    A slice is sub-structure of the an array that is array.

    PL/I was the first language to implement slices.

  • 8/11/2019 Encapsulation Presentation

    29/38

    Records(Structure)

    Fixed number of components with different data types.

    Component of record can be heterogeneous.

    Component of record are named with symbolic namesrather than subscript.

    Example: struct EmployeeType

    { int ID;

    int Age;

    float Salary;

    char Dept;

    }Obj;

  • 8/11/2019 Encapsulation Presentation

    30/38

    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;

  • 8/11/2019 Encapsulation Presentation

    31/38

    Implemention

    The storage representation is done using a singleblock.

    The element with maximum storage is taken as base

    to construct that block.

  • 8/11/2019 Encapsulation Presentation

    32/38

    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.

  • 8/11/2019 Encapsulation Presentation

    33/38

    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);

  • 8/11/2019 Encapsulation Presentation

    34/38

    Storage representation for record

  • 8/11/2019 Encapsulation Presentation

    35/38

    Variant Record

    It provide us the facility to apply case on records.

  • 8/11/2019 Encapsulation Presentation

    36/38

    Lists

    A data structure of an ordered sequence of datastructures 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

  • 8/11/2019 Encapsulation Presentation

    37/38

    Sets

    A set is a data object containing unordered collectionof 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 canonly 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).

  • 8/11/2019 Encapsulation Presentation

    38/38

    Will be Uploaded On

    Id: [email protected]

    Password: donotchangeit

    mailto:[email protected]:[email protected]