oops note.doc

Upload: suji002sujitham

Post on 02-Jun-2018

257 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 oops note.doc

    1/38

    OOPS 1

    INTODUCTION TO OOPS

    C++ programming is object oriented .

    In C++, the unit of programming is the class from which the objects

    are eventuall created.

    C++ programmers concentrate on creating their own user defined data

    tpes called classes.

    C++ is an e!tensive version of C language.

    C++ is derived from C.

    C++ name is named b "ic# $arcitti

    C++%s OOP aspect was inspired in earl 1&'(%s b )jarne Stroustrup.

    1. What is class?Declaration of class?

    * class is a collection of Objects. * Class is a collection of properties ,behavior with e!istence

    * class is a user defined data tpe which hold both data and

    function.

    he data included in the class i.e, the internal data is called the

    member data and function included in the class are called

    member function.

    e! fruit mango-

    fruit is the class and mango is the object.

    eneral form of class declaration isclassclass/name

    {

    private:

    variable declaration-

    function declaration-

    pulic:

    variable declaration-

    function declaration-

    0-

    Class #eword.

    Class!na"euserdefined data tpe.

    Class is similar to structure in c

    $$C2

  • 8/10/2019 oops note.doc

    2/38

    OOPS 3

    #. What is $estructor? %o& it is invo'e$?

    * destructor is a special member function of the class.

    It deallocate memor space.ie, It is used to destro the objects

    that have been created b a constructor he destructor name is the same as the class name but is

    preceded b a tilde456.

    2! 5pen46 77destructor

    8

    0

    * destructor is defined under public section of the class.

    * destructor never ta#es an argument nor does it return an

    value .

    It will be invo#ed implicitl b the compiler upon e!it from

    the program to clean up storage that is no longer accessible.

    (. %o& the o$) of "e"er fuction of a class is &ritten?

    * function which is defined or declared inside the class is

    called as member function.

    $ember function can be defined in two places.

    Outside the class definition.

    Inside class definition.

    Outsi$e the class $efinition

    $ember function that are declared inside a class have to be

    defined separatel outside the class.

    he general form of a member function $efinition outsi$e the

    class is:

    *eturn!t)pe class!na"e::function!na"e+ar,u"ent $eclaration-

    {

    unction o$)/

    0

    classpen

    8

    private

    int a,b-

    public

    $$C2

  • 8/10/2019 oops note.doc

    3/38

    OOPS 9

    void read46 - 77member function declared

    void dis46 - 77 member function declared

    0-

    void penread46

    8

    cin::a::b-

    0

    void pendis46 defined outside the class

    8

    cin::a::b-

    0

    void main46

    8

    pen p- p.read46-

    p.dis46-

    0

    Insi$e the class $efinition

    ;unction definition inside the class

    he general form of a member function $efinition insi$e the

    class is:

    *eturn!t)pe function!na"e+ar,u"ent $eclaration-

    { unction o$)/

    0

    classpen

    8

    private

    int a,b-

    public

    void read46 77member fuction

    8 cin::a::b-

    0

    void dis46 77member function inside the class

    8

    Cout

  • 8/10/2019 oops note.doc

    4/38

    OOPS =

    void main46

    8

    pen p-

    p.read46-

    p.dis46-

    0

    . Define class an$ o2ect. ?

    class+refer 3no:1-

    o2ects

    Objects are primar run>time entities.

    Objects have some properties ,behavior with e!istence.

    e! ?ame of Car,model,colour etc.

    when a program is e!ecuted ever object will interacts with each other

    b sending messages.

    *n object is a specimen of a class.

    4. What are the enefits of o2ect oriente$ pro,ra""in,@

    hrough inheritance, we can eliminate redundant code and e!tend the

    use of e!isting classes. It is eas to partition the wor# in a project based on objects.

    Object>oriented sstem can be easil upgraded from small to large

    sstems.

    Software comple!it can be easil managed.

    esser maintenance cost.

    5. 67plain the features of o2ect oriente$ pro,ra""in,?

    2mphasis is on data rather than procedure.

    Programs are divided into what are #nown as objects. Aata is hidden and cannot be accessed b e!ternal functions.

    Objects ma communicate with each other through functions.

    ;ollows bottom>up approach in program design.

    $$C2

  • 8/10/2019 oops note.doc

    5/38

    OOPS B

    8. 67plain $efault constructors or constructor?

    * constructor that accepts no parameters is called the default

    constructor.

    It is used to initialies the object of a class.

    * default constructor initialie the data members with no arguments.

    general form of the default constructor is.

    class user/define/name

    8

    private

    >>>>>>>>>>>>

    >>>>>>>>>>>>>

    public

    //////

    //////user/defined name4677 default constructor

    8

    077with no arg

    /////////////

    /////////////

    0/

    * statement such as

    * a-

    invo#es the default constructor of the compiler to create the object Da%.eg

    classpen

    8

    private

    int a,b-

    public

    pen46 77constructor

    8

    aE1(-bE3(-

    0

    void dis46 77member function

    8

    Cout

  • 8/10/2019 oops note.doc

    6/38

    OOPS F

    void main46

    8

    pen p-

    p.dis46-

    getch46-

    0

    9. What are $estructor in C? &hat are its use.

    Destuctor+refer 3no:#-

    It release memor space for future use.

    new is used to allocate memor in the constuctors. Aelete is used to free that memor.

    ;. What are Constructors? e7plain its characteristics?

    * member function with same name as its class is called as

    constructor and it is used to initialie the object of that class

    tpe with a legal initial value.

    Constructor is the first member function.

    Characteristicts

    It has the same name as that of the class it belongs. Constuctor is e!ecuted when an object is declared.

    It is automaticall called when an object is created of that

    class.

    Constructor have neither return value nor void .

    he main function of constructor is to initialie objects and

    allocate appropriate memor to objects.

    Constuctor are e!ecute implicitl the can be invi#ed

    e!plicitl.

    1

  • 8/10/2019 oops note.doc

    7/38

    OOPS G

    {

    unction o$)/

    0

    67:

    Classpen

    8

    private :

    int a,b-

    pulic:

    void read46-

    void displa46-

    0-

    void penread46

    8

    cin::a::b- 0

    void pendispla46

    8

    cout

  • 8/10/2019 oops note.doc

    8/38

    OOPS '

    *ll variables must be declared before the are used in the

    program.

    1#. Define an$ e7plain a cop) constructor@

    * cop constructor is a special member function of the

    form class/name4class name Hptr6 Ptr>pointer to the object.

    his tpe of function is used when the compiler has

    create a temporar object of a class object.

    he compiler will use the cop constructors whenever

    ou initialie an instance using values of another

    instance of same tpe.

    2! pen p- 77default constructor pen 4p6- 77cop constructor invo#ed

    Pro,ra"

    Class pen

    8

    private

    int a,b-

    public

    pen4int !, int 6 77constructor with argument

    8

    aE!-

    bE-

    0

    Pen4pen H ptr6

    8

    aEptr.a-

    bEptr.b-

    0Joid dis46

    8

    Cout

  • 8/10/2019 oops note.doc

    9/38

  • 8/10/2019 oops note.doc

    10/38

    OOPS 1(

    0-

    main46

    8

    Pen p4B(6-

    p.dis46-

    0

    #. 67plain the evolution of oops?

    he object oriented programming language language was

    developed at * H )ell laboratories in earl 1&'(%s b )jarnce

    Strostrup.

    Object oriented programming is an approach to program

    organiation and development that attempts to eliminate some

    of the pit falls of conventional programming b incoparating

    the best of structured programming features several powerful new concepts.techniues such as

    modular,topdown,bottom up and structured programming.

    O2ect@oriente$ pro,ra""in,

    Proce$ure oriente$

    Bsse"l) lan,ua,e

    >achine lan,ua,e

    1=< +Ainar) lan,ua,e-

    $$C2

  • 8/10/2019 oops note.doc

    11/38

    OOPS 11

    (. Arin, aout the enefits an$ appilication of o2ect oriente$

    pro,ra""in,.

    Aenefits+refer 3no:4-

    Bpplication of o2ect@oriente$ pro,ra""in,

    "eal time sstem

    Simulation and modeling

    Object>oriented databases

    Kperte!t,hpermedia and e!perte!t

    *I and e!pert sstems

    ?eural networ#s and parallel programming

    CI$7C*A7C*$ sstem

    . Distin,uish et&een proce$ure oriente$ lan,ua,es an$ o2ect

    oriente$ lan,ua,es?

    *efer part B+ 3no:5-

    Proce$ure oriente$ pro,ra""in,

    Conventional programming,using highlevel languages

    2mphasis is on doing things

    arge programs are divided into small programs #nown as

    functions

    $ost of the functions share global data

    Aata $ove openl around the sstem from function to

    function.

    ;unction transform data from one form to another

    $$C2

  • 8/10/2019 oops note.doc

    12/38

    OOPS 13

    2mplos top>down approach in program design.

    o2ect@oriente$ proce$ural

    methods functions

    objects modules

    message argument

    attribute variable

    4.67plain the asic concepts of oops?

    oop has few major components such as objects and classes.

    )ased on the objects and classes the features ofinheritance,encapsulation,data abstraction,reusabilit and

    polmorphism are implemented.

    OA6CTS

    object is a primar run time entit.

    object is a logical entit that contains both data and code that

    manipulate the data.

    e!name of person,place etc.

    CBSS

    In oops the objects are members of classes. * class is a collection of objects which have similar

    characteristics.

    e! class car.

    properties compan,model,color etc.

    6NCBPSUBTION

    $$C2

  • 8/10/2019 oops note.doc

    13/38

    OOPS 19

    Lrappind up of data and function into a single unit.

    Aata encapsulation is a stri#ing feature of a class.

    DBTB 6NCBPSUBTION

    *bstraction refers to the act of representing essential

    features without including the bac#ground details ore!planations

    he classes uses the concept of data abstraction.

    IN%6*ITBNC6

    Inheritance is the process b which objects of one class inherits

    the properties of objects of another class.

    he concept of inheritance provides the idea of reusabilit.

    POE>O*P%IS>

    Polmorphism is a gree# term.

    It means the abilt to ta#e more than one form.

    >ODU6@II

    1.Define an$ e7plain Inheritance in oops?

    Inheritance is the powerfull feature of oops.

    Inheritance is the process b which objects of one class acuire

    the properties of objects of another class.

    he concepts of inheritance provides the idea of reusailit).

    Inheritance is a mechanism which deriving a new class from

    alread e!isting class.

    )ase class is parent class and derived class is child class.

    $$C2

  • 8/10/2019 oops note.doc

    14/38

    OOPS 1=

    parent class

    child class

    base class

    derived class

    Inheritance of stu$ent class

    Inheritance are mainl five tpes.

    1.single inheritance

    3.multiple inheritance

    9.multilevel inheritance

    =.hierarchical inheritance

    B.hbrid inheritance

    he derived class inherits some or all of the traits from the base

    class.

    * derived class with onl one base class is called sin,le

    inheritance.

    * derived class with several base classes is called "ultiple

    inheritance.

    $$C2

    Student

    ?ame

    *ddress

    M Sem

    ;eesStipend

    P Sem

    ;eesStipend

  • 8/10/2019 oops note.doc

    15/38

    OOPS 1B

    Aeriving a class from another derived class is called "ultilevel

    inheritance or repeate$ inheritance.

    One class ma be inherited b more than one class is called

    hierarchical inheritance.

    he result will have both the multilevel and multiple

    inheritance is h)ri$ inheritance .

    #. Ariefl) e7plain an) four for"s of inheritance?

    Inheritance are mainl five tpes.

    1.single inheritance

    3.multiple inheritance

    9.multilevel inheritance

    =.hierarchical inheritance

    B.hbrid inheritance

    he derived class inherits some or all of the traits from the base

    class.

    * derived class with onl one base class is called sin,le

    inheritance.

    Aase class +parent class-

    Derive$ class + chil$ class-

    * derived class with several base classes is called "ultiple

    inheritance.

    $$C2

    B

    )

  • 8/10/2019 oops note.doc

    16/38

    OOPS 1F

    Aeriving a class from another derived class is called "ultilevel

    inheritance or repeate$ inheritance.

    Aase class +parent class-

    Inter"e$iate class

    Derive$ class + chil$ class-

    $$C2

    * ) C

    A

    B

    )

    C

  • 8/10/2019 oops note.doc

    17/38

    OOPS 1G

    One class ma be inherited b more than one class is called

    hierarchical inheritance.* class derived in a heirachical order.

    he result will have both the multilevel and multiple

    inheritance is h)ri$ inheritance.

    $$C2

    ABS6

    A2"IJ2A ?A2"IJ2A 3A2"IJ2A1

    ABS6

    ABS6

    D6*I6D1

    D6*I6D

  • 8/10/2019 oops note.doc

    18/38

    OOPS 1'

    (. 67plain frien$ function ?

    ;riend function can access the private and protected datas of

    a class. * friend function to a class ma be declared or defined

    within the scope of a class definition.

    he general snta! of a friend declaration is

    St!frien$ return!t)pe user!$efine!na"e+ar,u"ents list-/

    ;riend function declaration inside the class.

    2g

    Class *)C8

    private

    //////////////

    //////////////

    public

    //////////////

    //////////////

    friend void !4void6- 77declaration

    0-

    2!

    class pen

    8

    private

    $$C2

  • 8/10/2019 oops note.doc

    19/38

    OOPS 1&

    int a,b-

    public

    pen46

    8

    aE(-bEB-

    0

    pen4int w,int 6

    8

    aEw-bE-

    0

    void read 46

    8cin::a::b-

    0

    void dis46

    8

    cout

  • 8/10/2019 oops note.doc

    20/38

    OOPS 3(

    In friend class,an entire class is made a friend to another

    class

    Creating the friend class all the member function of the

    friend class can access the private member data of another

    class.

    he general snta! of a friend class is

    Class*

    8

    private

    ////

    ////

    public

    ////

    ;riend class )

    77friend class declaration class )

    77actual definition of friend class second

    8

    ///

    ///

    0In the above snta! ) is a friend class of class * from the

    class ),all the member of the class * can be accessed.

    2!

    Sum of two no

    class *

    8

    privateint a,b,s-

    public

    void read46

    8

    cin::a::b-

    $$C2

  • 8/10/2019 oops note.doc

    21/38

    OOPS 31

    0

    friend class )-

    0-

    class )

    8public

    void sum4* obj6

    8

    obj.sEabj.a+obj.b-

    0

    0-

    void main46

    8* a-

    ) b-

    a.read46-

    b.sum4a6-

    0

    4.67plain Sin,le inheritance?

    * derived class with onl one base class is called sin,le

    inheritance.

    eneral snta! for single inheritance definition is

    Class base

    8

    ////////////

    ////////////

    $$C2

  • 8/10/2019 oops note.doc

    22/38

    OOPS 33

    0-

    Class derived public base

    8

    //////////////

    //////////////

    0-

    he e!isting class or base class is called as direct

    base class

    ?ewl formed class is called derived class.

    he newl derived class had their own properties as well as

    the can inherit all the properties of the base class.

    +pen- Aase class +parent class-

    + Pencil-

    Derive$ class + chil$ class- Sin,le inheritance

    67: class pen

    8

    private

    int a,b-

    $$C2

    B

    )

  • 8/10/2019 oops note.doc

    23/38

    OOPS 39

    public

    pen46

    8

    aE(-

    bE1-

    0

    pen4int m,int n6

    8

    mEa-

    nEb-

    0

    void read46

    8

    cin::a::b-

    0void displa46

    8

    cout

  • 8/10/2019 oops note.doc

    24/38

    OOPS 3=

    0

    void displa146

    8

    cout

  • 8/10/2019 oops note.doc

    25/38

    OOPS 3B

    0-

    Class derivedpublic base1,public base3

    8

    ///////////

    ///////////

    0-

    $ultiple inheritance means that a derived class inherit the

    members of several base classes.

    4pencil6

    4Pen6 4paper6

    4"esult6

    2!class pen

    8

    private

    int a,b-

    public

    pen46

    8

    aE(-bE1-

    0

    pen4int m,int n6

    8

    mEa-

    nEb-

    $$C2

    * ) C

    A

  • 8/10/2019 oops note.doc

    26/38

    OOPS 3F

    0

    void read46

    8

    cin::a::b-

    0

    void displa46

    8

    cout

  • 8/10/2019 oops note.doc

    27/38

    OOPS 3G

    public

    paper46

    8

    eE(-

    fE1-

    0

    paper4int ,intr6

    8

    Ee-

    rEf-

    0

    void read346

    8

    cin::e::f-

    0void displa346

    8

    cout

  • 8/10/2019 oops note.doc

    28/38

    OOPS 3'

    tEh-

    0

    void read346

    8

    cin::a::b::c::d::e::f-

    0

    void displa346

    8

    cout

  • 8/10/2019 oops note.doc

    29/38

    OOPS 3&

    0-

    Class derived1public base1

    8

    /////////////

    /////////////

    0-

    Class derived3public derived 1

    8

    //////////

    //////////

    0-

    he class derived1 is called intermediate class.

    +Pen- Aase class +parent class-

    +Pencil-

    Inter"e$iate class

    Derive$ class + chil$ class-

    +Paper-

    class pen

    8

    private

    $$C2

    B

    )

    C

  • 8/10/2019 oops note.doc

    30/38

    OOPS 9(

    int a,b-

    public

    pen46

    8

    aE(-

    bE1-

    0

    pen4int m,int n6

    8

    mEa-

    nEb-

    0

    void read46

    8

    cin::a::b-0

    void dis46

    8

    cout

  • 8/10/2019 oops note.doc

    31/38

    OOPS 91

    0

    void displa146

    8

    cout

  • 8/10/2019 oops note.doc

    32/38

    OOPS 93

    One class ma be inherited b more than one class is called

    hierarchical inheritance.* class derived in a heirachical order.

    +Pen-

    4pencil6

    4boo#6

    2! 4Paper6

    class pen

    8

    private

    int a,b-

    public

    pen46

    8

    aE(-bE1-

    0

    pen4int m,int n6

    8

    mEa-

    nEb-

    0

    void read46

    8cin::a::b-

    0

    void displa46

    8

    cout

  • 8/10/2019 oops note.doc

    33/38

    OOPS 99

    0-

    class pencilpublic pen

    8

    private

    int c,d-

    public

    pencil46

    8

    Pen46-

    cE(-

    dE1-

    0

    pencil4int m,int n,int o,int p6

    8

    pen4m,n6-oEc-

    pEd-

    0

    void read146

    8

    cin::a::b::c::d-

    0

    void displa146

    8

    cout

  • 8/10/2019 oops note.doc

    34/38

    OOPS 9=

    pencil4int m,int n,int ,int r6

    8

    pen4m,n6-

    Ee-

    rEf-

    0

    void read346

    8

    cin::a::b::e::f-

    0

    void displa346

    8

    cout

  • 8/10/2019 oops note.doc

    35/38

    OOPS 9B

    0-

    Joid main46

    8

    Pencil p-

    Paper -

    )oo# b-

    p.displa146-

    .displa346-

    b.displa946-

    getch46-

    0

    5. 67plain h)ri$ inheritance?

    he result will have both the multilevel and multiple

    inheritance is h)ri$ inheritance.

    pen paper

    pencil

    $$C2

    ABS6

    ABS6

    D6*I6D1

    D6*I6D

  • 8/10/2019 oops note.doc

    36/38

    OOPS 9F

    result

    class pen

    8

    private

    int a,b-

    public

    pen46

    8

    aE(-

    bE1-

    0pen4int m,int n6

    8

    mEa-

    nEb-

    0

    void read46

    8

    cin::a::b-

    0void displa46

    8

    cout

  • 8/10/2019 oops note.doc

    37/38

    OOPS 9G

    0

    pencil4int m,int n,int o,int p6

    8

    pen4m,n6-

    oEc-

    pEd-

    0

    void read146

    8

    cin::a::b::c::d-

    0

    void displa146

    8

    cout

  • 8/10/2019 oops note.doc

    38/38

    OOPS 9'

    class resultpublic pencil,public paper

    8

    private

    int g,h-

    public

    result46

    8

    gE(-

    hE1-

    0

    result4int m,int n ,int o,int p,int ,int r,int s,int t6

    8

    Pencil4m,n,o,p6-

    Paper4,r6-

    sEe-tEf-

    0

    void read946

    8

    cin::a::b::c::d::e::f::g::h-

    0

    void displa946

    8

    cout