Object-Orientedg Programmingg (CSC221)
Introduction
Whatg isg OOP
• Ing simpleg term,g OOPg isg programmingg withg objects.
• Objects
o Objectsg areg theg evolutionaryg extensiong ofg theg conceptg ofg recordsg thatge
nablegusgtogorganizegdatagintogpackages.
o Itg allowg usg tog combineg bothg datag andg codeg intog ag singleg package.
o Therefore,g ang objectg isg ag languageg constructg thatg tiesg datag withg theg functionsg
thatgoperatesgongthegdata.
OOPg viewpoint
Lowglevelg object
Windowg object FilegManagement
gobject
Wordgprocessing Editinggobject
gApplication
Keyboardgcontrol
gobject
Theg figureg aboveg showg howg tog createg ag setg ofg objectsg forg ag wordg processorg application.
• Theg arrangementg isg dividedg intog objectsg suchg asg lowg levelg object,g windowg
object,gfilegmanagementgobjectsgetc.
Advantages
- Majorg operationg ofg ag programg cang easilyg beg isolated.
- Someg objectsg areg usedg tog createg otherg objects.
- Thisg helpsg tog hideg theg detailsg ofg theg lowerg levelg objects.
Traditional programming vs OOP
g g g
- Ing traditionalg view,g functionsg areg theg mostg important,g i.e.g allg theg codeg ing ag programg i
sgdesignedgaroundgthegfunctions.
- Ing theg OOPg view,g objectsg areg theg mostg important,g programsg areg designedg aroundg objects,
gfunctionsgaregsecondary
7
,- Thisgrolegrevealsgingthegwaygobjectsgaregused,grathergthangpassinggobjects(data)gtogfun
ctions,gobjectsgaregusedgtogcallgfunctions.
- Programsgcangavoidglargegfunctionsgthatgcontainglogicgforgmultiplegcases,ginsteadgmulti
plegobjectsgaregcreatedgtogrepresentgthegdifferentglogicalgcomponentsgofgagprogram.
Componentsg ofg OOP
• Classes
• Objects
• Instanceg variables
• Methods
Classes and objects
g g
- Ag classg isg ang abstractg definitiong ofg ang object
o Itg definesg theg structureg andg behaviourg ofg eachg objectg ing theg class.
o Itg servesg asg ag templateg forg creatingg objects
- objectsg mayg beg groupedg intog classes
o Ag particularg objectg ofg ag classg isg ang instance.
- Theg figureg belowg illustrate:
Classg =g typegdefinitiongObj
ectg=gvariablegofgagclass.
1.exe
- Ag classg containg twog typesg ofg components
o Instanceg variables
o g Methods
1.exe
- Instanceg variablesg defineg theg internalg datag stateg ofg ang object
- Methodsg defineg ang object‟sg behavior,g thatg isg theg actionsg thatg theg objectg can
perform.
Sampleg class
• Class
Properties Course Behaviour
Name Addg ag student
Location Deleteg ag student
Daysg offered Getg courseg roster
Creditg hours ….
Startg time e.t.c
8
,Endg time
Theg threeg faces(properties)g ofg OOP
WhengprogramminggwithgC++g„sgobjectgorientedg features,g thereg areg threeg underlyinggp
ropertiesgthatgsurfacegagaingandgagain:
• Encapsulation
• Polymorphism
• Inheritance
Encapsulation
- Encapsulationg isg thegtechniqueg ofg combiningg datag andg theg operationsg neededg tog
processgthegdatagundergonegpackage[object].
- Itg isg whatg givesg objectsg theirg buildingg blockg flavor.
- Encapsulationg providesg twog importantg features:
o Putsg datag andg functionsg underg oneg roof.
o Providesg datag hidingg capabilities.
Polymorphism
- Polymorphismgisg theg qualitygthatgallowsg oneg nameg togbeg usedg forgtwogorgmoreg relatedg butg
technicallygdifferentgpurposes.
- Polymorphismg allowsg oneg nameg tog specifyg ag generalg classg ofg actions.g Withing ag general
gclassg ofg actions,g theg specificg actiongtog beg appliedg isgdeterminedg byg theg typeg ofg data.
Forgexample,gingC,gthegabsolutegvaluegactiongrequiresgthreegdistinctgfunctiongnames:gabs(g)gforginteger,glabs(g)gf
orglongginteger,gandgfabs(g)g forgfloating-
pointgvalue.gHowevergingC++,geachgfunctiongcangbegcalledgbygthegsamegname,gsuchgasgabs(g).gThegtypegofgdatag
usedgtogcallgthegfunctiongdeterminesgwhichgspecificgversiongofgthegfunctiongisgactuallygexecuted.
-
Ing C++g itg isg possibleg tog useg oneg functiong nameg forg manyg differentg purposes.g Thisg typ
eg ofgpolymorphismgisgcalledgfunctiongoverloading.
- Polymorphismgcangalsogbegappliedgtogoperators.gIngthatgcasegitgisgcalledgoperatorgoverloading.
- Theg abilityg tog hideg manyg differentg implementationsg behindg ag singleg interface.
Whatg isg ang interface?
- Ag namedg setg ofg operationsg thatg characterizeg theg behaviourg ofg ang element.
• Theg interfaceg formalizesg polymorphism
<<interface>>
Triangle
gPolygon
, 9
draw
Square
Introduction
Whatg isg OOP
• Ing simpleg term,g OOPg isg programmingg withg objects.
• Objects
o Objectsg areg theg evolutionaryg extensiong ofg theg conceptg ofg recordsg thatge
nablegusgtogorganizegdatagintogpackages.
o Itg allowg usg tog combineg bothg datag andg codeg intog ag singleg package.
o Therefore,g ang objectg isg ag languageg constructg thatg tiesg datag withg theg functionsg
thatgoperatesgongthegdata.
OOPg viewpoint
Lowglevelg object
Windowg object FilegManagement
gobject
Wordgprocessing Editinggobject
gApplication
Keyboardgcontrol
gobject
Theg figureg aboveg showg howg tog createg ag setg ofg objectsg forg ag wordg processorg application.
• Theg arrangementg isg dividedg intog objectsg suchg asg lowg levelg object,g windowg
object,gfilegmanagementgobjectsgetc.
Advantages
- Majorg operationg ofg ag programg cang easilyg beg isolated.
- Someg objectsg areg usedg tog createg otherg objects.
- Thisg helpsg tog hideg theg detailsg ofg theg lowerg levelg objects.
Traditional programming vs OOP
g g g
- Ing traditionalg view,g functionsg areg theg mostg important,g i.e.g allg theg codeg ing ag programg i
sgdesignedgaroundgthegfunctions.
- Ing theg OOPg view,g objectsg areg theg mostg important,g programsg areg designedg aroundg objects,
gfunctionsgaregsecondary
7
,- Thisgrolegrevealsgingthegwaygobjectsgaregused,grathergthangpassinggobjects(data)gtogfun
ctions,gobjectsgaregusedgtogcallgfunctions.
- Programsgcangavoidglargegfunctionsgthatgcontainglogicgforgmultiplegcases,ginsteadgmulti
plegobjectsgaregcreatedgtogrepresentgthegdifferentglogicalgcomponentsgofgagprogram.
Componentsg ofg OOP
• Classes
• Objects
• Instanceg variables
• Methods
Classes and objects
g g
- Ag classg isg ang abstractg definitiong ofg ang object
o Itg definesg theg structureg andg behaviourg ofg eachg objectg ing theg class.
o Itg servesg asg ag templateg forg creatingg objects
- objectsg mayg beg groupedg intog classes
o Ag particularg objectg ofg ag classg isg ang instance.
- Theg figureg belowg illustrate:
Classg =g typegdefinitiongObj
ectg=gvariablegofgagclass.
1.exe
- Ag classg containg twog typesg ofg components
o Instanceg variables
o g Methods
1.exe
- Instanceg variablesg defineg theg internalg datag stateg ofg ang object
- Methodsg defineg ang object‟sg behavior,g thatg isg theg actionsg thatg theg objectg can
perform.
Sampleg class
• Class
Properties Course Behaviour
Name Addg ag student
Location Deleteg ag student
Daysg offered Getg courseg roster
Creditg hours ….
Startg time e.t.c
8
,Endg time
Theg threeg faces(properties)g ofg OOP
WhengprogramminggwithgC++g„sgobjectgorientedg features,g thereg areg threeg underlyinggp
ropertiesgthatgsurfacegagaingandgagain:
• Encapsulation
• Polymorphism
• Inheritance
Encapsulation
- Encapsulationg isg thegtechniqueg ofg combiningg datag andg theg operationsg neededg tog
processgthegdatagundergonegpackage[object].
- Itg isg whatg givesg objectsg theirg buildingg blockg flavor.
- Encapsulationg providesg twog importantg features:
o Putsg datag andg functionsg underg oneg roof.
o Providesg datag hidingg capabilities.
Polymorphism
- Polymorphismgisg theg qualitygthatgallowsg oneg nameg togbeg usedg forgtwogorgmoreg relatedg butg
technicallygdifferentgpurposes.
- Polymorphismg allowsg oneg nameg tog specifyg ag generalg classg ofg actions.g Withing ag general
gclassg ofg actions,g theg specificg actiongtog beg appliedg isgdeterminedg byg theg typeg ofg data.
Forgexample,gingC,gthegabsolutegvaluegactiongrequiresgthreegdistinctgfunctiongnames:gabs(g)gforginteger,glabs(g)gf
orglongginteger,gandgfabs(g)g forgfloating-
pointgvalue.gHowevergingC++,geachgfunctiongcangbegcalledgbygthegsamegname,gsuchgasgabs(g).gThegtypegofgdatag
usedgtogcallgthegfunctiongdeterminesgwhichgspecificgversiongofgthegfunctiongisgactuallygexecuted.
-
Ing C++g itg isg possibleg tog useg oneg functiong nameg forg manyg differentg purposes.g Thisg typ
eg ofgpolymorphismgisgcalledgfunctiongoverloading.
- Polymorphismgcangalsogbegappliedgtogoperators.gIngthatgcasegitgisgcalledgoperatorgoverloading.
- Theg abilityg tog hideg manyg differentg implementationsg behindg ag singleg interface.
Whatg isg ang interface?
- Ag namedg setg ofg operationsg thatg characterizeg theg behaviourg ofg ang element.
• Theg interfaceg formalizesg polymorphism
<<interface>>
Triangle
gPolygon
, 9
draw
Square