0% found this document useful (0 votes)
6 views

Oop Project 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Oop Project 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Whati
sOOP,
andwhyi
siti
mpor
tanti
nPy
thon?

OOPisapr ogrammingpatt
ernthati
sbuil
tar
oundobj
ect
sanditi
sused tost
ruct
ureasof
twar
e
pr
ogram i
ntosi mpl
e,r
eusabl
ecodeblocks(usual
l
ycl
asses)
,whi
chyouthenusetocr
eate
i
ndiv
idual
instancesoftheobj
ects.

I
mpor
tanceofoopi
npy
thon


Reusabi
l
ity
:Oncey
oudef
ineacl
ass,
youcancr
eat
emul
ti
pleobj
ect
sfr
om i
t.


Inher
it
ance:
Classescani
nher
itpr
oper
ti
esandbehav
ior
sfr
om ot
hercl
asses.


Simpli
fiesCodeMaint
enance:thecodeinobj
ect
-or
ient
edsof
twar
eiseasyt
ouseand
maint
ain.Thesof
twaresi
mplif
iesthecodemaint
enancepr
ocessbypr
omoti
ngmodul
arand
r
eusabledesi
gns.

2.
whati
sacl
assi
npy
thon?

Classesareacol l
ecti
onoffuncti
onsandatt
ri
but
esthatareatt
achedtoaspeci
fi
cnamewhi
ch
representuser
-defi
neddatatypesobt
ainedf
rom t
heexist
ingdataty
pe.

3.Whati
san‘
Obj
ect
’inPy
thon?

Anobj ecti
saspecif
icinst
anceofaclasst
hathasmet
hods(
funct
ions)andat
tri
but
es(
dat
a)
thatspecif
yit
spropert
iesandact
ions.

4.
Howdoy
oucr
eat
eani
nst
anceofacl
assi
nPy
thon?

I
nPython,youcr
eat
eaninst
anceofacl
assbycal
l
ingt
hecl
assasi
fitwer
eaf
unct
ion.Thi
s
pr
ocessisknownasinst
ant
iat
ion.

Tocr
eateinstancesofaclass,
youcall
thecl
assusi
ngcl
assnameandpassi
nwhat
ever
ar
gumentsits__ini
t__met
hodaccepts.

5.
Whatar
ethemai
nfeat
uresofOOPs?

Abst
ract
ion:
hidescompl
exi
mpl
ement
ati
ondet
ail
sbehi
ndasi
mpl
eint
erf
ace.

Pol
ymor
phi
sm:
i
saf
eat
uret
hatal
l
owsonei
nter
facet
obeusedf
oragener
alcl
assofact
ions.

Inher
it
ance:
isafeatur
ethatal
l
owsonecl
ass(
thechi
l
dcl
ass)t
oinher
itt
hepr
oper
ti
esand
methodsoft
heparentcl
ass.
Encapsul
ati
on:i
sthebundl
i
ngofdat
a,andt
hemet
hodst
hatoper
ateont
hatdat
a,i
ntoasi
ngl
e

uni
tcal
l
edanobj
ect
.

6.
Whati
sthedi
ff
erencebet
weenacl
assandanobj
ect
?

Aclassi satemplatethatdefi
nest hepropert
iesandbehav i
orsofobjectswhi chisusedto
definethestr
uctureofobjectsandser v
easbl uepri
ntsforcreat
ingmultipl
ei nst
ancesofobj ect
s
whileanobjectisaninstanceofacl asswithspecifi
cvaluesforit
sattr
ibutes(objectsare
createdfrom cl
assesandr epresenti
ndivi
dual ent
it
iesi
napr ogram)andi tisusedt oint
eract
withattri
but
esandmet hods.

7.
Whati
smet
hodov
erl
oadi
ng?

Met
hodOverl
oadi
ngi
safundament
alconceptinOOPthatenabl
esacl
asst
odef
inemul
ti
ple
met
hodswit
hthesamenamebutdi
ffer
entparameter
s.

8.
Whati
smet
hodov
err
idi
ng?

Methodoverr
idi
ngoccur
swhenasubcl
asspr
ovi
desaspeci
fi
cimpl
ement
ati
onf
oramet
hod
al
readydef
inedini
tspar
entcl
ass.

9.
Wri
teapr
ogr
am t
hatshowsmet
hodov
erl
oadi
ngandov
err
idi
ng.

Ov
erl
oadi
ngexampl
e

cl
asscal
cul
ator
:

defadd(
sel
f,
a,
b,
c=0)
:

r
etur
na+b+c

cal
c=cal
cul
ator
()

pr
int
(cal
c.add(
4,5)
)

pr
int
(cal
c.add(
6,7,
8))

Out
put
=9

21
Ov
err
idi
ngexampl
e

cl
assshape:

defdescr
ipt
ion(
sel
f)
:

r
etur
n't
hisi
sashape'

cl
asssquar
e(shape)
:

defdescr
ipt
ion(
sel
f)
:

r
etur
n't
hisi
sasquar
e'

shape=shape(
)

squar
e=squar
e()

pr
int
(shape.
descr
ipt
ion(
))

pr
int
(squar
e.descr
ipt
ion(
))

Out
put
=thi
sisashape

t
hisi
sasquar
e
10.Descr
ibeever
yli
neoft
hecodedownbel
owwithasmuchdetai
lasyoucan.
..
.You
needtowat chvi
deoandr
eadt
heabovegi
vennot
etoanswert
hisquest
ion.

cl
assPar
rot
:

#cl
assat
tri
but
e

name="
"

age=0

#cr
eat
epar
rot
1obj
ect

par
rot
1=Par
rot
()

par
rot
1.name="
Blu"

par
rot
1.age=10

#cr
eat
eanot
herobj
ectpar
rot
2

par
rot
2=Par
rot
()

par
rot
2.name="
Woo"

par
rot
2.age=15

#accessat
tri
but
es

pr
int
(f"
{par
rot
1.name}i
s{par
rot
1.age}y
ear
sol
d")

pr
int
(f"
{par
rot
2.name}i
s{par
rot
2.age}y
ear
sol
d")
Li
ne1-
her
ewei
nit
iat
eacl
asscal
l
edPar
rot
.

Li
ne2-
Thi
sisacl
assat
tri
but
enamewi
thani
nit
ial
val
ueofanempt
yst
ri
ng

Li
ne3-
anat
tri
but
eagei
sdef
inedwi
thani
nit
ial
val
ueof0.

Li
ne4-Ani
nst
anceoft
hePar
rotcl
assi
scr
eat
edandassi
gnedt
othev
ari
abl
epar
rot
1.

Li
ne5-
Thenameat
tri
but
eofpar
rot
1issett
o"Bl
u".

Li
ne6-
Set
stheageat
tr
ibut
eofpar
rot
1obj
ectt
o10

Li
ne7-
Thi
sli
necr
eat
esanot
heri
nst
anceoft
hePar
rotcl
assnamedpar
rot
2.

Li
ne8-
Set
sthenameat
tr
ibut
eoft
hepar
rot
2obj
ectt
o"Woo"
.

Li
ne9-
Set
stheageat
tr
ibut
eoft
hepar
rot
2obj
ectt
o15

Li
ne10-Pr
int
sthenameandageofpar
rot
1obj
ectusi
nganf
-st
ri
ng.

Li
ne11-Pr
int
sthenameandageoft
hepar
rot
2obj
ectusi
nganf
-st
ri
ng.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy