CPlus Pres 2

download CPlus Pres 2

of 20

Transcript of CPlus Pres 2

  • 8/12/2019 CPlus Pres 2

    1/20

    1

    Features of OOP

    Emphasis on data rather thanprocedure.

    Programs are divided into objects. Data structures are designed such

    that they characterize the objects. Functions that operate on the data of

    an object are tied together in the data

    structure.

    Objects may communicate with eachother through functions.

    Data is hidden & can not be accessedby external function.

    New data and functions can be easilyadded whenever necessary. Follows bottom up approachin program design.

  • 8/12/2019 CPlus Pres 2

    2/20

    2

    Concepts used in OOP

    Objects

    Classes

    Data Abstraction

    Data Encapsulation

    Inheritance

    Polymorphism

    Dynamic binding

    Message Passing

  • 8/12/2019 CPlus Pres 2

    3/20

  • 8/12/2019 CPlus Pres 2

    4/20

  • 8/12/2019 CPlus Pres 2

    5/20

    5

    Members of a structure can be directlyaccessed anywhere within their scope.

    C++ attempts to bring user defined data typesas close as possible, to the built-in

    data_types and also provides a facility to

    hide the data which is an important concept

    of OOP.

    Class

    It is a data type in C++ which holds dataand functions.

    It has two parts:Class declaration

    (same as structure declaration)

    Class function definition(describes how the functions are

    implemented)

  • 8/12/2019 CPlus Pres 2

    6/20

    6

    General Form of class

    class class_name

    {

    private:

    variable_declarations;

    function_declarations;

    data members member functions

    public:

    variable_declarations;function_declarations;

    }class item

    { private:

    int num;

    float cost ;public:

    getdata(int a, float b);

    void putdata(void);

    }

  • 8/12/2019 CPlus Pres 2

    7/20

    7

    Class is usually grouped under twosections Private & Public.

    Private members can be accessedonly within the class (data hiding)

    and not visible / accessible outside

    the class.

    oPublic members can be accessedfrom outside the class also.

    Only member functions (private/public)can have access to the private members

    (data & functions).

    Binding of data and functions togetherinto a single class_type variable is called

    Encapsulation

  • 8/12/2019 CPlus Pres 2

    8/20

    8

    General convention for class is to make thevalue part (variable members) of the data typeprivate and make the functions public.

    Actual function definitionscan appearwithin the class or outside the class,

    anywhere in the program.

    Objects can be created by simpleDeclaration such as: itemx, y, z; (itemis a class)

    Defining member functions

    Inside the class Outside the class

    classitem

    {int num; float cost; (default private)//declaration// public:void getdata (int a, float b);

    void putdata (void);{ cout

  • 8/12/2019 CPlus Pres 2

    9/20

    9

    Outside the class

    return_type class_name :: func_name (arg_list)

    { body }

    tells compiler that fu-name belongs to which class

    classitem

    { int num; float cost;

    public:

    void getdata (int a, float b);

    //declarations void putdata (void);}

    void item :: getdata (int a, float b);

    { num = a;

    Can access cost = b;

    private data };

    (num & cost) void item ::putdata (void);

    { cout

  • 8/12/2019 CPlus Pres 2

    10/20

    10

    Making an outside functioninline

    One of the objectives of OOP is to separate thedetails of implementation from the class definition.

    Good practice is to define the memberfunctions outside the class.

    We can define such functions as inline by justusing qualifier inline.

    When a function is defined inside a class, it istreated as an inline function that means onlysmall functions are defined insides the class.

    class item

    { :

    public

    void getdata (int a, float b);

    } ;

    inlinevoid item :: getdata (int a, float b)

    { num = a; cost = b; }

  • 8/12/2019 CPlus Pres 2

    11/20

    11

    Accessing class members

    Note that objects communicate by sendingand receiving messages through the member

    functions.

    itemX;

    X . getdata (10,7.5)X . putdata ( ) ;

    Objects of the class can call only publicmember functions by using dot operator.

    In publicsection, if we define a variableval, then it can be accessed directly outside

    in the main program.

    X .val + 10;

    If valis defined in privatesection, thenabove statement is illegal.

  • 8/12/2019 CPlus Pres 2

    12/20

    12

    Nesting of member functions Member function can be called by using

    its name inside another member function

    of the same class.

    This is called nesting of member

    function.

    Private member function Some situations may require to hide

    member functions as well.

    For examples:

    oDeletion of an account in customer file,oProviding increment etc.

    classsample

    { int m;

    void get (void);

    public:void update (void);}

  • 8/12/2019 CPlus Pres 2

    13/20

    13

    voidsample :: update (void){ get ( ) }private member function

    sample S;

    S . update ( ) legal

    S . get ( ) illegal

    Memory Allocation for Object When a class is declared, member functions

    are created and placed in the memory space

    only once.

    But when object is declared as a specific

    class type, same member functions are usedand no separate allocation done. But space

    for member variables is allocated for each

    object.

  • 8/12/2019 CPlus Pres 2

    14/20

    14

    Static member variables

    Used to maintain values common to entireclass.

    Type and scope of each static membervariable must be declared in the beginning

    of the class definition and defined outside.All objects share same copy of static

    variable.

    They are associated with class rather thanobject, therefore, called class variables.

    They are visible only within the class butits lifetime is the entire program.

    It is initialized to zero when the firstobject of its class is created.

  • 8/12/2019 CPlus Pres 2

    15/20

    15

    class item

    { static int count; int num;

    public :void getdata (int a )

    { num = a;count ++;

    }

    void getcount (void )

    { cout

  • 8/12/2019 CPlus Pres 2

    16/20

    16

    Static member functions

    Static function can have access to staticmembers (functions / variables ) declaredin the same class.

    Static member function can be called usingthe class name (instead of its objects)

    Static function callitem :: static_fun;

    Non static function callitem T;

    T. fun_name;

    Arrays of objects

    item a[10];

    a[i] . getdata;a[i] . getcount;

  • 8/12/2019 CPlus Pres 2

    17/20

    17

    Objects as Arguments in the Function

    There are two ways of passing object as aparameter.

    call by value call by reference

    A copy of the entire Only the address ofobject is passed to the object is transferred

    the function to the function

    Object can be passed to any function(member functions of the same class or to a

    non_member function).

    class item

    { int num;public :voidgetdata (int a )

    { num = a; }

    voidadd (item S1, item &S2 ){ cout

  • 8/12/2019 CPlus Pres 2

    18/20

    18

    Returning objects

    Functions can return objects.

    classcomplex

    { private:

    floatx; floaty;

    public :

    void inputdata (float r , float i)

    { x = r ; y = i ; }void show(complex c)

    { cout

  • 8/12/2019 CPlus Pres 2

    19/20

    19

    Constant Member Function

    If member function does not alter any datain the class, we may declare it as constmember function by appending a function

    declaration with the qualifier const.

    voidmult (int, int) const ;

    Error message is generated if such functiontries to alter the data.

    Pointers to Members

    Address of a member (data / function) canbe obtained by applying &operator to a fullyqualified class member name.

    A class using pointer can be declared usingthe operator ::*with the class name.

  • 8/12/2019 CPlus Pres 2

    20/20

    20

    classitem

    { int m;

    public :

    voidshow ( ) ;

    }

    Pointer to the members of a class itemis defined by item ::*

    Example: Pointer ptrto member mis defined as

    int item :: *ptr = &item :: m;

    int *ptr = & m Invalid

    Here &item :: m means address of m.(m is a member of class item) ptr defined above can be used to access

    the member minside the member functions

    and not outside the class.

    itema; // inside a member function

    cout