Object Oriented Programming - A Mathematical World - Handouts | MATH 181, Exams of Mathematics

Material Type: Exam; Professor: Champion; Class: A Mathematical World; Subject: Mathematics; University: University of Illinois - Urbana-Champaign; Term: Summer 2018;

Typology: Exams

2017/2018

Uploaded on 09/06/2018

aqsa-qayyum9801
aqsa-qayyum9801 🇺🇸

1 document

1 / 355

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object Oriented Programming (CS304) VU
© Virtual University of Pakistan 1
CS304-Handouts
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Object Oriented Programming - A Mathematical World - Handouts | MATH 181 and more Exams Mathematics in PDF only on Docsity!

CS304-Handouts

  • LECTURE NO.01 8 LECTURE NO.02 15 LECTURE NO.03 21 LECTURE NO.04 29 01.1. 01.2. 01.3. 01.4. 01.5. 01.6. 01.7. 02.1. 02.2. 02.3. 02.4. 02.5. 02.6. 02.7. 03.1. 03.2. 03.3. 04.1. 04.2. 04.3. 04.4. 04.5. 04.6. 04.7. IWOOOWTSIEIISMSACICGSSOACNTRODUCTIONNFORMATIONNTERFACEMPLEMENTATIONNHERITANCE ANGIBLE AND UMMARY NCAPSULATION EPARATION OF UMMARY UB PECIALIZATIONBSTRACTIONLASSESONCEPTSBSTRACTONCRETEBJECTENERALIZATIONVERRIDINGHAT IS AHAT IS ANESSAGES - M TYPINGODELS-O 23 RIENTATION : 19 M RCC............................................................................................................................ 14............................................................................................................................ 17............................................................................................................................ 19 OELATED WITH(ELASSES :LASSES ODEL 11 34....................................................................................................................... 25 BJECT 21XTENSIONH I...................................................................................................................... 8 NTANGIBLEI IDING 16NTERFACE(R 29?............................................................................................................... 18 ESTRICTION 10 36? 38 12 - : 15A) DVANTAGES 31I NHERITANCE& O I BJECTSMPLEMENTATION)
  • LECTURE NO.05 40 LECTURE NO.06 55 LECTURE NO.07 65 LECTURE NO.08 72 05.3. 05.4. 05.5. 06.1. 06.2. 06.3. 06.4. 06.5. 07.1. 07.2. 07.3. 07.4. 07.5. 07.6. 07.7. SCACPPPOCTADOAA IMPLE OLYMORPHISM OLYMORPHISM IN OLYMORPHISM YPE INOMPOSITIONGGREGATIONLASSLASSBSTRACTIONCCESSING MEMBERSCCESS SPECIFIERSBJECTEFINING ABJECT AND C.................................................................................................................................... 65 -O AC++OMPATIBILITY SSOCIATION RIENTED N 65C EW...................................................................................................................... 52...................................................................................................................... 66LASS 54 56 – U A OOM :.............................................................................................................. 69 SERDVANTAGES 68ODELING AN 50......................................................................................................... 68........................................................................................................ 55 DM EFINED ODEL 56 57T E YPE XAMPLE
  • LECTURE NO.09.................................................................................................................................... 84 LECTURE NO.10.................................................................................................................................... 99 LECTURE NO.11.................................................................................................................................. 105 08.1. 08.2. 08.3. 08.4. 08.5. 08.6. 08.7. 08.8. 08.9. 08.10. 08.11. 09.1. 09.2. 09.3. 09.4. 09.5. 09.6. 10.1. 10.2. 10.3. 10.4. 10.5. 11.1. 11.2. 11.3. MDICCDCCCSDIDATHISUSCCONSTTHISUDMNLINEMPORTANT POINTS ABOUT COPY CONSTRUCTOR HALLOW EPARATION OF INTERFACE AND IMPLEMENTATIONONSTRUCTORONSTRUCTORONSTRUCTORONSTRUCTOROPYOMPLEX EFINING EFAULT EEP ESTRUCTOR CCESSOR SES OF THIS SAGE EXAMPLE OF IFFERENCE BETWEENEMBEREMBERSD HALLOW EEPPP CC OINTER OINTER AND CONST MFOPYONSTRUCTOR CUNCTIONS F EMBERIC OPYNCNITIALIZERUNCTIONSM FONSTRUCTOR 90OPY UMBER UNCTIONSEMBER C P 94 82....................................................................................................................... 96OPYOINTER 75POO 85 F ROPERTIES VERLOADING VERLOADINGUNCTIONS 81............................................................................................................. 100 73FC 72UNCTIONS........................................................................................................... 79 ONSTANT MEMBER FUNCTIONS LI 95........................................................................................................ 99 NITIALIZATION AND IST 76 M............................................................................................... 106 75 102EMBER 77......................................................................................... 78 72 F UNCTION A SSIGNMENT 104 : 94....................................................... 99 105:
  • LECTURE NO.12.................................................................................................................................. 112 LECTURE NO.13.................................................................................................................................. 118 LECTURE NO.14.................................................................................................................................. 124 LECTURE NO.15.................................................................................................................................. 132 LECTURE NO.16.................................................................................................................................. 142 11.4. 11.5. 12.1. 12.2. 12.3. 12.4. 12.5. 12.6. 13.1. 13.2. 13.3. 13.4. 14.1. 15.1. 15.2. CONSTSALSTHISGAPBCCCAF TATIC IFE OF TATIC OINTER TORIEND REAKUP OF NEWASEOMPLETE CODE OFOMPOSITION CCESSING LOBAL RRAY OF GGREGATION P S OINTER AND STATIC MEMBER FUNCTIONS O TUDY VSMF V BJECTS TATIC UNCTIONSARIABLESEMBER ARIABLE VSO S BJECTSO 119 TATIC BJECTS 124 D................................................................................................................... 135 107 FATA OUNCTION 109............................................................................................................ 116 PERATION DD........................................................................................................... 138 118 ATAM ATE CLASS SEMBER TATIC M 115EMBER 119 M....................................................................................... 114 121 EMBERS
  • LECTURE NO.17 149 LECTURE NO.18 155 LECTURE NO.19 158 LECTURE NO.20 163 LECTURE NO.21 168 LECTURE NO.22 175 16.1. 17.1. 18.1. 18.2. 18.3. 19.1. 19.2. 19.3. 19.4. 19.5. 20.1. 20.2. 20.3. 20.4. 20.5. 21.1. 21.2. 21.3. 21.4. 21.5. OOSOFSSOOOSOOFUBPTUDRIENDUNCTION ELF ASSIGNMENT PROBLEM TREAM TREAM UBSCRIPT EHAVIOR OF OST YPENARYSERPERATOR OVERLOADINGVERLOADINGTHERVERLOADINGVERLOADINGTHERVERLOADINGVERLOADINGRAWBACKS OF - C DINCREMENT OPERATOR ONVERSION BBO EFINED TYPESF IE INARY OPERATORS INARY OPERATORSUNCTIONS ANDNSERTION OPERATOR PERATORS XTRACTION OPERATOR O[] PERATOR PERFORMINGO ++ PERATOR ASSSF TUNCTION TREAM TREAM UBSCRIPTSSIGNMENT OPERATORAND YPE 170........................................................................................................... 166: C--....................................................................................................... 173 ONVERSION FOR PREIE................................................................................................... 164 ONSERTION XTRACTION 142() PERATOR OVERLOADING[] :............................................................................................. 156: 161 OPERATOR........................................................................................... 155O : 169 PERATOR....................................................................................... 158 - 158DEFINED TYPES OSO UB PERATOROPERATOR 165PERATOR 151....................................................................... 164S TRING OPERATION 159 : 174: 168: 160 157,
  • LECTURE NO.23 183 LECTURE NO.24 191 LECTURE NO.25 207 LECTURE NO.26 213 22.1. 22.2. 22.3. 22.4. 22.5. 23.1. 23.2. 23.3. 23.4. 24.1. 25.1. 25.2. 26.1. 26.2. 26.3. PIUMLI“ISA““ISSMOHBTPNHERITANCE INNHERITANCE IN PROTECTED RACTICAL IMPLEMENTATION OF TATIC ASE YPES OF RIVATECCESSING BASE CLASS MEMBER FUNCTIONS IN DERIVED CLASSVERLOADING VSIERARCHY OFODIFIED A”A” INNITIALIZATION RR OTATIONT ELATIONSHIPELATIONSHIP I YPEINHERITANCE NHERITANCED” EFAULT....................................................................................................................... 189 ACCESS SPECIFIER INHERITANCE CC++................................................................................................................ 175 .LASSES O C VERRIDING......................................................................................................... 213 175....................................................................................................... 176....................................................................................................... 186ONSTRUCTOR 216.................................................................................................... 214 175 211: 185 207INHERITANCE IN C 199 ++.............................................. 175 :
  • LECTURE NO.27.................................................................................................................................. 218 LECTURE NO.28.................................................................................................................................. 225 LECTURE NO.29.................................................................................................................................. 233 LECTURE NO.30.................................................................................................................................. 243 LECTURE NO.31.................................................................................................................................. 250 27.1. 27.2. 27.3. 28.1. 28.2. 28.3. 28.4. 29.1. 29.2. 29.3. 29.4. 29.5. 29.6. 29.7. 29.8. 29.9. 30.1. 30.2. 31.1. 31.2. 31.3. SPPVVSSACAPSVVVDPSMPV PECIALIZATION ROTECTED ROPERTIES OF HAPE TATIC VS URE HAPE OLYMORPHISM HAPE ROBLEMS INONCRETE IRTUAL IRTUAL BSTRACT BSTRACT IRTUAL IRTUAL YNAMIC IRTUALTULTIPLE ABLE V HHHIRTUAL IERARCHY IERARCHY............................................................................................................................... 240 IERARCHY DIFFF D NHERITANCE IUNCTIONSUNCTIONSD UNCTIONSCCC ESTRUCTORSNHERITANCE I ISPATCH YNAMICLASSESLASSES INLASSESNHERITANCE M FP ULTIPLEUNCTIONS – ROTECTED(R C ESTRICTIONR............................................................................................................ 230............................................................................................................ 235 233........................................................................................................... 234 ASE : EVISITED......................................................................................................... 225B(D– 230 INDING C++U 256 YNAMIC S 236................................................................................................... 250I SAGENHERITANCE TUDY 222 234.............................................................................................. 234INHERITANCE: 238 231:........................................................................................ 246 ) A..................................................................................... 218B INDINGS IMPLE 251 223 ) P 242 AYROLL A PPLICATION...........................
  • LECTURE NO.32.................................................................................................................................. 258 LECTURE NO.33.................................................................................................................................. 264 LECTURE NO.34.................................................................................................................................. 270 LECTURE NO.35.................................................................................................................................. 276 LECTURE NO.36.................................................................................................................................. 282 32.1. 32.2. 32.3. 33.1. 33.2. 33.3. 33.4. 33.5. 33.6. 33.7. 33.8. 34.1. 34.2. 34.3. 35.1. 35.2. GTFMUOTFSTDGCEMCUNCTIONIRST ECOND XAMPLE EMPLATES EMPLATE HIRDLASSLASS ENERIC SER VERLOADING VS EFAULT ENERICULTIPLEEMBER-D S TTS OLUTION EFINED OLUTION EMPLATES EMPLATES TPPA– OLUTION T ROGRAMMING EMPLATESTA OLICY LGORITHMSC.......................................................................................................................... 259 YPE EMPLATESRGUMENTS ASLASS T A : YPES 267................................................................................................................ 269 RGUMENTS............................................................................................................... 268S.T 272T: PECIALIZATION EMPLATE........................................................................................................... 267 EMPLATES : 264........................................................................................................ 276 260 270 259 P OLICY 264........................................................................................... 273 265 :.................................................................................
  • LECTURE NO.37 288 LECTURE NO.38 296 LECTURE NO.39 303 36.1. 36.2. 36.3. 36.4. 36.5. 36.6. 36.7. 36.8. 36.9. 37.1. 37.2. 37.3. 37.4. 37.5. 38.1. 38.2. 38.3. 38.4. 38.5. 39.1. 39.2. 39.3. 39.4. 39.5. 39.6. MPFCUNEDDRFRTDTTTTTTTGGGPUNCTIONUNCTION ARTIAL XAMPLE EMPLATES AND EMPLATES AND EMPLATES AND EMPLATES AND EMPLATES AND EMPLATES AND EMPLATES EMPLATES ROBLEMSOMPLETESINGESOLUTIONESOLUTIONONEFAULTEFAULTERIVATIONS IN CASE OF AENERICENERICENERICEMBER-TYPE D S IFFERENT T AAA–NT PECIALIZATION EMPLATES P 308TTSTEMPLATE CLASS YPELGORITHMSLGORITHMSLGORITHM ON &– ARAMETERS EMPLATES PECIALIZATION EMPLATE OOC S - PTYPE RDER RDERONCLUSION TATIC ARAMETERS IFFFFFNHERITANCE RIENDSRIENDSRIENDSRIENDSRIENDSS PECIALIZATIONS P 289......................................................................................................... 291R ARAMETERSOM....................................................................................................... 308 284EVISITED RR VERLOADING 286EMBERSEVISITEDEVISITED : 296––––............................................................................................... 283 304GRRRR............................................................................................. 285 A 288ULEULEULEULE ENERALRRAY 291 282..................................................................................... 303 1234 288 305................................................................................. 306 296................................................................................ 297................................................................................ 299................................................................................ 301............................................................................... 286 286 290T EMPLATE CLASS
  • LECTURE NO.40 309 LECTURE NO.41 316 LECTURE NO.42 325 LECTURE NO.43 334 40.1. 40.2. 41.1. 41.2. 41.3. 41.4. 41.5. 42.1. 42.2. 42.3. 42.4. 42.5. 42.6. 42.7. 42.8. 42.9. 43.1. CISSTLCFCIIICSACIAETERATORSTERATORSTERATORTERATORTERATORUNCTIONS FOR TANDARD EQUENCE XAMPLEURSORSOMMONONTAINERONTAINER ANDSSOCIATIVEONTAINERLGORITHMS CONTAINERS 309 – FCS O C A.......................................................................................................................... 312.......................................................................................................................... 325T UNCTIONS FOR UMMARY ATEGORIESRA PERATIONS ONTAINERS BNORMAL EMPLATE CEQUIREMENTSDAPTERS..................................................................................................................... 331ONTAINERS F IIRST 316TERATOR:........................................................................................................ 325 - LCLASS 327 T.................................................................................................... 325.................................................................................................... 327 IBRARY 326 ERMINATION A 327 T............................................................................................ 323 LL C YPES ONTAINERSC : 316ONTAINERS :
  • LECTURE NO.44.................................................................................................................................. 342 LECTURE NO.45.................................................................................................................................. 349 43.2. 43.3. 43.4. 43.5. 44.1. 45.1. GEEESR RROR XCEPTION XCEPTION TACKESOURCE RACEFUL UHNWINDING ANDLING T MHH ERMINATION ANDLING ANDLINGANAGEMENT 335 342 338P ROCESS

01.1. Introduction^ Lecture No.01Course Objective: Objective of this course is to make students familiar with the concepts ofobject oriented programming. These concepts will be reinforced by theirimplementation in C++. Course Contents: The main topics that we will study in the 45 lectures of this course are givenbelow, Recommended Text Book: Reference Books: • • • • • • • • 1. Object OrientationObjects and ClassesOverloadingInheritancePolymorphismGeneric ProgrammingException HandlingIntroduction to Design PatternsC++ How to Program (Object-Oriented Software Engineering Deitel & Deitel )

2. By Jacobson, Christerson, Jonsson, Overgaard(For object oriented programming introductory concepts)The C++ Programming Language By Bjarne Stroustrup(For better c++ understanding)

Objects in a School

Suppose we want to develop a fee collection system for a school for this we will needto find out related objects and their interactions as happens in real life.In this way we can say thatworld problems by thinking solution of the problem in terms of real world objects. So we can say that in our daily life everything can be taken as an object that behaves in acertain way and has certain attributes. In object orientation we move our concentration to objects in contrast to proceduralparadigm in which we simply write our code in functions and call them in our mainprogram. 01.2. A model is an abstraction of something real or conceptual.We need models to understand an aspect of reality.^ Teacher^ BookParents What is a Model?^ StudentPenClassroom object orientation^ makes it easier for usSchool BagPlaygroundLibrary to solve our real

Model Examples Highway mapsArchitectural modelsMechanical models 01.3. In the context of programming models are used to understand the problem beforestarting developing it.We make Object Oriented models showing several interacting objects to understanda system given to us for implementation. Example 1– Object Oriented Model Objects Ali, Car, House, Tree OO Models: Interactions Ali lives in the house

Example 2– Object Oriented Model (A School Model)Ali drives the car^ AliCardrives lives-in HouseTree

01.6. Examples of Tangible Objects: Ali is a tangible object, having some characteristics (attributes) and behavior as givenbelow, We will identify Ali using his name. Car is also a tangible object having some characteristics (attributes) and behaviorgiven below, CarState (attributes) ColorModel AliCharacteristics (attributes) NameAge^ 3. Tangible and Intangible Objects^ Unique identity Behavior (operations)AccelerateStart CarChange GearBehaviour (operations)WalksEats

We can identify Car using its registration number Examples of Intangible Objects (also called as conceptual objects): Time is an intangible (conceptual) object We will assign our own generated unique ID in the model for Time object Date is also an intangible (conceptual) object TimeState (attributes) HoursSecondsMinutes State (attributes) YearDayMonth Behavior (operations)Set/Get HoursSet/Get Seconds Set/Get Minutes Behavior (operations) Set/Get YearSet/Get DaySet/Get Month

We will assign our own generated unique ID in the model for Date object. 01.7. • • • • • • • • • • Summary: Model is the abstraction of some real word scenario. It helps us to understandthat scenario.Object oriented model of any scenario (problem) describes that scenario(problem) in the form of interacting objects.We use Object Orientation because it helps us in mapping real world problemin a programming language.Object Orientation is achieved using objects and their relationships.Properties of an object are described using itsobject is described using itsObjects may be tangible (physical) or intangible (also called conceptual orvirtual).Generally when we have given a certain problem description,problem description areThere may be more than one aspects of an objectIt is not necessary that every object has a specific role in implementation of aproblem there may be some objects without any role, like school parking inour school.It is easier to develop programs using Object Oriented Programming becauseit is closer to real life. candidates functions. for becoming objects of our system. data members and behavior of an nouns in that

Advantages of Information Hiding Following are two major advantages of information hiding, It simplifies our Object Oriented Model: Asinteractions hiding implementation details so it makes it easier for everyone tounderstand our object oriented model. It is a barrier against change propagation As implementation of functions is limited to our class and we have only given thename of functions to user along with description of parameters so if we changeimplementation of function it doesn’t affect the object oriented model.We can achieve information hiding usingthese two concepts in detail now, 02.2. Encapsulation means itself” Encapsulation and information hiding are much related concepts (informationwe Encapsulation saw earlier^ •^ •^ •^ thatAll information related to an object is stored within the objectIt is hidden from the outside worldIt can only be manipulated by the object itself “we have enclosed all the characteristics of an object in the object our object oriented Encapsulation model only and hadAbstraction objects , so we seeand their

hiding is achieved using Encapsulation)We have seen in previous lecture that object characteristics include data membersand behavior of the object in the form of functions.So we can say that Data and Behavior are tightly coupled inside an object andboth the information structure and implementation details of its operations arehidden from the outer world. Examples of Encapsulation Consider the same example of object Ali of previous lecture we described it asfollows,

You can see that Ali stores his personal information in itself and its behavior isalso implemented in it.Now it is up to object Ali whether he wants to share that information withoutside world or not. Same thing stands for its behavior if some other object inreal life wants to use his behavior of walking it can not use it without thepermission of Ali.So we say that attributes and behavior of Ali are encapsulated in it.Any other object don’t know about these things unless Ali share this informationwith that object through an interface,Same concept also applies to phone which has some data and behavior ofshowing that data to user we can only access the information stored in the phoneif phone interface allow us to do so. Advantages of Encapsulation The following are the main advantages of Encapsulation, a. As all data and functions are stored in the objects so there is no data or functionaround in program that is not part of any object and is this way it becomes veryeasy to understand the purpose of each data member and function in an object. b. As data members and functions are hidden in objects and each object has aspecific behavior so there is less complexity in codesituations that a functions is using some other function and that functions is Simplicity and clarityLow complexity there will be no such

02.3. using some other function.^ c.^ Everyone will be able to understand whole scenario by simple looking into objectdiagrams without any issue as each object has specific role and specific relationwith other objects. Interface^ Better understanding

Ali • • CharacteristicsBehavior(attributes)(operations) • • NameAgeWalksEats

02.5.^ b. Real Life example of separation of interface and implementations Separation of Interface & Implementation^ So it has,Address Book in a Phone^ Similarly take the example of contact details saved in the SIM of a phone,In that case we can say physical structure of SIM card as^ And Read/write operations provided by the phone as As discussed earlier we only show interface of an object to outside world andhide actual implementation from outside world. The benefit of using thisapproach is that our object interface to outside word becomes independentfrom inside implementation of that interface.This is achieved through the concepts of encapsulation and informationhiding.^ •^ • ¾^ Data StructureFunctionalityDriver has a standard interface to drive a car and using that interfacehe drive can drive any car regardless of its model or type whateverengine type it has or whatever type of fuel it is using.^ mechanism to change gear^ in the form of Mechanical structure of gear box^ Functionality^ Data Structure.

02.6. A Person sends message (stimulus) “stop” to a Car by applying brakesA Person sends message “place call” to a Phone by pressing appropriate button 02.7. Examples – Messages MessagesSummary Objects communicate through messages they send messages (stimuli) byinvoking appropriate operations on the target object. The number and kind ofmessages that can be sent to an object depends upon its interface • • • • • • Information hiding is achieved through encapsulation.Encapsulation and Information Hiding are related to each other.Interface of an object provides us the list of available functions.An object may have more than one interface.Interface and implementation are separated from each other to achieveInformation Hiding.Objects communicate with each other using messages.

Useful Links:http://www.alice.org/A Graphical Programming Environment to teach Computer Programming.