Polymorphism OOCP

download Polymorphism OOCP

of 70

Transcript of Polymorphism OOCP

  • 8/10/2019 Polymorphism OOCP

    1/70

  • 8/10/2019 Polymorphism OOCP

    2/70

  • 8/10/2019 Polymorphism OOCP

    3/70

  • 8/10/2019 Polymorphism OOCP

    4/70

  • 8/10/2019 Polymorphism OOCP

    5/70

  • 8/10/2019 Polymorphism OOCP

    6/70

    Simple pointer e&ample

    Sanjay Patel

    6

  • 8/10/2019 Polymorphism OOCP

    7/70

    )o% t"e #aria le a!!essed ypointer

    Sanjay Patel

    7

  • 8/10/2019 Polymorphism OOCP

    8/70

    Output

    Sanjay Patel

    8

  • 8/10/2019 Polymorphism OOCP

    9/70

    A!!essin$ Array t"rou$" pointer

    Sanjay Patel

    9

  • 8/10/2019 Polymorphism OOCP

    10/70

    T"e *&+i means ase address of t"e array &plus t"e offset.

    or e&ample if I %ant to 'no% t"e #alue at& / , it %ill e *&+ 01 /.

    T"e 2*&+i indi!ates t"e #alue stored at t"e& i/t" address.

    3e!"anism

    Sanjay Patel

    10

  • 8/10/2019 Polymorphism OOCP

    11/70

    Output

    Sanjay Patel

    11

  • 8/10/2019 Polymorphism OOCP

    12/70

    Cont(d..

    Sanjay Patel

    12

  • 8/10/2019 Polymorphism OOCP

    13/70

  • 8/10/2019 Polymorphism OOCP

    14/70

    Output

    Sanjay Patel

    14

  • 8/10/2019 Polymorphism OOCP

    15/70

    4e "a#e already seen t"at "o% to a!!essmem er of !lass %it" pointer.Pointer !an point to an o je!t !reated y!lass.

    ABC s564"ere ABC is !lass and s5 in t"e o je!t.

    Similarly %e !an define a pointer a 7ptr oftype ABC.

    ABC 2a 7ptr6

    Pointer to o je!t

    Sanjay Patel

    15

  • 8/10/2019 Polymorphism OOCP

    16/70

    !lass ABC8pu li!: #oid $etdata*int a, int 8

    &0a,y0 6 9 #oid s"o%* 8!out &6!out y6 996

    int main*8 ABC s56 s5.$etdata*5;,

  • 8/10/2019 Polymorphism OOCP

    17/70

    s5.$etdata*5;,

  • 8/10/2019 Polymorphism OOCP

    18/70

    =&ample

    Sanjay Patel

    18

  • 8/10/2019 Polymorphism OOCP

    19/70

    In a o#e pro$ram , t"e o je!t of t"e !lass testis o j.One pointer #aria le is !reated named ptr7o j.

    T"is is a!tually a pointer to o je!t. T"is pointer !an a!!ess t"e pu li! fun!tion oft"e !lass test. )en!e %e are !allin$ t"e

    fun!tion $et#al of t"e !lass test usin$ t"epointer to t"e o je!t. T"us mem er fun!tion!an e a!!essed usin$ pointer

    Cont(d..

    Sanjay Patel

    19

  • 8/10/2019 Polymorphism OOCP

    20/70

    A #irtual fun!tion is a mem er fun!tion t"at isde!lared %it"in a ase !lass and redefined yderi#ed !lass.

    To de!lare a fun!tion #irtual, %e use t"e@ey%ord #irtual.

    Virtual fun!tion

    Sanjay Patel

    20

  • 8/10/2019 Polymorphism OOCP

    21/70

    T"e #irtual fun!tion !an e redefined in t"ederi#ed !lass. T"us usin$ #irtual fun!tion %e!an "a#e one interfa!e, multiple fun!tion

    performin$ different tas'. T"is feature is !alledpolymorp"ism.4"en #irtual fun!tion is redefined y deri#ed!lass , t"e 'ey%ord #irtual is not needed.

    T"e fun!tion %ritten in ase !lass a!ts asinterfa!e and t"e fun!tion defined in deri#ed!alss a!t as different forms of t"e fun!tion.

    Cont(d..

    Sanjay Patel

    21

  • 8/10/2019 Polymorphism OOCP

    22/70

    Virtual fun!tion in ase !lass:Automati!ally #irtual in deri#ed !lass

    eri#ed !lass de!laration *in interfa!eot re>uired to "a#e #irtual 'ey%ord

    But typi!ally in!luded any%ay,for reada ility

    T"e D#irtualE term tells t"e !ompiler to %ait todefine it until it is !alled.

    Ad#anta$es of #irtual fun!tion

    Sanjay Patel

    22

  • 8/10/2019 Polymorphism OOCP

    23/70

    Clear ad#anta$es to #irtual fun!tions as %e(#eseenOne major disad#anta$e: o#er"eadF

    Gses more stora$eHate indin$ is on t"e fly , so pro$rams run slo%er*3u!" slo%er

    So if #irtual fun!tions not needed, s"ould not eused. If you 'no% all of t"e fun!tionality, $oa"ead and pro$ram it.

    isad#anta$es

    Sanjay Patel

    23

  • 8/10/2019 Polymorphism OOCP

    24/70

    !lass S"ape8

    pu li!:#irtual #oid say)i *8

    !out J ust "iF KnLL69

    96

    Virtual fun!tion

    Sanjay Patel

    24

  • 8/10/2019 Polymorphism OOCP

    25/70

    Synta& of #irtual fun!tion

    Sanjay Patel

    25

  • 8/10/2019 Polymorphism OOCP

    26/70

    If t"e mem er fun!tion definition is outside t"e!lass, t"e 'ey%ord #irtual must not espe!ified a$ain.

    Memem er

    Sanjay Patel

    26

  • 8/10/2019 Polymorphism OOCP

    27/70

    A #irtual fun!tion !an e used same as non-#irtual mem er fun!tions.A #irtual fun!tion !an e in"erited from a ase!lass y a deri#ed !lass, li'e ot"er !lassmem er fun!tions.

    Cont(d..

    Sanjay Patel

    27

  • 8/10/2019 Polymorphism OOCP

    28/70

    Simple =&ample

    Sanjay Patel

    28

  • 8/10/2019 Polymorphism OOCP

    29/70

    To let deri#ed !lasses "a#e t"eir o%nimplementation for t"e #irtual fun!tion. %eo#erride ase !lass #irtual fun!tions in deri#ed

    !lass.In order for a deri#ed !lass #irtual fun!tioninstan!e to o#erride t"e ase !lass #irtualfun!tion instan!e, its si$nature must mat!" t"e

    ase !lass #irtual fun!tion e&a!tly. T"e o#erridin$ fun!tions are #irtualautomati!ally. T"e use of 'ey%ord #irtual isoptional in deri#ed !lasses.

    Cont(d..

    Sanjay Patel

    29

  • 8/10/2019 Polymorphism OOCP

    30/70

    =&ample

    Sanjay Patel

    30

  • 8/10/2019 Polymorphism OOCP

    31/70

    In C, only !ompile-time indin$ is pro#ided.In C++, all non-#irtual fun!tions are ound at!ompile-time.

    Compile*Stati! time indin$ VS Mun* ynami! timeindin$

    Sanjay Patel

    31

  • 8/10/2019 Polymorphism OOCP

    32/70

    Sanjay Patel32

  • 8/10/2019 Polymorphism OOCP

    33/70

    Sanjay Patel33

  • 8/10/2019 Polymorphism OOCP

    34/70

  • 8/10/2019 Polymorphism OOCP

    35/70

    T"e #irtual fun!tions must e mem ers ofsome !lass.A #irtual fun!tion is a mem er fun!tion t"at isde!lared %it"in a ase !lass and redefined yderi#ed !lass.

    To de!lare a fun!tion #irtual, %e use t"e

    @ey%ord #irtual. T"ey !annot e stati! mem er. T"ey area!!essed y usin$ o je!t pointers.A #irtual fun!tion !an e a friend of anot"er!lass.

    Mules for #irtual fun!tion

    Sanjay Patel

    35

  • 8/10/2019 Polymorphism OOCP

    36/70

    T"e prototypes of t"e ase !lass #ersion of a#irtual fun!tion and all t"e deri#ed !lass#ersions must e identi!al. If t%o fun!tions

    %it" t"e same name "a#e different prototypes,C++ !onsiders t"em as o#erloaded fun!tions,and t"e #irtual fun!tion me!"anism is i$nored.4e !annot "a#e #irtual !onstru!tors, ut %e!an "a#e #irtual destru!tors.

    Cont(d..

    Sanjay Patel

    36

  • 8/10/2019 Polymorphism OOCP

    37/70

    A !onstru!tor !annot e #irtual sin!e it is usedto !onstru!t an o je!t. A destru!tor !an e #irtual. Virtual destru!torsare #ery useful %"en some deri#ed !lasses"a#e !leanup !ode.

    Constru!tor and estru!tor

    Sanjay Patel

    37

  • 8/10/2019 Polymorphism OOCP

    38/70

    T"e address of a #aria le !an e o tained ypre!edin$ t"e name of a #aria le %it" anampersand si$n *N , 'no%n as reference

    operator , and %"i!" !an e literally translatedas address of . or e&ample:

    Hets is!uss A$ain* Referenceoperator (&) )

    Sanjay Patel

    38

  • 8/10/2019 Polymorphism OOCP

    39/70

    Cont(d..

    Sanjay Patel

    39

  • 8/10/2019 Polymorphism OOCP

    40/70

    As just seen, a #aria le %"i!" stores t"eaddress of anot"er #aria le is !alled a pointe r .Pointers are said to point to t"e #aria le%"ose address t"ey store.An interestin$ property of pointers is t"at t"ey!an e used to a!!ess t"e #aria le t"ey point

    to dire!tly. T"is is done y pre!edin$ t"e pointer name%it" t"e de re fe rence ope rator *2 . T"e operatoritself !an e read as #alue pointed to y .

    ereferen!e operator *2

    Sanjay Patel

    40

  • 8/10/2019 Polymorphism OOCP

    41/70

    Cont(d..

    Sanjay Patel

    41

  • 8/10/2019 Polymorphism OOCP

    42/70

    T"e referen!e and dereferen!e operators aret"us !omplementary:N:--- is t"e re fe re nce ope rator , and !an eread as address of 2:--- is t"e de re fe rence ope rator , and !an eread as #alue pointed to y

    Cont(d

    Sanjay Patel

    42

  • 8/10/2019 Polymorphism OOCP

    43/70

    Sanjay Patel43

    T"e first e&pression is >uite !lear, !onsiderin$ t"att"e assi$nment operation performed on my#ar %asmy#ar0< .

    T"e se!ond one uses t"e referen!e operator *N ,%"i!" returns t"e address of my#ar, %"i!" %eassumed it to "a#e a #alue of 5 Q. T"e t"ird one is some%"at o #ious, sin!e t"e

    se!ond e&pression %as true and t"e assi$nmentoperation performed on foo %as foo0Nmy#ar. T"e fourt" e&pression uses t"e dereferen!e

    operator *2 t"at !an e read as #alue pointed to y ,

    and t"e #alue pointed to y foo is indeed < .

  • 8/10/2019 Polymorphism OOCP

    44/70

    Pointer =&ample

    Sanjay Patel

    44

  • 8/10/2019 Polymorphism OOCP

    45/70

    A pointer to a C++ !lass is done e&a!tly t"esame %ay as a pointer to a stru!ture and toa!!ess mem ers of a pointer to a !lass you

    use t"e mem er a!!ess operator -> operator, just as you do %it" pointers to stru!tures.Also as %it" all pointers, you must initiali e t"epointer efore usin$ it.

    Pointer and o je!t

    Sanjay Patel

    45

    using namespace std; int main &

  • 8/10/2019 Polymorphism OOCP

    46/70

    Sanjay Patel46

    g p ;

    class Box

    {

    double length; // Length of a box

    double breadth; // Breadth of a box double height; // Height of a box

    public:Box double l!"#$% double b!"#$% double h!"#$&

    {

    cout ''()onstructor called#(length ! l;

    breadth ! b;

    height ! h;

    *

    double +olume &

    {

    return length , breadth , height;

    * *;

    { Box Box- .#.% -#"% -# &; // 0eclare box- Box Box" 1# % 2#$% "#$&; // 0eclare box" Box ,ptrBox; // 0eclare pointer to

    a class#

    // Sa3e the address of first object ptrBox ! 4Box-; cout '' (+olume of Box-: ( '' ptrBox56+olume & ;

    // Sa3e the address of first object ptrBox ! 4Box";

    cout '' (+olume of Box": ( '' ptrBox56+olume &;

    return $;*

    // 3irtual members class riangle: public Polygon {

  • 8/10/2019 Polymorphism OOCP

    47/70

    Sanjay Patel47

    // 3irtual membersclass Polygon { protected:

    int 7idth% height; public: 3oid set83alues int a% int b& {

    7idth!a; height!b; * 3irtual int area & {

    return $; **;class 9ectangle: publicPolygon{

    public: int area & { return 7idth , height; * *;

    class riangle: public Polygon { public: int area & { return 7idth , height / "&; *

    *;int main & { 9ectangle rect; riangle trgl; Polygon poly; Polygon , ppoly- ! 4rect; Polygon , ppoly" ! 4trgl; Polygon , ppoly. ! 4poly; ppoly-56set83alues % &; ppoly"56set83alues % &; ppoly.56set83alues % &; cout '' ppoly-56area & ''

  • 8/10/2019 Polymorphism OOCP

    48/70

    A pure #irtual fun!tion is a #irtual fun!tion inase!lass t"at "as no denition.

    =.$. Consider t"e #irtual fun!tion s"o%Area* inase !lass S"ape6 it "as only an a stra!t

    meanin$. T"us, s"o%Area* !an e de!lared as pure

    #irtual fun!tion. A pure #irtual fun!tion is de!lared usin$spe!ier K0 ; .

    Pure #irtual fun!tion

    Sanjay Patel

    48

  • 8/10/2019 Polymorphism OOCP

    49/70

    Synta&

    Sanjay Patel

    49

  • 8/10/2019 Polymorphism OOCP

    50/70

    Pure #irtual fun!tion

    Sanjay Patel

    50

  • 8/10/2019 Polymorphism OOCP

    51/70

    Only a #irtual mem er fun!tion !an e pure.

    Memem er

    Sanjay Patel

    51

  • 8/10/2019 Polymorphism OOCP

    52/70

    e!larin$ a #irtual fun!tion pure is not t"esame as denin$ a #irtual fun!tion %it" anempty ody.

    Anot"er t"in$

    Sanjay Patel

    52

  • 8/10/2019 Polymorphism OOCP

    53/70

    A ! !l *i l i $

  • 8/10/2019 Polymorphism OOCP

    54/70

    A stra!t ase !lasses*implementin$polymorp"ism

    Sanjay Patel

    54

  • 8/10/2019 Polymorphism OOCP

    55/70

    Sanjay Patel55

  • 8/10/2019 Polymorphism OOCP

    56/70

    Sanjay Patel56

    a stra!t ase !lass !lass Trian$le: pu li! Poly$on

  • 8/10/2019 Polymorphism OOCP

    57/70

    Sanjay Patel57

    !lass Poly$on8 prote!ted: int %idt","ei$"t6 pu li!: #oid set7#alues *int a, int

    8%idt"0a6 "ei$"t0 6 9 #irtual int area *#oid 0;696

    !lass Me!tan$le: pu li!Poly$on8 pu li!: int area *#oid

    8

    p y8 pu li!: int area *#oid 8return *%idt" 2 "ei$"t < 6 9 96int main * 8 Me!tan$le re!t6

    Trian$le tr$l6 Poly$on 2 ppoly5 0 Nre!t6 Poly$on 2 ppoly< 0 Ntr$l6 ppoly5-?set7#alues *U, 6

    ppoly

  • 8/10/2019 Polymorphism OOCP

    58/70

    Sanjay Patel58

  • 8/10/2019 Polymorphism OOCP

    59/70

    Sanjay Patel59

  • 8/10/2019 Polymorphism OOCP

    60/70

    Implementin$ polymorp"ism

    Sanjay Patel

    60

    ynami! 3emory 3ana$ement:

  • 8/10/2019 Polymorphism OOCP

    61/70

    ynami! memory mana$ement=na les pro$rammers to allo!ate and deallo!atememory for any uilt-in or user-defined type.

    Performed y operators ne%ne% and deletedelete .or e&ample, dynami!ally allo!atin$ memory for

    an array instead of usin$ a fi&ed-si e array.

    ynami! 3emory 3ana$ement:Operators ne%ne% and deletedelete

    Sanjay Patel

    61

  • 8/10/2019 Polymorphism OOCP

    62/70

    Operator ne%ne%Allo!ates *i.e., reser#es stora$e of t"e propersi e for an o je!t at e&e!ution time

    Calls a !onstru!tor to initiali e t"e o je!t.Meturns a pointer of t"e type spe!ified to t"eri$"t of ne%ne%.Can e used to dynami!ally allo!ate any

    fundamental type *su!" as intint or dou ledou le orany !lass type.

    Operator ne%ne%

    Sanjay Patel

    62

  • 8/10/2019 Polymorphism OOCP

    63/70

    Operator deletedeleteestroys a dynami!ally allo!ated o je!t.

    Calls t"e destru!tor for t"e o je!t.eallo!ates *i.e., releases memory from t"e

    free store. T"e memory !an t"en e reused y t"e

    system to allo!ate ot"er o je!ts.

    Operator deletedelete

    Sanjay Patel

    63

  • 8/10/2019 Polymorphism OOCP

    64/70

    ynami! 3emory mana$ement

    Sanjay Patel

    64

  • 8/10/2019 Polymorphism OOCP

    65/70

    elete operator

    Sanjay Patel

    65

  • 8/10/2019 Polymorphism OOCP

    66/70

  • 8/10/2019 Polymorphism OOCP

    67/70

    T"e 'ey%ord t"is identifies a spe!ial type ofpointer. Suppose t"at you !reate an o je!tnamed & of !lass A, and !lass A "as a no

    stati! mem er fun!tion f* .If you !all t"e fun!tion &.f* , t"e 'ey%ord t"is int"e ody of f* stores t"e address of &.

    ou !annot de!lare t"e t"is pointer or ma'eassi$nments to it.A stati! mem er fun!tion does not "a#e a t"ispointer.

    t"is pointer

    Sanjay Patel

    67

  • 8/10/2019 Polymorphism OOCP

    68/70

    riend fun!tions do not "a#e a this pointer,e!ause friends are not mem ers of a !lass.

    Only mem er fun!tions "a#e a this pointer.

    Point to remem er

    Sanjay Patel

    68

  • 8/10/2019 Polymorphism OOCP

    69/70

    =&ample

    Sanjay Patel

    69

    l

  • 8/10/2019 Polymorphism OOCP

    70/70

    =&ample70