OOP Complete Notes

Embed Size (px)

Citation preview

  • 7/25/2019 OOP Complete Notes

    1/635

    OOP COMPLETE NOTEBY

    Umer Tanvir

  • 7/25/2019 OOP Complete Notes

    2/635

    CSC241:

    Object OrientedProgrammingSpring 21!

    1" Starting OOP

    Please turn OFF your Mobile Phones!

    1#2$#1%.ar+an &adi,

  • 7/25/2019 OOP Complete Notes

    3/635

    St)d- &((ignment

    ' +ope -o) did go t+ro)g+ c+apter 1 o* bot+ t+e 0o a( t+e eperience3

    +at did -o) ,earn3

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    4/635

    Proced)ra, v(" Object5Oriented

    Proced)ra,

    it+dra6 depo(it6 tran(*er

    Object Oriented

    C)(tomer6 mone-6 acco)nt

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    5/635

    Object5Orientation7OO8

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- $

  • 7/25/2019 OOP Complete Notes

    6/635

    +at i( Object5Orientation3

    & tec+ni9)e *or (-(tem modeling

    OO modelcon(i(t( o* (evera, interacting object(

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- %

  • 7/25/2019 OOP Complete Notes

    7/635

    +at i( a Mode,3

    & mode, i( an ab(traction o* (omet+ing

    P)rpo(e i( to )nder(tand t+e prod)ct be*ore devit

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- :

  • 7/25/2019 OOP Complete Notes

    8/635

    Eamp,e( ; Mode,

    0ig+a- map(

    &rc+itect)ra, mode,(

    Mec+anica, mode,(

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- Eamp,e ; OO Mode,

    Object( &,i

    0o)(e

    Car

    Tree

    'nteraction( &,i ,ive( in t+e +o)(e

    &,i drive( t+e car

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- 1

    Ali

    Car

    House

    Tree

    lives-in

    drives

  • 7/25/2019 OOP Complete Notes

    11/635

    Object5Orientation 5 &dvantage

    Peop,e t+in/ in term( o* object(

    OO mode,( map to rea,it-

    T+ere*ore6 OO mode,( are ea(- to deve,op

    ea(- to )nder(tand

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- 11

  • 7/25/2019 OOP Complete Notes

    12/635

    +at i( an Object3

    &n object i(

    Somet+ing tangib,e 7&,i6 Car8

    Somet+ing t+at can be appre+ended inte,,ect)a,7Time6 ?ate8

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- 12

  • 7/25/2019 OOP Complete Notes

    13/635

    > +at i( an Object3

    &n object +a(

    State 7attrib)te(8

    e,,5de@ned be+avio)r 7operation(8

    Uni9)e identit-

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- 1!

  • 7/25/2019 OOP Complete Notes

    14/635

    Eamp,e ; &,i i( a Tangib,e Object

    State 7attrib)te(8 Name

    &ge

    be+avio)r 7operation(8 a,/(

    Eat( 'dentit-

    0i( name

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- 14

  • 7/25/2019 OOP Complete Notes

    15/635

    Eamp,e ; Car i( a Tangib,e Object

    State 7attrib)te(85 Co,or5 Mode,

    be+avio)r 7operation(85 Start Car

    - &cce,erate5 C+ange Aear

    'dentit-5 't( regi(tration n)mber

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- 1$

  • 7/25/2019 OOP Complete Notes

    16/635

    Eamp,e ; Time i( an Object&ppre+ended 'nte,,ect)a,,-

    State 7attrib)te(85 0o)r( 5 Second(5 Min)te(

    be+avio)r 7operation(85 Set 0o)r( 5 Set Second(5 Set Min)te(

    'dentit-5 o),d +ave a )ni9)e '? in t+e mode,

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- 1%

  • 7/25/2019 OOP Complete Notes

    17/635

    Eamp,e ; ?ate i( an Object&ppre+ended 'nte,,ect)a,,-

    State 7attrib)te(8-Year 5 ?a-- Mont+

    be+avio)r 7operation(85 Set Year 5 Set ?a-

    5 Set Mont+ 'dentit-

    5 o),d +ave a )ni9)e '? in t+e mode,

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- 1:

  • 7/25/2019 OOP Complete Notes

    18/635

    ?e@nition

    What Is an Object? &n object i( a (o*tare b)nd,e o* re,ated variab,e

    met+od(" So*tare object( are o*ten )(ed to moor,d object( -o) @nd in ever-da- ,i*e"

    Object( are /e- to )nder(tanding object-oriented

    tec+no,og-" Yo) can ,oo/ aro)nd -o) no and (eeamp,e( o* rea,5or,d object( dog6 de(/6 te,evi(et6 bic-c,e"

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    19/635

    ea,5or,d object( (+are toc+aracteri(tic(

    T+e- a,, +ave stateand behavior dog( +ave (tate 7name6 co,or6 breed6 +)ngr-8 an

    be+avior 7bar/ing6 *etc+ing6 and agging tai,8"

    Bic-c,e( +ave (tate 7c)rrent gear6 c)rrent peda,cadence6 to +ee,(6 n)mber o* gear(8 and be+

    7bra/ing6 acce,erating6 (,oing don6 c+anging g

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    20/635

    So*tare object( are mode,ed a*teror,d object(

    & (o*tare object maintain( it( (tate in one or mvariables"

    & (o*tare object imp,ement( it( be+avior it+ m" & met+od i( a *)nction 7()bro)tine8 a((ociated object"

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    21/635

    Can repre(ent rea,5or,d object( b-)(ing (o*tare object("

    Yo) mig+t ant to repre(ent rea,5or,d dog( a( (object( in an animation program or a rea,5or,d a( a (o*tare object in t+e program t+at contro,(e,ectronic eerci(e bi/e"

    Yo) can a,(o )(e (o*tare object( to mode, ab(trconcept(" .or eamp,e6 an eventi( a common ob)(ed in AU' indo (-(tem( to repre(ent t+e ac)(er pre((ing a mo)(e b)tton or a /e- on t+e /e

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    22/635

    Moving to ne t+in/ing >

    C i( a proced)ra, ,ang)age" & proced)re i(a ,i(t o* in(tr)ction(" Der- (ma,, program( 7ten( o* ,ine(8 need no

    organiation"

    &( t+e- get ,arge 7+)ndred( o* ,ine(86 t+e- aredivided into *)nction("

    .)nction( are (ti,, ,i(t( o* in(tr)ction("

    ?ividing a program into *)nction( give( a(tr)ct)re to t+e program 7+ence (tr)ct)redprogramming8"

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    23/635

    Moving to ne t+in/ing >

    C programming cannot do t+e *o,,oing e, .)nction( +ave )nre(tricted acce(( to g,oba, dat

    ?ata and .)nction( are )nre,atedF t+e- do not*orm a ,ogica, gro)p or (+o an- *orm o* binding

    Cannot mode, Grea, or,dH"

    'n rea, or,d6 e dea, it+ object( ,i/e car(6

    peop,e etc" &re ()c+ object( ,i/e GdataH3 +ic+ data t-pe

    de(cribe( a car6 *or eamp,e3

    &re ()c+ object( ,i/e G*)nction(H3 +at i,, a*)nction car78 do3

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    24/635

    Moving to ne t+in/ing >

    & rea, or,d object6 e"g" car '( it( de(cription p)re,- t+e abi,it- to +ave a

    Gdata t-peH to repre(ent it( (peci@cation( orGattrib)te(H on,-3 .or eamp,e +at attrib)te(de(cribe a car3

    So b- +aving a,, t+e(e attrib)te( Gon,-H do -o)/no a,, abo)t a car3 o),d -o) b)- a car b-

    mere,- ,oo/ing at t+e(e attrib)te( and t+eirva,)e(3

    NoI +at e,(e do -o) i(+ to +ave3

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    25/635

    Moving to ne t+in/ing >

    & rea, or,d object6 e"g" carTe(t driveI ig+t3

    +at o),d -o) /no t+ro)g+ a te(t drive +oit Gbe+ave(H in re(pon(e to vario)( (tim),)(I

    0o can it negotiate a (peed bra/er6 a (peed- t)rn*),, bra/ing etc"

    EJective,-6 -o) i(+ to /no +o it G*)nction(

    Be+avio)r i( ,i/e a *)nction" So neit+er data nor *)nction(6 b-

    t+em(e,ve(6 mode, rea,5or,d object(eJective,-"

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    26/635

    Object Oriented &pproac+ Neit+er data nor *)nction(6 b- t+em(e,ve(6 mode,

    rea,5or,d object( eJective,-"

    OO ,ang)age( 7,i/e CKK8 combine bot+ GdataH andG*)nction( t+at operate on t+at dataH into a (ing,e)nit6 ca,,ed GobjectH" 0ence Gencap(),atingH an entit

    T+e object( *)nction( are ca,,ed member *)nction("

    T-pica,,-6 data can be acce((ed t+ro)g+ *)nction(on,-" So data get( G+iddenH"

    ?ata +iding en()re( t+e data i( not accidenta,,-a,tered"

    e*erence ana,og- o* a groing compan-F 2 peop,ev( 1 emp,o-ee compan-"

    Object( comm)nicate it+ eac+ ot+er b- ca,,ingeac+ ot+erH( member *)nction("

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    27/635

    Object Oriented &pproac+ > OOP i( a,, abo)t GorganiationH and not abo)t

    program operation" Mo(t individ)a,

    (tatement( are (imi,ar to C (tatement("Member *)nction( ma- be ver- (imi,ar to Cproced)ra, *)nction("

    'n C -o) )(ed to t+in/ abo)t *)nction("

    'n CKK -o) (+o),d t+in/ abo)t object("

    O*ten object( in CKK are rea,5or,d ana,ogie 'n CKK object( are Gin(tance(H o* Gc,a((e(H"

    E"g" 0onda Cit- i( an in(tance o* c,a(( carI

    C,a(( (erve( a( a coo/ie c)tter and an objeca coo/ie"

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    28/635

    &b(traction

    &b(traction i( a a- to cope it+ comp,eit-"

    Princip,e o* ab(traction

    Capt)re on,- t+o(e detai,( abo)t an object t+are,evant to c)rrent per(pective

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- 2

    Nat)ra,Set>

    add7 e,em 8>

    add7 e,em 8>

    adde,ement tot+e (et

    '* e,em 1

    t+en errore,(e

    adde,ement tot+e (et

    Overriding

  • 7/25/2019 OOP Complete Notes

    73/635

    Overriding

    & c,a(( ma- need to override t+e de*a),t be+avioprovided b- it( ba(e c,a((

    ea(on( *or overriding Provide be+avio)r (peci@c to a derived c,a(( Etend t+e de*a),t be+avio)r e(trict t+e de*a),t be+avio)r 'mprove per*ormance

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- :!

    Eamp,e ; Speci@c Be+avio)r

  • 7/25/2019 OOP Complete Notes

    74/635

    Eamp,e Speci@c Be+avio)r

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- :4

    Shapeco,orvertice(

    dramove(etCo,or

    Circ,eradi)(dracomp)te&rea

    Line,engt+dra

    Triang,eang,e

    dracomp)te&r

    Eamp,e ; Eten(ion

  • 7/25/2019 OOP Complete Notes

    75/635

    p

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- :$

    indoidt+

    +eig+topenc,o(edra

    ?ia,ogBocontro,(enab,edra

    15 'nvo/eindoH(dra25 dra t+edia,og bo

    Eamp,e ; e(triction

  • 7/25/2019 OOP Complete Notes

    76/635

    p

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- :%

    IntegerSet>

    Nat)ra,Set>

    add7 e,em 8>

    add7 e,em 8>

    &dd e,ementto t+e (et

    '* e,em 1

    t+en give errore,(e

    &dd e,ementto t+e (et

    Eamp,e ; 'mprove Per*ormance

  • 7/25/2019 OOP Complete Notes

    77/635

    C,a(( Circ,eoverride( rotateoperation o* c,a((S+ape it+ a N),,operation"

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- ::

    Shapeco,or

    coorddrarotate(etCo,or

    Circ,eradi)(drarotate

  • 7/25/2019 OOP Complete Notes

    78/635

    Eamp,e ; &b(tract C,a((e(

  • 7/25/2019 OOP Complete Notes

    79/635

    p

    0ere6 Per(on i( an ab(tract c,a((1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- :=

    Teac+er?octorSt)dent

    Personname

    agegendereata,/

    Eamp,e ; &b(tract C,a((e(

  • 7/25/2019 OOP Complete Notes

    80/635

    p

    0ere6 De+ic,e i( an ab(tract c,a((1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    81/635

    & concrete c,a(( imp,ement( a concrete concept

    Main p)rpo(e i( to be in(tantiated

    Provide( imp,ementation detai,( (peci@c to t+e d

    contet

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    82/635

    0ere6 St)dent6 Teac+er and ?octor areconcrete c,a((e(1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    83/635

    M),tip,e 'n+eritance

  • 7/25/2019 OOP Complete Notes

    84/635

    e ma- ant to re)(e c+aracteri(tic( o* more t+parent c,a((

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    85/635

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    86/635

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    87/635

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    88/635

  • 7/25/2019 OOP Complete Notes

    89/635

    Prob,em ; ?)p,icate .eat)re(

  • 7/25/2019 OOP Complete Notes

    90/635

    +ic+ eatoperation Mermaidin+erit(3

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- =

    Mermaid

    oman .i(+

    eat>

    eat>

    So,)tion ; Override t+e Common.eat)re

  • 7/25/2019 OOP Complete Notes

    91/635

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- =1

    Mermaid

    oman .i(+eat>

    eat>

    eat>

    'nvo/e eatoperation ode(iredc,a((

    Prob,em ; ?)p,icate .eat)re( 7?iamond Prob,em

  • 7/25/2019 OOP Complete Notes

    92/635

    +ic+ changeGearoperation &mp+ibio)(De+ic,e in+erit(3

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- =2

    &mp+ibio)( De+ic,e

    Land De+ic,e ater De+ic,e

    De+ic,e

    Car Boat

    c+angeAear

  • 7/25/2019 OOP Complete Notes

    93/635

  • 7/25/2019 OOP Complete Notes

    94/635

    ind( o* &((ociation

  • 7/25/2019 OOP Complete Notes

    95/635

    C,a(( &((ociation

    'n+eritance

    Object &((ociation Simp,e &((ociation

    Compo(ition

    &ggregation

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- =$

    Simp,e &((ociation

  • 7/25/2019 OOP Complete Notes

    96/635

    '( t+e ea/e(t ,in/ beteen object(

    '( a re*erence b- +ic+ one object can interact (ome ot+er object

    '( (imp,- ca,,ed a( a((ociation

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- =%

    ind( o* Simp,e &((ociation

  • 7/25/2019 OOP Complete Notes

    97/635

    "r"t navigation

    One5a- &((ociationTo5a- &((ociation

    "r"t n)mber o* object( Binar- &((ociation

    Ternar- &((ociation N5ar- &((ociation

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- =:

    One5a- &((ociation

  • 7/25/2019 OOP Complete Notes

    98/635

    e can navigate a,ong a (ing,e direction on,-

    ?enoted b- an arro toard( t+e (erver object

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- =

  • 7/25/2019 OOP Complete Notes

    99/635

    Eamp,e ; &((ociation

  • 7/25/2019 OOP Complete Notes

    100/635

    &,i drive( +i( Car

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- 1

    &,i Cardrive(1

  • 7/25/2019 OOP Complete Notes

    101/635

    Eamp,e ; To5a- &((ociation

  • 7/25/2019 OOP Complete Notes

    102/635

    Emp,o-ee or/( *or compan-

    Compan- emp,o-( emp,o-ee(

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- 12

    Emp,o-ee Compan-or/(5*or 1

  • 7/25/2019 OOP Complete Notes

    103/635

    Binar- &((ociation

  • 7/25/2019 OOP Complete Notes

    104/635

    &((ociate( object( o* eact,- to c,a((e(

    ?enoted b- a ,ine6 or an arro beteen t+e a((oobject(

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- 14

  • 7/25/2019 OOP Complete Notes

    105/635

  • 7/25/2019 OOP Complete Notes

    106/635

    Ternar- &((ociation

  • 7/25/2019 OOP Complete Notes

    107/635

    &((ociate( object( o* eact,- t+ree c,a((e(

    ?enoted b- a diamond it+ ,ine( connected toa((ociated object(

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- 1:

    Eamp,e ; Ternar- &((ociation

  • 7/25/2019 OOP Complete Notes

    108/635

    Object( o* eact,- t+ree c,a((e( area((ociated

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- 1(tr)ct TEST

    W

    private

    int F

    (tr)ct TEST

    W

    private

  • 7/25/2019 OOP Complete Notes

    163/635

    F

    p)b,ic

    void Set7int va,8 W va,F XF

    int Aet78 W ret)rn F XF

    X

    TEST varF

    var"Set718F

    int - var"Aet78F

    1#2$#1%Nationa, Univer(it- o* Comp)ter and Emerging

    Science(

    p

    int FX

    TEST varF

    var" 1F ##e

    int - var"F ##e

    So even ,egitimate acce(( o* data goe( t+ro)g+ inter*aceI e +ave (ec)red t+e data *)rt+erIII

    Member *)nction( de@nitionstructTEST W

    private

    int F

    p)b,ic

    void Set7int va,8F

    int Aet78F

    e can dec,aremember *)nction(in(ide t+e (tr)ct an

  • 7/25/2019 OOP Complete Notes

    164/635

    1#2$#1%Nationa, Univer(it- o* Comp)ter and Emerging

    Science(

    78F

    Xvoid +,-+::Set7int va,8 W

    va,F

    X

    int +,-+::Aet78 W

    ret)rn F

    X

    main78 W

    TEST varF

    var"Set718F

    int - var"Aet78F

    X

    de@ne t+em o)t(ida( e,, )(ing t+ename o* (tr)ct tore(o,ve ambig)it-"

    D" 'MP Note t+at t+a,,o( )( to (epara+eader andimp,ementation @,e

    C,a(( Compatibi,it-

    & c,a(( i( be+aviora,,- compatib,e it+ anot+er i()pport( a,, t+e operation( o* t+e ot+er c,a((

  • 7/25/2019 OOP Complete Notes

    165/635

    S)c+ a c,a(( i( ca,,ed ()bt-pe

    & c,a(( can be rep,aced b- it( ()bt-pe

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1%%

    >C,a(( Compatibi,it-

    ?erived c,a(( i( )()a,,- a ()bt-pe o* t+e ba(e c,a

  • 7/25/2019 OOP Complete Notes

    166/635

    't can +and,e a,, t+e ,ega, me((age( 7operation(8ba(e c,a((

    T+ere*ore6 ba(e c,a(( can alwaysbe rep,aced b-derived c,a((

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1%:

    Eamp,e ; C,a(( Compatibi,it-Shape

    color

    vertices

    move

  • 7/25/2019 OOP Complete Notes

    167/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1%'denti*- C,a((e(

    E,iminate irre,evant c,a((e(

  • 7/25/2019 OOP Complete Notes

    179/635

    Editor ; Der- broad (cope

    U(er ; O)t o* (-(tem bo)ndar-

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1'denti*- C,a((e(

    &dd c,a((e( b- ana,-ing re9)irement(

  • 7/25/2019 OOP Complete Notes

    180/635

    Aro)p ; re9)ired to be+ave a( a (+ape 'ndivid)a, (+ape( can be gro)ped toget+er and can

    a( a (ing,e (+ape

    Die ; editor m)(t +ave a di(p,a- area

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1'denti*- C,a((e(

    S+ape

    Following classes have &een identi'ied(

  • 7/25/2019 OOP Complete Notes

    181/635

    Line

    Circ,e

    Triang,e

    Men)

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1

  • 7/25/2019 OOP Complete Notes

    182/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1

    Line

    Circle

    Triangle

    "iew

    )enu

    'denti*- &((ociation(

    Etract verb( connecting object(

    di id , + b d +

  • 7/25/2019 OOP Complete Notes

    183/635

    'ndivid)a, (+ape( can be gro)ped toget+er Aro)p con(i(t( o* ,ine(6 circ,e(6 triang,e(

    Aro)p can a,(o con(i(t( o* ot+er gro)p(7Compo(ition8

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1 'denti*- &((ociation(

    Deri*- acce(( pat+(

    Di i +

  • 7/25/2019 OOP Complete Notes

    184/635

    Die contain( (+ape( Die contain( ,ine(

    Die contain( circ,e(

    Die contain( triang,e(

    Die contain( gro)p(

    7&ggregation8

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1 'denti*- &((ociation(

    Deri*- acce(( pat+(

    M d t Di

  • 7/25/2019 OOP Complete Notes

    185/635

    Men) (end( me((age to Die7Simp,e One5a- &((ociation8

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1

  • 7/25/2019 OOP Complete Notes

    186/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1

  • 7/25/2019 OOP Complete Notes

    187/635

    Propertie( are not mentioned

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1'denti*- &ttrib)te(Etract propertie( o* t+e object

    .rom t+e domain /no,edge

    Li

    T i l

  • 7/25/2019 OOP Complete Notes

    188/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1'denti*- &ttrib)te(Etract propertie( o* t+e object

    .rom t+e domain /no,edge

    %roup

    )enu

  • 7/25/2019 OOP Complete Notes

    189/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1=

    %roupno+'+&,ects "iew

    no+'+&,ects

    selected

    )enuameis+pen

    Object Mode, ; Arap+ic Editor

    )enu

    nameis+pen

    "iew

    no+'+&,ectsselected

    Shape

    colorverticesnn

  • 7/25/2019 OOP Complete Notes

    190/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1=1

    Line

    length

    Circle

    radius%roup

    no+'+&,ects

    Triangle

    angle

    n

    nn

    n

    nn

    n

    n

    n n

    nn

    nn

    n

    'denti*- Operation(Etract verb( connected it+ an object

    !evelopa graphic editor that can draw

    di''erent geometric shapes such as line

  • 7/25/2019 OOP Complete Notes

    191/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1=2

    di''erent geometric shapes such as line.circle and triangle/ 0ser can select. move

    or rotatea shape/ To do so. editor provid

    user with a menu listing di''erent

    commands/ Individual shapes can &egroupedtogether and can &ehaveas a

    single shape/

    > 'denti*- Operation(E,iminate irre,evant operation(

    ?eve,op o)t o* (-(tem bo)ndar-

  • 7/25/2019 OOP Complete Notes

    192/635

    ?eve,op ; o)t o* (-(tem bo)ndar-

    Be+ave ; +ave broad (emantic(

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1=!

    >'denti*- Operation(.o,,oing are (e,ected operation(

    Line

    Circle

  • 7/25/2019 OOP Complete Notes

    193/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1=4

    Line!rawSelect

    )ove

    *otate

    Circle!rawSelect

    )ove

    *otate

    >'denti*- Operation(.o,,oing are (e,ected operation(

    Triangle!

    Shape!

  • 7/25/2019 OOP Complete Notes

    194/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1=$

    Triangle!rawSelect

    )ove

    *otate

    Shape!rawSelect

    )ove

    *otate

    >'denti*- Operation(.o,,oing are (e,ected operation(

    %roup

    !raw

    )enu

    +pen

  • 7/25/2019 OOP Complete Notes

    195/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1=%

    !rawSelect

    )ove

    *otate

    +penSelect

    )ove

    *otate

    >'denti*- Operation(Etract operation( )(ing domain

    /no,edge

    "iew

  • 7/25/2019 OOP Complete Notes

    196/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1=:

    "iewAdd

    *emove

    %roup

    Show

    Select

    )ove

    *otate

    Shap

    colorvertic

    drawselecmoverotate

    "iewno+'+&,ectsselected

    add12remove12group12show12

    select12move12

    nn

    )enu

    nameis+pen

    open12select12move12rotate12

  • 7/25/2019 OOP Complete Notes

    197/635

    %rou

    no+'+&

    draw12

    Triangle

    angle

    draw12

    nnCircle

    radius

    draw12nn

    Line

    length

    draw12

    nn

    nn

    12move12rotate12

    nn

    nnnn

    12

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 1='denti*- 'n+eritance

    B- ana,-ing re9)irement(

  • 7/25/2019 OOP Complete Notes

    199/635

    3'ndivid)a, (+ape( can be gro)ped toget+er and be+ave a( a (ing,e (+ape4

    Aro)p in+erit( *rom S+ape

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 2

    e@ning t+e Object Mode, &pp,ication o* in+eritance demand( an iteration

    +o,e object mode,

    'n t+e in+eritance +ierarc+

  • 7/25/2019 OOP Complete Notes

    200/635

    'n t+e in+eritance +ierarc+-6 &,, attrib)te( are (+ared

    &,, a((ociation( are (+ared

    Some operation( are (+ared

    Ot+er( are overridden

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 21

    >e@ning t+e Object Mode,

    S+are a((ociation(

  • 7/25/2019 OOP Complete Notes

    201/635

    Die contain( a,, /ind o* (+ape(

    Aro)p con(i(t( o* a,, /ind o* (+ape(

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 22

    >e@ning t+e Object Mode,

    S+are attrib)te(

  • 7/25/2019 OOP Complete Notes

    202/635

    S+ape ; Line6 Circ,e6 Triang,e and Aro)p Co,or6 vertice(

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 2!

    >e@ning t+e Object Mode,

    S+are operation(

  • 7/25/2019 OOP Complete Notes

    203/635

    S+ape ; Line6 Circ,e6 Triang,e and Aro)p Se,ect

    Move

    otate

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 24

  • 7/25/2019 OOP Complete Notes

    204/635

  • 7/25/2019 OOP Complete Notes

    205/635

  • 7/25/2019 OOP Complete Notes

    206/635

    C,a(( C,a(( i( a too, to rea,ie object(

    C,a(( i( a too, *or de@ning a ne t-pe

  • 7/25/2019 OOP Complete Notes

    207/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 2

  • 7/25/2019 OOP Complete Notes

    208/635

  • 7/25/2019 OOP Complete Notes

    209/635

  • 7/25/2019 OOP Complete Notes

    210/635

    &b(traction On,- inc,)de detai,( in t+e (-(tem t+at are re9)i

    ma/ing a *)nctiona, (-(tem

    St)dent Name

    .ele&ant to our problem

  • 7/25/2019 OOP Complete Notes

    211/635

    &ddre((

    Sib,ing

    .at+er B)(ine((

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 212

    /ot rele&ant to our problem

  • 7/25/2019 OOP Complete Notes

    212/635

  • 7/25/2019 OOP Complete Notes

    213/635

    +- Member .)nctionT+e- mode, t+e be+avior( o* an object

    Object( can ma/e t+eir data invi(ib,e

    Object remain( in con(i(tent (tate

  • 7/25/2019 OOP Complete Notes

    214/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 21$

  • 7/25/2019 OOP Complete Notes

    215/635

  • 7/25/2019 OOP Complete Notes

    216/635

    ?ec,aring c,a(( variab,e( Dariab,e( o* c,a((e( 7object(8 are dec,ared j)(t ,i/

    variab,e( o* (tr)ct)re( and b)i,t5in data t-pe(

    T-peName Daraiba,eNameF

  • 7/25/2019 OOP Complete Notes

    217/635

    -p

    int varF

    St)dent aSt)dentF

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 21

  • 7/25/2019 OOP Complete Notes

    218/635

    Eamp,eclass Student{

    int rollNo;

    !oid setRollNo"int

    aNo$;};

  • 7/25/2019 OOP Complete Notes

    219/635

    };

    Student aStudent;

    aStudent%rollNo;

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 22

    Error

    (tr)ct 5Z c,a(( tran(itionIclassTEST Wprivate

    int F

    p)b,ic

    void Set7int va,8 W va,F XF

    structTEST W

    private

    int F

    p)b,ic

    void Set7int va,8 W

    va,F XFint Aet78 W ret)rn F XF

  • 7/25/2019 OOP Complete Notes

    220/635

    int Aet78 W ret)rn F X

    X

    main78 W

    TEST varF

    var"Set718Fint - var"Aet78F

    X1#2$#1%

    Nationa, Univer(it- o* Comp)ter and EmergingScience(

    X

    main78 W

    TEST varF

    var"Set718F

    int - var"Aet78FX

    ep,acing struct withclass6 doe( not +ave an- aJectI

    T+i( i( t+e /e-ord t+at OOL 7CKK8 provide *or OOPI

  • 7/25/2019 OOP Complete Notes

    221/635

  • 7/25/2019 OOP Complete Notes

    222/635

    &cce(( (peci@er(

  • 7/25/2019 OOP Complete Notes

    223/635

    &cce(( (peci@er(

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 224

    &cce(( (peci@er(T+ere are t+ree acce(( (peci@er(

    Gp)b,icH i( )(ed to te,, t+at member can be acce((ed -o) +ave acce(( to t+e object

    GprivateH i( )(ed to te,, t+at member can on,- be acce*rom a member *)nction

  • 7/25/2019 OOP Complete Notes

    224/635

    GprotectedH to be di(c)((ed +en e cover in+eritanc

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 22$

    Eamp,eclass Student{

    pri!ate&

    char * name;

    int rollNo;

    'annot (e accessed outside c)ass

  • 7/25/2019 OOP Complete Notes

    225/635

    pu'lic&

    !oid setName"char *$;

    !oid setRollNo"int$;

    %%%};

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 22%

    'an (e

    accessed

    outside c)ass

    Eamp,eclass Student{

    %%%

    int rollNo;

    pu'lic&!oid setRollNo"int aNo$;

  • 7/25/2019 OOP Complete Notes

    226/635

    !oid setRollNo"int aNo$;

    };

    int main"${

    Student aStudent;

    aStudent%SetRollNo"($;}

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 22:

  • 7/25/2019 OOP Complete Notes

    227/635

    Eamp,eclass Student

    {

    char * name;

    int RollNo;

    class Student

    {

    pri!ate&

    char * name;

  • 7/25/2019 OOP Complete Notes

    228/635

    };;

    int RollNo;

    };

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 22=

    Eamp,eclass Student

    {

    char * name;

    int RollNo;

  • 7/25/2019 OOP Complete Notes

    229/635

    !oid SetName"char *$;

    };

    Student aStudent;

    aStudent%SetName"li$;

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 2!

    Error

    Eamp,eclass Student

    {

    char * name;

    int RollNo;pu'lic&

  • 7/25/2019 OOP Complete Notes

    230/635

    p

    !oid setName"char *$;

    };

    Student aStudent;

    aStudent%SetName")li$;

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 2!1

    Uni@ed Mode,ing Lang)age 7UML8 c,a((diagram

    10..*

    abstract

    association

    13using42

  • 7/25/2019 OOP Complete Notes

    231/635

    1#2$#1%Nationa, Univer(it- o* Comp)ter and Emerging

    Science(

    privateinheritance

    13is a42

    &((ignment 7CP8 'n(ta,, 'BM ationa, o(e or an- ot+er UML too, a

    dra t+e diagram( )(ed in t+i( ,ect)re and ta/e o)t o* t+o(e diagram( to (+o me +at -o) +av

    ?ead,ine net c,a((

  • 7/25/2019 OOP Complete Notes

    232/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    233/635

    T+an/(

  • 7/25/2019 OOP Complete Notes

    234/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    235/635

    evie C,a((

    Concept

    ?e@nition

    ?ata member( Member .)nction(

  • 7/25/2019 OOP Complete Notes

    236/635

    Member .)nction(

    &cce(( (peci@er

    Member .)nction( Member *)nction( are t+e *)nction( t+at operate

    data encap(),ated in t+e c,a((

    P)b,ic member *)nction( are t+e inter*ace to t+e

  • 7/25/2019 OOP Complete Notes

    237/635

    Member .)nction( 7contd"8 ?e@ne member *)nction in(ide t+e c,a(( de@nitio

    O

    ?e@ne member *)nction o)t(ide t+e c,a(( de@nit B)t t+e- m)(t be dec,ared in(ide c,a(( de@nition

  • 7/25/2019 OOP Complete Notes

    238/635

    .)nction 'n(ide C,a(( Bod-class ClassName {

    pu'lic&ReturnType FunctionName"$ {

  • 7/25/2019 OOP Complete Notes

    239/635

    ReturnType FunctionName"$ {

    }};

    Eamp,e?e@ne a c,a(( o* (t)dentt+at +a( a ro,, n)mber" T+i(c,a(( (+o),d +ave a* ti t+ t b d

  • 7/25/2019 OOP Complete Notes

    240/635

    *)nction t+at can be )(edto (et t+e ro,, n)mber

    Eamp,eclass Student{

    int rollNo;

    pu'lic&!oid setRollNo"int aRollNo${

  • 7/25/2019 OOP Complete Notes

    241/635

    rollNo + aRollNo;

    }

    };

    .)nction O)t(ide C,a(( Bod-class ClassName{

    pu'lic&

    ReturnTypeFunctionName"$;

    };

  • 7/25/2019 OOP Complete Notes

    242/635

    }

    ReturnTypeClassName&&FunctionName"$

    {

    }

    Scoperesolution

    operator

  • 7/25/2019 OOP Complete Notes

    243/635

  • 7/25/2019 OOP Complete Notes

    244/635

    Eamp,einline int rea"int len, int hi$

    {

    return len * hi;

    }

    int main"$

  • 7/25/2019 OOP Complete Notes

    245/635

    int main"$

    {

    cout -- rea"(.,/.$;

    }

    'n,ine .)nction( '* e de@ne t+e *)nction in(ide t+e c,a(( bod- t+

    *)nction i( b- de*a),t an in,ine *)nction

    'n ca(e *)nction i( de@ned o)t(ide t+e c,a(( bod-

    e m)(t )(e t+e /e-ord Gin,ineH to ma/e a *)ncin,ine

  • 7/25/2019 OOP Complete Notes

    246/635

    Eamp,eclass Student{

    int rollNo;

    pu'lic&

    !oid setRollNo"int aRollNo${

  • 7/25/2019 OOP Complete Notes

    247/635

    rollNo + aRollNo;

    }

    };

    Eamp,eclass Student{

    pu'lic&

    inline !oid setRollNo"int aRollNo$;

    };

  • 7/25/2019 OOP Complete Notes

    248/635

    };

    !oid Student&&setRollNo"int aRollNo${

    rollNo + aRollNo;

    }

    Eamp,eclass Student{

    pu'lic&

    !oid setRollNo"int aRollNo$;};

  • 7/25/2019 OOP Complete Notes

    249/635

    inline !oid Student&&setRollNo"int aRollNo${

    rollNo + aRollNo;}

  • 7/25/2019 OOP Complete Notes

    250/635

    Con(tr)ctor

  • 7/25/2019 OOP Complete Notes

    251/635

    Con(tr)ctor Con(tr)ctor i( )(ed to initia,ie t+e object( o* a c

    Con(tr)ctor i( )(ed to en()re t+at object i( in ede@ned (tate at t+e time o* creation

    Con(tr)ctor i( a)tomatica,,- ca,,ed +en t+e objcreated

    Con(tr)ctor are not )()a,,- ca,,ed ep,icit,-

  • 7/25/2019 OOP Complete Notes

    252/635

    Con(tr)ctor are not )()a,,- ca,,ed ep,icit,-

    Con(tr)ctor 7contd"8 Con(tr)ctor i( a (pecia, *)nction +aving (ame na

    t+e c,a(( name

    Con(tr)ctor doe( not +ave ret)rn t-pe

    Con(tr)ctor( are common,- p)b,ic member(

  • 7/25/2019 OOP Complete Notes

    253/635

    Eamp,eclass Student{

    pu'lic&

    Student"${

    rollNo + .;

  • 7/25/2019 OOP Complete Notes

    254/635

    }

    };

    Eamp,eint main"$

    {

    Student aStudent;

    0*constructor is implicitly called at thipoint*0

  • 7/25/2019 OOP Complete Notes

    255/635

    }

    ?e*a),t Con(tr)ctor Con(tr)ctor it+o)t an- arg)ment i( ca,,ed de*a

    con(tr)ctor

    '* e do not de@ne a de*a),t con(tr)ctor t+e com

    i,, generate a de*a),t con(tr)ctorT+i( compi,er generated de*a),t con(tr)ctor initi

    t+e data member( to t+eir de*a),t va,)e(

  • 7/25/2019 OOP Complete Notes

    256/635

    Eamp,eclass Student

    {

    int rollNo;

    char *name;

    float GP;

  • 7/25/2019 OOP Complete Notes

    257/635

    pu'lic&

    00no constructors

    };

    Eamp,e

    Compi,er generated de*a),t con(tr)cto

    {

    rollNo + .;GP + .%.;

  • 7/25/2019 OOP Complete Notes

    258/635

    name + N122;

    }

    Con(tr)ctor Over,oading

    Con(tr)ctor( can +ave parameter(

    T+e(e parameter( are )(ed to initia,ie t+e datamember( it+ )(er ()pp,ied data

  • 7/25/2019 OOP Complete Notes

    259/635

    Eamp,e

    class Student{

    pu'lic&

    Student"$;

    Student"char * aName$;

  • 7/25/2019 OOP Complete Notes

    260/635

    Student"char * aName, int aRollNo$;

    Student"int aRollNo, int aRollNo, float a

    };

    Eamp,e

    Student&&Student"int aRollNo,

    char * aName${

    if"aRollNo - .${

    rollNo + .;}

    else {

  • 7/25/2019 OOP Complete Notes

    261/635

    rollNo + aRollNo;

    }

    }

    Eamp,e

    int main"$

    {

    Student student(;

    Student student/")Name$;

    Student student3"Name, ($;

  • 7/25/2019 OOP Complete Notes

    262/635

    Student student4"Name,(,4%.$;

    }

    Con(tr)ctor Over,oading

    U(e de*a),t parameter va,)e to red)ce t+e ritin

  • 7/25/2019 OOP Complete Notes

    263/635

    Eamp,e

    Student&&Student" char * aName + N122,

    int aRollNo+ .,

    float aGP + .%.${

    }

    '( e9)iva,ent to

  • 7/25/2019 OOP Complete Notes

    264/635

    Student"$;

    Student"char * aName$;

    Student"char * aName, int aRollNo$;Student"char * Name, int aRollNo, float aGP$;

    e*erence(

    Object Oriented Programming in CKK6 b- obert La*ore6 p)b,

    Sam( P)b,i(+ing 7T+e aite Aro)p8" 4t+ed" avai,ab,e in (o Object Oriented Programming U(ing CKK b- o-ce .arre,, 6 p)

    Co)r(e Tec+no,og-6 Cengage Learning" 4thed. available in soft form

    Nationa, Univer(it- o* Comp)ter Emerging ScQ"n)"ed)"p/R

  • 7/25/2019 OOP Complete Notes

    265/635

    Dirt)a, Univer(it- o* Pa/i(tan Qoc"v)"ed)"p/R

    Open Co)r(eare Con(orti)m

    Q+ttp##"occon(orti)m"org#en#co)r(e(R

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    0o to correct,- Searc+ *or a(o*tare [ goog,e

    +en e need a (o*tare6 t+e @r(t p,ace +ere e goi( at Aoog,e Searc+" '* -o) don\t /no t+e (o*tarename6 t+en e )(e (ome /e-ord( at Aoog,e Searc+7*or e"g" Note Ta/ing (o*tare6 Dideo Editing (o*tare6P+oto Editing (o*tare etc8" Once Aoog,e (+o )( t+ere(),t(6 e c,ic/ t+e ,in/( a*ter reading it( tit,e and

    (ome de(cription" .o,,oing ()c+ practice o*tena(te( o)r time b- c,ic/ing )(e,e(( ,in/(" +at mo(to* t+e peop,e don\t /no i( t+at6 -o) can ea(i,- (earc+*or t+e (o*tare # app,ication( at Aoog,e Searc+ )(ingit @,t i ti L t\ + d t+ t

  • 7/25/2019 OOP Complete Notes

    266/635

    it( @,tering option(" Let\( (ee +o e can do t+at

    Con(),t t+e @,e )p,oaded

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 2%:

    T+an/(

  • 7/25/2019 OOP Complete Notes

    267/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    CSC241:Object OrientedProgrammingSpring 21!

    1" .)nction(

  • 7/25/2019 OOP Complete Notes

    268/635

    Please turn OFF your Mobile Phones!

    1#2$#1%.ar+an &adi,

    evi(ion

    C+apter $ o* boo/

  • 7/25/2019 OOP Complete Notes

    269/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 2:

    .)nction(

    $ 'unctiongroups a number o' programstatements into a unit and gi&es it a name#

    +his unit can then be in&o8ed'rom other parts o'the program#

    +he most important reason to use 'unctions is toaid in the conceptual organi9ationo' a program

    $nother reason to use 'unctions is to reduce

  • 7/25/2019 OOP Complete Notes

    270/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 2:1

    program si9e# $ny seuence o' instructions thatappears in a program more than once is acandidate 'or being made into a 'unction#

    +he 'unction;s code is stored in only one placeinmemory< e&en though the 'unction is e0ecuted

    Return type

  • 7/25/2019 OOP Complete Notes

    271/635

    5nput ar6um1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 2:2

    .)nction(00demonstrates a simple function

    7include-iostream8usin6namespacestd;

    intcu'e"int9$; 00 function deration

    intmain"${ 00 tests the cu'e"$ function&

    intn + (;

    #hile"n :+ .${

    cin 88 n;

    cout --

  • 7/25/2019 OOP Complete Notes

    272/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 2:!

    system"P1S=$; return.;

    }00end of main

    intcu'e" int9 ${ 00 function definition

    return9*9*9; 00 returns cu'e of 9&} 00 {function 'ody}

    Return type

    .)nction(

    ,ach integer read is passed to the cube6*'unction by the

    call cube6n*# +he &alue returnedby the 'unction replaces

    the e0pression cube6n* and then is passed to the output

    object cout

    +he main6* 'unction passes the &alue = to the cube6*

  • 7/25/2019 OOP Complete Notes

    273/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 2:4

    +he main6*'unction passes the &alue = to the cube6*

    'unction< and the cube6*'unction returns the &alue2>= to

    the main6*'unction#

    +he argument nis passed by &alueto the 'ormal parameter

    0# +his simply means that 0 is assigned the &alue o' nwhen

    the 'unction is called#

    Default r6uments

    7include-iostream8

    usin6namespacestd;00declaration #ith default ar6uments

    !oidrepchar"char+>*>, int+4?$;

    intmain"${

    repchar"$; 00prints 4? asteris@s

    repchar">+>$; 00prints 4? eAual si6ns

    repchar">B>, 3.$; 00prints 3. plus si6ns

    system"P1S=$; return.;

    }

    00 displays line of characters

    !oidrepchar"charch, intn${

  • 7/25/2019 OOP Complete Notes

    274/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 2:$

    p " , ${

    00 defaults supplied if necessary

    for"int +.; -n; BB$ 00 loops n times

    cout -- ch; 00 prints ch

    cout -- endl;

    }

    5nline unction

    function call in!ol!es su'stantial o!erhead

    =9tra time and space ha!e to 'e used to in!o@ethe function, pass parameters to it, allocatestora6e for its local !aria'les, store thecurrent !aria'les and the location of e9ecutioin the main pro6ram, etc%

    5n some cases, it is 'etter to a!oid all this

    'y specifyin6 the function to 'e inline% Thistells the compiler to replace each call to thefunction #ith e9plicit code for the function%

    To the pro6rammer, an inline function appears

  • 7/25/2019 OOP Complete Notes

    275/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 2:%

    p 6 , ppthe same as an ordinary function, e9cept forthe use of the inlinespecifier%

    5nline unction

    00 demonstrates inline functions

    7include-iostream8usin6namespacestd;

    inlinefloatl'sto@6"floatpounds${

    00 con!erts pounds to @ilo6rams

    return.%4?3?E/ * pounds;

    }

    intmain"${ floatl's;

    cout --

  • 7/25/2019 OOP Complete Notes

    276/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 2::

    cout -- Four #ei6ht in @ilo6rams is -- l'sto@6"l's$

    -- endl;

    return.;

    }

    Recursion

    The e9istence of functions ma@es possi'le pro6rammin6 techniAue called recursion%

    Recursion in!ol!es a function callin6itself% This sounds rather impro'a'le, andindeed a function callin6 itself is often

    'u6% o#e!er, #hen used correctly this

    techniAue can 'e surprisin6ly po#erful% Recursion is much easier to understand #ithan e9ample than #ith len6thy e9planations,so letHs apply it to a pro6ram&

  • 7/25/2019 OOP Complete Notes

    277/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- 2:D"imensional $rrays7include-iostream8

    usin6namespacestd;

    intmain"${ constintro#+3, col+3; int i,;

    intaryQro#Qcol + {

    {((,(/,(3},

    {/(,//,/3},

    {3(,3/,33} };

    for"i+. ; i- ro# ; iBB${

    for"+. ; -col; BB${ cout -- aryQiQ -- ;}

    cout -- endl;

    }

    for"i+. ; i- ro# ; iBB${

    for"+. ; -col; BB${

    cout -- LaryQiQ -- +--aryQiQ--

  • 7/25/2019 OOP Complete Notes

    359/635

    }

    3JDimensional rrays

    .9//ff3

    .9//ff3

    .9//ff3

    .9//ff3

    .9//ff4

    .9//ff4

    aryQ.Q.+

    aryQ.Q(+

    aryQ.Q/+

    aryQ(Q.+

    aryQ(Q(+

    aryQ(Q/+

    .9//ff4

    .9//ff4

    .9//ff?

    aryQ/Q.+

    aryQ/Q(+

    666561

    565551

    161511

  • 7/25/2019 OOP Complete Notes

    360/635

    aryQ/Q/+

    alculating -uare o' a Matri0

    7include-iostream8usin6namespacestd;intmain"${ constintro#+3, col+3; int i,;

    intQro#Qcol; cout -- SAuare of a --ro#--9--col--Iatrices--endl;

    for"i+. ; i- ro# ; iBB${ for"+. ; -col; BB${ cout -- Q-- iB( -- Q-- B( -- + ; cin 88 QiQ; } } for"i+. ; i- ro# ; iBB$ for"+. ; -col; BB$ QiQ + QiQ*QiQ; for"i+. ; i- ro# ; iBB${ for"+. ; -col; BB$ cout -- QiQ --

  • 7/25/2019 OOP Complete Notes

    361/635

    return .;}

    Passing >" $rray to Function7include-iostream8usin6namespacestd;!oid6etXdata"floataQQ3,intro#, intcol${

    inti,; for"i+.; i-ro#; iBB$ for"+.; -col; BB${ cout -- Q--iB(--Q--B(--&; cin 88 aQiQ;}}!oidsho#Xdata"floataQQ3,intro#, intcol${ inti,; for"i+.; i-ro#; iBB${ for"+.; -col; BB$ {cout -- aQiQ --

  • 7/25/2019 OOP Complete Notes

    362/635

    return.;}

    Passing E" $rray to Function 625>*

    7include-iostream8

    usin6namespacestd;

    !oid6etXdata"floataQQ3Q/,intro#, intcol,intpa6e${

    inti,,@;

    for"@+.; @-pa6e; @BB${

    for"i+.; i-ro#; iBB${

    for"+.; -col; BB${

    cout --Q-- i --Q-- --Q-- @ --&;

    cin 88 aQiQQ@;

    } 00 end of for "+.

    } 00 end of for "i+.

    } 00 end of for "@+.

    }

  • 7/25/2019 OOP Complete Notes

    363/635

    }

    Passing E" $rray to Function 6>5>*

    !oidsho#Xdata"floataQQ3Q/,intro#, intcol, intpa6e${ inti,,@; for"@+.; @-pa6e; @BB${

    for"i+.; i-ro#; iBB${ for"+.; -col; BB${ cout -- aQiQQ@ --

  • 7/25/2019 OOP Complete Notes

    364/635

    }

    -orting "ata Using Cubble -ort $lgo7include-iostream8

    usin6namespacestd;

    !oidprint" floatQ, int$;

    !oidsort " floatQ, int$;

    intmain"${

    inti; floatdataQ(.;

    cout -- =nter (. Num'ers for Sortin6

  • 7/25/2019 OOP Complete Notes

    365/635

    -orting "ata Using Cubble -ort $lgo!oidsort" floataQ, intn ${ 00 'u''le sort&

    for"inti+(; i-n; iBB$ 00 'u''le up ma9{aQ.%%nJi}&

    for"int+.; -nJi; BB$

    if"aQ 8 aQB(${

    floattemp + aQ;

    aQ+aQB(;

    aQB( + temp;

    }

    }

    !oidprint" floataQ, intn ${

    cout -- Sorted Data is -- endl;

    for"inti+.; i-n; iBB$

    cout -- aQi -- ;

    }

  • 7/25/2019 OOP Complete Notes

    366/635

    Using Dstring

    7include-iostream8

    7include-iomanip%h8

    usin6namespacestd;

    intmain"${

    charstrQ + { >I>,>%>,> >,>>,>l>,>i>,.,> >,

    >5>,>5>,>1>,.}; 00 char ch Q + I% li;

    intsiOe + siOeof"str$;

    cout --

  • 7/25/2019 OOP Complete Notes

    367/635

    .eading ,mbedded Clan8s00 'lan@sin%cpp reads strin6 #ith em'edded 'lan@s

    7include-iostream8

    usin6namespacestd;

    intmain"${

    constintIY + U.; 00 ma9 characters in strin6

    charstrQIY; 00 strin6 !aria'le str

    cout --

  • 7/25/2019 OOP Complete Notes

    368/635

    return.;}

    opying a -tring the ard Way00 strcopy(%cpp

    00 copies a strin6 usin6 a for loop

    7include-iostream8

    7include-cstrin68 00for strlen"$

    usin6namespacestd;

    intmain"${ 00initialiOed strin6

    charstr(Q + Wh, Captain, my Captain:

    our fearful trip is done;

    constintIY + U.; int ; 00 IY is siOe of str/ 'uff

    charstr/QIY; 00empty strin6for" +.; -strlen"str($; BB$ 00copy strlen character

    str/Q + str(Q; 00 from str( to str/

    str/Q + >; 00insert N122 at end

    cout -- str/ -- endl; 00display str/

    system"P1S=$; return.;

    }

  • 7/25/2019 OOP Complete Notes

    369/635

    opying a -tring the ,asy Way

    00 strcopy/%cpp

    00 copies a strin6 usin6 strcpy"$ function

    7include-iostream87include-cstrin68 00for strcpy"$

    usin6namespacestd;

    intmain"${

    charstr(Q + Ti6er, ti6er, 'urnin6 'ri6ht

  • 7/25/2019 OOP Complete Notes

    370/635

    }

    $rrayof Strin6s00 straray%cpp

    00 array of strin6s

    7include-iostream8

    usin6namespacestd;

    intmain"${

    constintDFS + ; 00num'er of strin6s in array

    constintIY + (.; 00ma9imum siOe of each strin6

    00n array of strin6s

    charstarQDFSQIY + { Sunday, Ionday, Tuesday,

    Mednesday, Thursday, riday , Saturday };for" int+. ; -DFS ; BB$ 00display e!ery strin6

    cout -- starQ -- endl;

    system"P1S=$;

    return.;

    }

  • 7/25/2019 OOP Complete Notes

    371/635

    $rray o' -trings

  • 7/25/2019 OOP Complete Notes

    372/635

    Lab Ta(/

    rite a and te(t a program to ca,c),ate ?eterminant and ever(e o* a !! m

  • 7/25/2019 OOP Complete Notes

    373/635

    e*erence(

    Object Oriented Programming in CKK6 b- obert La*ore6 p)b,Sam( P)b,i(+ing 7T+e aite Aro)p8" 4t+ed" avai,ab,e in (o

    Object Oriented Programming U(ing CKK b- o-ce .arre,, 6 p)

    Co)r(e Tec+no,og-6 Cengage Learning" 4thed. available in soft form

    Nationa, Univer(it- o* Comp)ter Emerging Sc

    Q"n)"ed)"p/R Dirt)a, Univer(it- o* Pa/i(tan Qoc"v)"ed)"p/R

    Open Co)r(eare Con(orti)mQ+ttp##"occon(orti)m"org#en#co)r(e(R

  • 7/25/2019 OOP Complete Notes

    374/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    T+an/(

  • 7/25/2019 OOP Complete Notes

    375/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    CSC241:

    Object OrientedProgrammingSpring 21!

    1"Compo(ition2"&ggregation

    !".riend .)nction(

    Pl t OFF M bil Ph !

    1#2$#1%.ar+an &adi,

  • 7/25/2019 OOP Complete Notes

    376/635

    Please turn OFF your Mobile Phones!

    Pointer to Object(

    Pointer to object( are (imi,ar a( pointer to b)i,t5iT+e- can a,(o be )(ed to d-namica,,- a,,ocate o

  • 7/25/2019 OOP Complete Notes

    377/635

    Eamp,e

    class Student{

    pu'lic&

    Studen"$;

    Student"char * aName$;

    !oid setRollNo"int aNo$;

    };

  • 7/25/2019 OOP Complete Notes

    378/635

    Eamp,e 766re6ation$

    int main"${

    Student o';

    Student *ptr;

    ptr + Lo';

    ptrJ8setRollNo"(.$;return .;

    }

  • 7/25/2019 OOP Complete Notes

    379/635

    &,,ocation it+ ne Operator

    ne operator can be )(ed to create object( at r)

  • 7/25/2019 OOP Complete Notes

    380/635

    Eamp,e

    int main"${Student *ptr;

    ptr + ne# Student;

    ptrJ8setRollNo"(.$;

    return .;}

  • 7/25/2019 OOP Complete Notes

    381/635

    Eamp,e

    int main"${Student *ptr;

    ptr + ne# Student")li$;

    ptrJ8setRollNo"(.$;

    return .;}

  • 7/25/2019 OOP Complete Notes

    382/635

    Brea/)p o* ne Operation

    ne operator i( decompo(ed a( *o,,o( &,,ocating (pace in memor-

    Ca,,ing t+e appropriate con(tr)ctor

  • 7/25/2019 OOP Complete Notes

    383/635

    Ca(e St)d-

    ?e(ign a c,a(( date t+ro)g+ +ic+ )(er m)(t be aper*orm *o,,oing operation(

    Aet and (et c)rrent da-6 mont+ and -ear

    'ncrement b- n)mber o* da-(6 mont+( and -ear

    Set de*a),t date

  • 7/25/2019 OOP Complete Notes

    384/635

  • 7/25/2019 OOP Complete Notes

    385/635

    &ttrib)te(

    T+e de*a),t date i( a *eat)re (+ared b- a,, objectT+i( attrib)te m)(t be dec,ared a (tatic member

  • 7/25/2019 OOP Complete Notes

    386/635

    &ttrib)te( in ?ate"+

    class Date{

    int day;

    int month;

    int year;static Date defaultDate;

    };

  • 7/25/2019 OOP Complete Notes

    387/635

    'nter*ace(

    get?a-

    getMont+

    getYear

    (et?a-

    (etMont+

    (etYear

    add?a-

    addMont+

    addYear

    (et?e*a),t?ate

  • 7/25/2019 OOP Complete Notes

    388/635

    'nter*ace(

    &( t+e de*a),t date i( a (tatic member t+e inter*(et?e*a),t?ate (+o),d a,(o be dec,ared (tatic

  • 7/25/2019 OOP Complete Notes

    389/635

    'nter*ace( in ?ate"+

    class Date{

    pu'lic&

    !oid setDay"int aDay$;

    int 6etDay"$ const;

    !oid addDay"int 9$;

    };

  • 7/25/2019 OOP Complete Notes

    390/635

    'nter*ace( in ?ate"+

    class Date{

    pu'lic&

    static !oid setDefaultDate"

    int aDay,int aIonth, int aFear$;

    XF

  • 7/25/2019 OOP Complete Notes

    391/635

    Con(tr)ctor( and ?e(tr)ctor( in ?a

    Date"int aDay + .,int aIonth+ ., int aFear+ .$;

    Date"$; 00Destructor

    };

  • 7/25/2019 OOP Complete Notes

    392/635

    'mp,ementation o* ?ate C,a((

    T+e (tatic member variab,e( m)(t be initia,ied

    "ate "ate::de'ault"ate 63G

  • 7/25/2019 OOP Complete Notes

    393/635

    Con(tr)ctor(

    "ate::"ate6int a"ay< int aMonthXF

  • 7/25/2019 OOP Complete Notes

    517/635

    Member access inBase Class erive! Class

    u&lic rotected

    rotected rotected

    rivate Hidden

    Private 'n+eritancec,a(( C+i,d privateParent W>XF

  • 7/25/2019 OOP Complete Notes

    518/635

    Member access in

    Base Class erive! Class

    u&lic rivate

    rotected rivaterivate Hidden

    Private 'n+eritance

    '* t+e )(er doe( not (peci@e( t+e t-pe o* in+erita

  • 7/25/2019 OOP Complete Notes

    519/635

    '* t+e )(er doe( not (peci@e( t+e t-pe o* in+eritat+en t+e de*a),t t-pe i( private in+eritance

    c,a(( C+i,d privateParent W>X

    i( e9)iva,ent to

    c,a(( C+i,d Parent W>X

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- $22

    Private 'n+eritance

    e )(e private in+eritance +en e ant to re)

  • 7/25/2019 OOP Complete Notes

    520/635

    e )(e private in+eritance +en e ant to re)o* (ome c,a((

    Private 'n+eritance i( )(ed to mode, 'mp,ementterm( o* re,ation(+ip

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- $2!

    Eamp,e

    c,a(( Co,,ection W

  • 7/25/2019 OOP Complete Notes

    521/635

    c,a(( Co,,ection W

    """p)b,ic

    void &ddE,ement7int8F

    boo, Searc+E,ement7int8F

    boo, Searc+E,ement&gain7int8Fboo, ?e,eteE,ement7int8F

    XF

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- $24

    Eamp,e

    '* e,ement i( not *o)nd in t+e Co,,ection t+e *)nc

  • 7/25/2019 OOP Complete Notes

    522/635

    '* e,ement i( not *o)nd in t+e Co,,ection t+e *)ncSearc+E,ement i,, ret)rn *a,(e

    Searc+E,ement&gain @nd( t+e (econd in(tance oe,ement in t+e co,,ection

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- $2$

    C,a(( Set

    c,a(( Set private Co,,ection W

  • 7/25/2019 OOP Complete Notes

    523/635

    c,a(( Set private Co,,ection W

    private"""

    p)b,ic

    void &ddMember7int8F

    boo, '(Member7int8Fboo, ?e,eteMember7int8F

    XF

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- $2%

    C,a(( Set

    void Set&ddMember7int i8W

  • 7/25/2019 OOP Complete Notes

    524/635

    void Set&ddMember7int i8W

    i* 7I '(Member7i8 8&ddE,ement7i8F

    X

    boo, Set'(Member7int i8W

    ret)rn Searc+E,ement7i8FX

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- $2:

    e*erence(

    Object Oriented Programming in CKK6 b- obert La*ore6 p)b,

  • 7/25/2019 OOP Complete Notes

    525/635

    Sam( P)b,i(+ing 7T+e aite Aro)p8" 4t+ed" avai,ab,e in (o

    Object Oriented Programming U(ing CKK b- o-ce .arre,, 6 p)Co)r(e Tec+no,og-6 Cengage Learning" 4thed. available in soft form

    Nationa, Univer(it- o* Comp)ter EmergingScience( 6.&ST Q"n)"ed)"p/R

    Dirt)a, Univer(it- o* Pa/i(tan Qoc"v)"ed)"p/R

    Open Co)r(eare Con(orti)mQ+ttp##"occon(orti)m"org#en#co)r(e(R

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og- $2X

    i( e9)iva,ent to

    c,a(( C+i,d Parent W>X

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    Private 'n+eritance

    e )(e private in+eritance +en e ant to re)

  • 7/25/2019 OOP Complete Notes

    529/635

    po* (ome c,a((

    Private 'n+eritance i( )(ed to mode, 'mp,ementterm( o* re,ation(+ip

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    Eamp,e

    c,a(( Co,,ection W

  • 7/25/2019 OOP Complete Notes

    530/635

    """p)b,ic

    void &ddE,ement7int8F

    boo, Searc+E,ement7int8F

    boo, Searc+E,ement&gain7int8Fboo, ?e,eteE,ement7int8F

    XF

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    Eamp,e

    '* e,ement i( not *o)nd in t+e Co,,ection t+e *)nc

  • 7/25/2019 OOP Complete Notes

    531/635

    Searc+E,ement i,, ret)rn *a,(e

    Searc+E,ement&gain @nd( t+e (econd in(tance oe,ement in t+e co,,ection

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    C,a(( Set

    c,a(( Set private Co,,ection W

  • 7/25/2019 OOP Complete Notes

    532/635

    private

    """

    p)b,ic

    void &ddMember7int8F

    boo, '(Member7int8Fboo, ?e,eteMember7int8F

    XF

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    C,a(( Set

    void Set&ddMember7int i8W

  • 7/25/2019 OOP Complete Notes

    533/635

    i* 7I '(Member7i8 8

    &ddE,ement7i8F

    X

    boo, Set'(Member7int i8W

    ret)rn Searc+E,ement7i8FX

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    Specia,iation 7e(triction8

    t+e derived c,a(( i( be+avio)ra,,- incompatib,e b ,

  • 7/25/2019 OOP Complete Notes

    534/635

    ba(e c,a((

    Be+avio)ra,,- incompatib,e mean( t+at ba(e c,aa,a-( be rep,aced b- t+e derived c,a((

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    Specia,iation 7e(triction8

    Specia,iation 7e(triction8 can be imp,emented i t d t t d i + it

  • 7/25/2019 OOP Complete Notes

    535/635

    private and protected in+eritance

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    Eamp,e ; Specia,iation 7e(triction8

    Person

    age Q 12$R age a

  • 7/25/2019 OOP Complete Notes

    536/635

    age Q""12$R

    &d),t

    age Q1XF

  • 7/25/2019 OOP Complete Notes

    537/635

    Member access in

    Base Class erive! Class

    u&lic rivate

    rotected rivate

    rivate Hidden

    Eamp,ec,a(( Per(onW

    >

    protected

  • 7/25/2019 OOP Complete Notes

    538/635

    int ageF

    p)b,ic

    boo, Set&ge7int Vage8W

    i* 7Vage Z Vage 12$8 W

    age VageF

    ret)rn tr)eF

    X

    ret)rn *a,(eF

    X

    XF1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    Eamp,ec,a(( &d),t private Per(on Wp)b,ic

  • 7/25/2019 OOP Complete Notes

    539/635

    p)b,ic

    boo, Set&ge7int Vage8Wi* 7Vage Z1< Vage 12$8 W

    age VageFret)rn tr)eF

    Xret)rn *a,(eF

    XXF1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    &b(tract Ba(e C,a(( 'n t+e eamp,e( (o *ar6 in+eritance +a( been )(ed to add

    *)nctiona,it- to an ei(ting c,a((" No ,etH( ,oo/ at an eamp,e +erein+eritance i( )(ed *or a diJerent p)rpo(e a( part o* t+e origina,de(ign o* a program"

    O , d , d t b * , * id t

  • 7/25/2019 OOP Complete Notes

    540/635

    O)r eamp,e mode,( a databa(e o* emp,o-ee( o* a idget compan-"eHve (imp,i@ed t+e (it)ation (o t+at on,- t+ree /ind( o* emp,o-ee(are repre(ented" Manager( manage6 (cienti(t( per*orm re(earc+ todeve,op better idget(6 and ,aborer( operate t+e dangero)( idget5(tamping pre((e("

    T+e databa(e (tore( a name and an emp,o-ee identi@cation n)mber*or a,, emp,o-ee(6 no matter +at t+eir categor-" 0oever6 *ormanager(6 it a,(o (tore( t+eir tit,e( and go,* c,)b d)e(" .or (cienti(t(6it (tore( t+e n)mber o* (c+o,ar,- artic,e( t+e- +ave p)b,i(+ed"Laborer( need no additiona, data be-ond t+eir name( and n)mber("

    O)r eamp,e program (tart( it+ a ba(e c,a(( emp,o-ee" T+i( c,a((+and,e( t+e emp,o-eeH( ,a(t name and emp,o-ee n)mber" .rom t+i(c,a(( t+ree ot+er c,a((e( are derived manager6 (cienti(t6 and ,aborer"

    T+e manager and (cienti(t c,a((e( contain additiona, in*ormationabo)t t+e(e categorie( o* emp,o-ee6 and member *)nction( to+and,e t+i( in*ormation6 a( (+on in .ig)re

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

  • 7/25/2019 OOP Complete Notes

    541/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    Eerci(e 1

    'magine a p)b,i(+ing compan- t+at mar/et( bot+ booa)dioca((ette ver(ion( o* it( or/(" Create a c,a(( p)

  • 7/25/2019 OOP Complete Notes

    542/635

    a)dioca((ette ver(ion( o* it( or/(" Create a c,a(( p)

    t+at (tore( t+e tit,e 7a (tring8 and price 7t-pe oat8 o*p)b,ication" .rom t+i( c,a(( derive to c,a((e( boo/6add( a page co)nt 7t-pe int86 and tape6 +ic+ add( atime in min)te( 7t-pe oat8" Eac+ o* t+e(e t+ree c,a((+o),d +ave a getdata78 *)nction to get it( data *rom at t+e /e-board6 and a p)tdata78 *)nction to di(p,a- i

    rite a main78 program to te(t t+e boo/ and tape c,acreating in(tance( o* t+em6 a(/ing t+e )(er to @,, in dgetdata786 and t+en di(p,a-ing t+e data it+ p)tdata

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    Eerci(e 2

    Start it+ t+e p)b,ication6 boo/6 and tape c,a((e( Eerci(e 1 &dd a ba(e c,a(( (a,e( t+at +o,d( an ar

  • 7/25/2019 OOP Complete Notes

    543/635

    Eerci(e 1" &dd a ba(e c,a(( (a,e( t+at +o,d( an ar

    t+ree oat( (o t+at it can record t+e do,,ar (a,e( opartic),ar p)b,ication *or t+e ,a(t t+ree mont+(" 'ngetdata78 *)nction to get t+ree (a,e( amo)nt( *rom)(er6 and a p)tdata78 *)nction to di(p,a- t+e (a,e(&,ter t+e boo/ and tape c,a((e( (o t+e- are derivebot+ p)b,ication and (a,e(" &n object o* c,a(( boo/

    (+o),d inp)t and o)tp)t (a,e( data a,ong it+ it( odata" rite a main78 *)nction to create a boo/ objea tape object and eerci(e t+eir inp)t#o)tp)t capa

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    Eerci(e !

    &(()me t+at t+e p)b,i(+er in Eerci(e( 1 and ! dto add a t+ird a- to di(trib)te boo/( on comp)

  • 7/25/2019 OOP Complete Notes

    544/635

    to add a t+ird a- to di(trib)te boo/( on comp)

    di(/6 *or t+o(e +o ,i/e to do t+eir reading on t+e,aptop" &dd a di(/ c,a(( t+at6 ,i/e boo/ and tape6derived *rom p)b,ication" T+e di(/ c,a(( (+o),dincorporate t+e (ame member *)nction( a( t+e oc,a((e(" T+e data item )ni9)e to t+i( c,a(( i( t+et-pe eit+er C? or ?D?" Yo) can )(e an en)m t-(tore t+i( item" T+e )(er co),d (e,ect t+e appropt-pe b- t-ping c or d"

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    e*erence(

    Object Oriented Programming in CKK6 b- obert La*ore6 p)b,

    Sam( P)b,i(+ing 7T+e aite Aro)p8" 4t+ed" avai,ab,e in (o

  • 7/25/2019 OOP Complete Notes

    545/635

    g 7 p8

    Object Oriented Programming U(ing CKK b- o-ce .arre,, 6 p)Co)r(e Tec+no,og-6 Cengage Learning" 4thed. available in soft form

    Nationa, Univer(it- o* Comp)ter EmergingScience( 6.&ST Q"n)"ed)"p/R

    Dirt)a, Univer(it- o* Pa/i(tan Qoc"v)"ed)"p/R

    Open Co)r(eare Con(orti)mQ+ttp##"occon(orti)m"org#en#co)r(e(R

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    T+an/(

  • 7/25/2019 OOP Complete Notes

    546/635

    1#2$#1% COMS&TS 'ntit)te o* 'n*ormation Tec+no,og-

    CSC241:Object Oriented

  • 7/25/2019 OOP Complete Notes

    547/635

    ProgrammingSpring 21!

    1" Overriding Over,oading

    2" M),tip,e 'n+eritance

    !" Dirt)a, 'n+eritance

    4" Dirt)a, .)nction(#Member(

    Please turn OFF your Mobile Phones!

    1#2$#1%.ar+an &adi,

    Overriding Member .)nction( oBa(e C,a(( ?erived c,a(( can override t+e member

    *)nction( o* it( ba(e c,a((

  • 7/25/2019 OOP Complete Notes

    548/635

    To override a *)nction t+e derived c,a(((imp,- provide( a *)nction it+ t+e (ame(ignat)re a( t+at o* it( ba(e c,a((

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- $$!

    OverridingParent

    """

  • 7/25/2019 OOP Complete Notes

    549/635

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- $$4

    .)nc1

    hild"""

    .)nc1

    Overriding

    c,a(( Parent Wp)b,ic

  • 7/25/2019 OOP Complete Notes

    550/635

    p

    void.)nc178Fvoid.)nc17int8F

    XF

    c,a(( C+i,d p)b,ic Parent W

    p)b,icvoid .)nc178F

    XF

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- $$$

    Over,oading v(" Overriding

    Over,oading i( done it+in t+e (cope o* one c,a(

    Overriding i( done in (cope o* parent and c+i,d

  • 7/25/2019 OOP Complete Notes

    551/635

    Overriding i( done in (cope o* parent and c+i,d

    Overriding it+in t+e (cope o* (ing,e c,a(( i( erroto d)p,icate dec,aration

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- $$%

    Overriding

    c,a(( Parent W

    p)b,ic

  • 7/25/2019 OOP Complete Notes

    552/635

    p)b,ic

    void .)nc178F

    void .)nc178F ##Error

    XF

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- $$:

    Overriding Member .)nction( oBa(e C,a((

    ?erive c,a(( can override member *)nctioo* ba(e c,a(( ()c+ t+at t+e or/ing o*

  • 7/25/2019 OOP Complete Notes

    553/635

    o* ba(e c,a(( ()c+ t+at t+e or/ing o*

    *)nction i( tota,,- c+anged

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- $$>X ' need Yo)r anon-mo)( .eed Bac/

  • 7/25/2019 OOP Complete Notes

    567/635

    &bo)t t+e 'n(tr)ctor

    &bo)t t+eCo)r(e

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- $:2

    M),tip,e 'n+eritance

    & c,a(( can in+erit *rom more t+en one c,a((

  • 7/25/2019 OOP Complete Notes

    568/635

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- $:!

    M),tip,e 'n+eritance

    Tran(mitter

    """

    eceiver

    """

  • 7/25/2019 OOP Complete Notes

    569/635

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- $:4

    Tran(mit78 eceive78

    P+one

    Eamp,e

    c,a(( P+one p)b,ic Tran(mitter6 p)b,ic

  • 7/25/2019 OOP Complete Notes

    570/635

    W

    """

    XF

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- $:$

    M),tip,e 'n+eritance

    ?erived c,a(( can in+erit *rom p)b,ic ba(e c,a(( a( private and protected ba(e c,a((e(

  • 7/25/2019 OOP Complete Notes

    571/635

    c,a(( Mermaid

    private oman6 private .i(+

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- $:%

    M),tip,e 'n+eritance

    T+e derived c,a(( in+erit( data member( and *)n*orm a,, t+e ba(e c,a((e(

  • 7/25/2019 OOP Complete Notes

    572/635

    Object o* derived c,a(( can per*orm a,, t+e ta(/(object o* ba(e c,a(( can per*orm

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- $::

    Eamp,e

    int main78W

    P+one objF

  • 7/25/2019 OOP Complete Notes

    573/635

    obj"Tran(mit78F

    obj"eceive78F

    ret)rn F

    X

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- $:" S+o),d ' (tart t+e 'TP again333

  • 7/25/2019 OOP Complete Notes

    606/635

    1 concept 1 topic 1 program per c,a(( 555 Nice o/e F58

    )e(tion( d)ring ,ect)re(5O Noted

    Program(6 program(6 program( 6 >""and t+en coding

    +ere i( t+e T+eor-33333

    evi(ion C,a((e( 555 o/ S)re6 +en 333 Te,, me

    S+ortc)t( 5555 7T+ere i( no (+ortc)t in ,i*e BT 8

    &ttention to bac/benc+er( 555 0o can ' do t+at33

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- %11

    .eed Bac/ >>

    To)g+ time 6 a((ignment(6 project +- )(3333

    ?onHt get angr- +-3 55

    Bod- ,ang)age >"" e,, concentrate more on t+e co)r(e F58

    Entertain (t)dent( >"e,, eact,- +o3 can ) ep,ain F58

  • 7/25/2019 OOP Complete Notes

    607/635

    Practica, ,i*e6 (t)dent( doing ma(ti d)ring ,ect)re(>

    .a(t (peed>+-6 (eem( to b in +)rr- a,a-( 333

    .avio)ri(m 6 ) r not ne)tra,>

    Not a,, (t)dent( are programmer( mo(t o* t+em are norma, +)man be

    Not p)t -o)r (e,* in a (it)ation (o t+at ) +ave to>>"

    55555555555

    ?re(( code *or pre(entation>

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- %12

    Motivation .o,,oing *)nction print( an arra- o* integer e,em

    !oid printrray"int* array, int siOe$

  • 7/25/2019 OOP Complete Notes

    608/635

    {

    for " int i + .; i - siOe; iBB $

    cout -- arrayQ i -- ), ;

    }

    """Motivation +at i* e ant to print an arra- o* c+aracter(3

    !oid printrray"char* array,int siOe$

  • 7/25/2019 OOP Complete Notes

    609/635

    int siOe$

    {

    for " int i + .; i - siOe; iBB $

    cout -- arrayQ i -- ), ;

    }

    """Motivation +at i* e ant to print an arra- o* do)b,e(3

    !oid printrray"dou'le* array,int siOe$

  • 7/25/2019 OOP Complete Notes

    610/635

    int siOe$

    {

    for " int i + .; i - siOe; iBB $

    cout -- arrayQ i -- ), ;

    }

    """Motivation No i* e ant to c+ange t+e a- *)nction print

    arra-" e"g" *rom

    (, /, 3, 4, ?

  • 7/25/2019 OOP Complete Notes

    611/635

    , , , ,

    to

    1 5 2 5 ! 5 4 5 $

    """Motivation No con(ider t+errayc,a(( t+at rap( an arra

    integer(

    class rray {

  • 7/25/2019 OOP Complete Notes

    612/635

    int* prray;

    int siOe;

    pu'lic&

    };

    """Motivation +at i* e ant to )(e anrrayc,a(( t+at rap

    o* do)b,e3

    class rray {

  • 7/25/2019 OOP Complete Notes

    613/635

    dou'le* prray;

    int siOe;

    pu'lic&

    };

    """Motivation +at i* e ant to )(e anrrayc,a(( t+at rap(

    o* boo,ean variab,e(3

    class rray {

  • 7/25/2019 OOP Complete Notes

    614/635

    'ool* prray;

    int siOe;

    pu'lic&

    };

    """Motivation

    No i* e ant to add a *)nction ()m torrayce +ave to c+ange a,, t+e t+ree c,a((e(

  • 7/25/2019 OOP Complete Notes

    615/635

    Aeneric Programming Aeneric programming re*er( to program( contai

    generic ab(traction(

    & generic program ab(traction 7*)nction6 c,a((8 ct i d it+ t

  • 7/25/2019 OOP Complete Notes

    616/635

    parameteried it+ a t-pe

    S)c+ ab(traction( can or/ it+ man- diJerent

    data

  • 7/25/2019 OOP Complete Notes

    617/635

    &dvantage(

    e)(abi,it-

    ritabi,it-

  • 7/25/2019 OOP Complete Notes

    618/635

    -

    Maintainabi,it-

    Temp,ate( 'n CKK generic programming i( done )(ing temp

    To /ind( .)nction Temp,ate(

    C, T , t

  • 7/25/2019 OOP Complete Notes

    619/635

    C,a(( Temp,ate(

    Compi,er generate( diJerent t-pe5(peci@c copie

    (ing,e temp,ate

    .)nction Temp,ate(

    & *)nction temp,ate can be parameteried to opdiJerent t-pe( o* data

  • 7/25/2019 OOP Complete Notes

    620/635

    ?ec,arationtemplate- class T 8

    !oid funName" T 9 $;

    00 WR

    template- typename T 8

  • 7/25/2019 OOP Complete Notes

    621/635

    template- typename T 8

    !oid funName" T 9 $;

    00 WR

    template- class T, class 1, 8!oid funName" T 9, 1 y, $;

    Eamp,e ; .)nction Temp,ate( .o,,oing *)nction temp,ate print( an arra- +aving

    an- t-pe o* e,ement(

    template- typename T 8

    oid printrra " T* arra int si e $

  • 7/25/2019 OOP Complete Notes

    622/635

    !oid printrray" T* array, int siOe $

    {

    for " int i + .; i - siOe; iBB $

    cout -- arrayQ i -- ), ;}

    >Eamp,e ; .)nction Temp,ate(int main"$ {

    int irrayQ? + { (, /, 3, 4, ? };

    !oid printrray" irray, ? $;

    00 5nstantiated for intQ

  • 7/25/2019 OOP Complete Notes

    623/635

    char crrayQ3 + { \aH, \'H, \cH };

    !oid printrray" crray, 3 $;

    00 5nstantiated for charQ

    return .;}

    Ep,icit T-pe Parameteriation & *)nction temp,ate ma- not +ave an- paramete

    template -typename T8T 6et5nput"$ {

  • 7/25/2019 OOP Complete Notes

    624/635

    6 p "$ {

    T 9;

    cin 88 9;

    return 9;}

    >Ep,icit T-pe Parameteriation

    int main"$ {

    int 9;9 + 6et5nput"$; 00 =rror:

  • 7/25/2019 OOP Complete Notes

    625/635

    6 p

    dou'le y;

    y + 6et5nput"$; 00 =rror:}

    >Ep,icit T-pe Parameteriation

    int main"$ {

    int 9;9 + 6et5nput- int 8"$;

  • 7/25/2019 OOP Complete Notes

    626/635

    dou'le y;

    y + 6et5nput- dou'le 8"$;}

    U(er5de@ned Specia,iation(

    & temp,ate ma- not +and,e a,, t+e t-pe( ()cce((

    Ep,icit (pecia,iation( need to be provided *or (t-pe7(8

  • 7/25/2019 OOP Complete Notes

    627/635

    t-pe7(8

    Eamp,e ; U(er Specia,iation(

    template- typename T 8

    'ool is=Aual" T 9, T y $ {return " 9 ++ y $;

  • 7/25/2019 OOP Complete Notes

    628/635

    }

    > Eamp,e ; U(er Specia,iation(

    int main {

    is=Aual" ?, $; 00 W]is=Aual" %?, %? $; 00 W]

  • 7/25/2019 OOP Complete Notes

    629/635

    is=Aual" )a'c, )9yO $;

    00 2o6ical =rror:

    return .;}

    > Eamp,e ; U(er Specia,iation(

    template- 8

    'ool is=Aual- const char* 8"const char* 9, const char* y $ {

  • 7/25/2019 OOP Complete Notes

    630/635

    return " strcmp" 9, y $ ++ . $;

    }

    > Eamp,e ; U(er Specia,iation(int main {

    is=Aual" ?, $;

    00 Tar6et& 6eneral template

    is=Aual" %?, %? $;00 Tar6et& 6eneral template

  • 7/25/2019 OOP Complete Notes

    631/635

    is=Aual" )a'c, )9yO $;

    00 Tar6et& user specialiOation

    return .;}

    e*erence( Object Oriented Programming in CKK6 b- obert La*ore6 p)b,

    Sam( P)b,i(+ing 7T+e aite Aro)p8" 4t+ed" avai,ab,e in (o Object Oriented Programming U(ing CKK b- o-ce .arre,, 6 p)

    Co)r(e Tec+no,og-6 Cengage Learning"

    4thed. available in soft form

    Nationa, Univer(it- o* Comp)ter EmergingS i .&ST Q d /R

  • 7/25/2019 OOP Complete Notes

    632/635

    Science( 6.&ST Q"n)"ed)"p/R

    Dirt)a, Univer(it- o* Pa/i(tan Qoc"v)"ed)"p/R

    Open Co)r(eare Con(orti)mQ+ttp##"occon(orti)m"org#en#co)r(e(R

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- %!:

    T+an/(

  • 7/25/2019 OOP Complete Notes

    633/635

    1#2$#1% COMS&TS 'n(tit)te o* 'n*ormation Tec+no,og- %!