03 c# fundamentals- oop

44
8/17/2019 03 C# Fundamentals- OOP http://slidepdf.com/reader/full/03-c-fundamentals-oop 1/44 C# Fundamentals C# Fundamentals Object Oriented Programming Object Oriented Programming Paul Holley [email protected]

Upload: austinraja

Post on 06-Jul-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 1/44

C# FundamentalsC# Fundamentals

Object Oriented ProgrammingObject Oriented Programming

Paul [email protected]

Page 2: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 2/44

Resources Troelsen Chapter 4, 5

“4- Object Oriented Programming ith C!"

“5- #$ceptions and Object %i&etime"

 'lso, (ery highly recommended) Prosise Chapter *, p+ 54-5 “Types and #$ceptions" Prosise Chapter , p+ 5.-//

“The +0#T 1rameor2 Class %ibrary"

Other reading o& interest)

 “'pplied 3icroso&t +0#T 1rameor2 Programming", e&& Richter,Part “3anaging Types" 6p+ .-57/89 3icroso&t Press

Page 3: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 3/44

Certi&ication Resources

“3C':; 3C<: +0#T :e(eloping and =mplementing >indos-?ased 'pplications ith C! and 3icroso&t isual <tudio +0#T, #$am7-@/", 'mit Aalani

Chapter @, 1irst <ubchapters  Chapter , #rror Handling

“3C':; 3C<: :e(eloping >indos-?ased 'pplications ith isual?asic +0#T and isual C! +0#T, #$ams 7-7/ and 7-@/", 3att<toec2er+ 3icroso&t Press, *nd #dition+

Chapter @, %essons -/

  Chapter *, %essons @-*

  Chapter , %essons @-  Chapter 4, %essons @,4

 'lso, recommended) 3<:0 %ibrary

Page 4: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 4/44

O(er(ie Object Oriented Programming 6OOP8 in C!

#ncapsulation- Type (isibility, protection, and problemabstraction

=nheritance- Type reuse and relationships Polymorphism- Type similarity and uniBueness

<tructured #$ception Handling Try, Catch, 1inally, Thro

=nteracting ith the +0#T arbage Collector  :ispose68, 1inaliDe68, C+Collect68

Programming ith Common 1C% 0amespaces Euic2 intro to <ystem+=O

Page 5: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 5/44

 'genda 6OOP Code ?asics8

Troelsen Chapter 4, “Object Oriented Programming ith C!"

#ncapsulation

=nheritance

Polymorphism <tructured #$ception Handling

arbage Collection

Prosise Chapter , “The +0#T 1rameor2 Class %ibrary"

<ystem+=O

Page 6: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 6/44

OOP in +0#T Troelsen Chapter 4, “Object Oriented Programming ith C!"

 'n OOP Class is a custom user de&ined type) >ith data &ields 6internal (ariables a2a attributes8,

properties

 'nd &unctions to manage the data 6methods8 That o&ten model real-orld entities 6:emo @8

:emo 7@ #mployees, o(er(ie

#mp'pp- 3ain68 demonstration+ #mployee+cs- 'bstract base Class that other

employees inherit &rom 3anager+cs, PT<ales+cs, <ales#mp+cs- 0e Types

deri(ed &rom #mployee

Page 7: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 7/44

OOP in +0#T Troelsen Chapter 4, “Object Oriented Programming ith C!"

:emo 7@ #mployees #mployee+cs- #mployees Class de&inition+

0ote the custom constructor, &ields, methods

Custom constructors silently remove de&ault C! “ctor",hich sets all data to “sa&e (alues"+ =& any customconstructor is de&ined, de&ault must also be, or elsede&ault cannot be used+

  this. Aeyord used &or current object instance “sel&-re&erence"

  this. Aeyord used &or “&orarding constructor calls" “:e&ault Public =nter&ace", is de&ined as members

accessible (ia object instance, declared (ia  public 

 'ssembly (isibility type spec internal 

Page 8: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 8/44

Homeor2 3a2e a >indos 'pp that manages #mployee data+ The

 'pp should be based on :emo 7@ #mployees, and isdemonstrated ithout source by Homeor2+e$e

The 'pp must sho encapsulation and inheritance use isually inherit &rom a base &orm in an e$ternal assembly that has a

company logo “#mployee Co+" as a &i$ed label &ield+

Handle to ne 2inds o& #mployees, a Consultant and a HumanResource person+ 3a2e appropriate &ields, properties, ctorFs, andmethods+

HR person has int contacts property, and gets bonus o&contactsGamount

Consultant has &loat o(ertime property, and gets bonus o&o(ertimeGamount

Page 9: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 9/44

Homeor2 6cont8 3a2e a >indos 'pp that manages #mployee data+ The 'pp

should be based on :emo 7@ #mployees, and is demonstratedithout source by Homeor2+e$e

The 'pp must sho use o& polymorphism The 'pp should add ne employees o& the (arious 2ind into an

internal #mployee 'rray, and initialiDe data, &or any o& the (arious2inds o& employee+ arious screen entries can accomplish this,as gi(en in Homeor2+e$e+

The 'pp must ha(e &unctionality that ill clear all employees o&any type

The 'pp must be able to display the “stats" or employee in&o o&any selected employee

Compute a ee2ly payroll list ith names and amounts, andprint it to a &ile pay+t$t+

 ' payroll total should be displayed on the =+

Page 10: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 10/44

Homeor2 6cont8 3a2e a >indos 'pp that manages #mployee data+

The 'pp should be based on :emo 7@ #mployees, andis demonstrated ithout source by Homeor2+e$e

The 'pp must be able to distinguish deri(ed classmembers by type The payroll is computed by issuing bonus amounts 6belo8,

and then adding currPayFs into a total) 3anager- @777+77 <ales- @77+77 PT <ales- 57+77

HR- @7+77 Consultant-@77+77

#ITR' CR#:=T) <a(e the current employee data to a &ileemps+dat

#ITR' CR#:=T) Reco(er the sa(ed employees

Page 11: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 11/44

OOP in +0#T Troelsen Chapter 4, “Object Oriented Programming ith C!"

Pillars o& OOP #ncapsulation- hiding implementation details

security

later re-implementation upgrade, changes

=nheritance- promoting code reuse “is-a", classical inheritance o& “base class"

“has-a", internal Class member+ Containment 6Class usesoutside Class8 or nested 6Class de&ined ithin Class8+

Polymorphism- using related objects in a similar ay Classical, all descendants ha(e a “base" beha(ior 

“ad-hoc", objects ith similar method signatures can betreated as a common object

Page 12: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 12/44

OOP in +0#T C! 'ccessibility 3odi&iers 6&or #ncapsulation speci&ication8

Page 13: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 13/44

OOP in +0#T

C! 'ccessibility %e(el 3odi&iers

Page 14: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 14/44

OOP in +0#T Troelsen Chapter 4, “Object Oriented Programming ith C!"

C! #ncapsulation <er(ices Class &ields o&ten restricted ith private, readonly 

6Troelsen compares “traditional" 'ccessor 6et8 method, 3utator6<et8 method, &or Class private &ields8

C! Class properties, et and <et, eg #mp=: in :emo @0ote the use o& (alue in <et, hich permits (alidation andtrans&ormation o& the (alue be&ore mapping to pri(ate &ield

“Read only" properties only ha(e et “>rite only" properties only ha(e <et static properties are “class le(el", apply to all objects created &or

the Class, not just a gi(en object instance 6eg, Comp0ame in#mployee8+ <tatic initialiDation can occur (ariously duringprogram initialiDation or 6repeatedly8 as part o& objectconstructiion+

static readonly, li2e a const, but &ields can be computed at run-

time+

Page 15: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 15/44

OOP in +0#T Troelsen Chapter 4, “Object Oriented Programming ith C!"

C! =nheritance <upport nheritance- promoting code reuse

“is-a", classical inheritance o& “base class" by a “deri(edclass" or “subclass", eg, <alesPerson) #mployee in :emo @

<ubclass le(erages all the beha(iors o& base class, and addsspeci&ic details de&ined by the subclass

?ase class creation is best accomplished by base classconstruction during subclass constructionpublic 3anager6string 1ull0ame, int emp=:,

&loat currPay, string ssn, ulong numbO&Opts8! base"FullName emp$ currPay ssn%

  J

se o& the base 2eyord is not limited to constructor code,can be used any time a subclass ishes to access a baseclass property or &ield

Page 16: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 16/44

OOP in +0#T Troelsen Chapter 4, “Object Oriented Programming ith C!"

C! =nheritance <upport

  nheritance- promoting code reuse

3ultiple base class inheritance is not permitted in C! 3ultiple inter&ace inheritance is permitted public- generally accessible pri(ate- class speci&ic protected- accessible by any deri(ed subclass, not object

user  sealed- cannot be inherited9 eg, the <ystem+<tring class is

sealed, and should not be “e$tended"

Page 17: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 17/44

OOP in +0#T  http);;msdn+microso&t+com;practices;type;Patterns;#nterprise;de&ault+asp 

C! <ingleton :esign Pattern

 

Singleton- One 6and O0%K one8 possible classinstance desired+ Common implementation  'lternate implementation “Thread-sa&e" implementation OthersL

Page 18: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 18/44

OOP in +0#T Troelsen Chapter 4, “Object Oriented Programming ith C!"

C! =nheritance <upport

:emo * <taticReadOnly:ata :emonstrates static readonly+

%i2e a const, but &ields can be computed; initialiDed atrun-time+

Page 19: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 19/44

OOP in +0#T Troelsen Chapter 4, “Object Oriented Programming ith C!"

C! =nheritance <upport

:emo Containment

=nheritance- promotes code reuse nheritance- containment and delegation

“has-a", internal Class member is another Class+

 Containment - eg, Car Class can contain a Radio Class “Parent" class contains “child" class

 Delegation- adding members to the parent so that objectusers can access the child class, eg Cran2Tunes68 instead o&objCar+the3usic?o$+TurnOn68

Page 20: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 20/44

OOP in +0#T

Troelsen Chapter 4, “Object Oriented Programming ith C!"

C! =nheritance <upport:emo 4 0ested

=nheritance- promoting code reuse

nheritance- nested Class “has-a", internal Class member is de&ined ithin Class+

 Nested Class- eg, Radio Class is de&ined ithin Car Class asa “helper Class"

0OT#) :enotation o& nested classes is (ia “M"+ 1or e$ample,Car &'adio denotes the Radio class ithin Car in 3<:0documentation6but ne Car+Radio68 ma2es an instance8+

0ested classes can be declared pri(ate

Page 21: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 21/44

OOP in +0#T Troelsen Chapter 4, “Object Oriented Programming ith C!"

C! Polymorphic <upport

:emo @ #mployee Polymorphism- using related objects in a similar ay

Polymorphism- override Classical, all descendants ha(e a “base" beha(ior 

o(erride must be used &or both abstract de&inition and (irtual rede&inition

<ubclass can rede&ine a base class member (ia o(erridepublic class 3anager ) #mployee J N

public override (oid i(e?onus6&loat amount8JN

The base class method can still be used (ia basebase+i(e?onus6amount89

Page 22: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 22/44

OOP in +0#T Troelsen Chapter 4, “Object Oriented Programming ith C!"

C! Polymorphic <upport

:emo @ #mployee, :emo 5 <hapes

Polymorphism- abstract Class and method abstract Class- not to be instantiated, no direct instancesabstract public class #mployee J N ;; :emo @

0OT#) cannot  do #mployee I ne #mployee689 Can do #mployee I ne 3anager689 Can do

<hapeQ s J ne He$agon68, ne Circle68, N 9

abstract methods in base class-subclass must supply implementation, no de&ault

#$ample is :ra68 method &or <hapes

public abstract (oid :ra689

Page 23: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 23/44

OOP in +0#T Troelsen Chapter 4, “Object Oriented Programming ith C!"

C! Polymorphic <upport Polymorphism- using related but deri(ation-speci&ic

objects in a similar ay

Polymorphism- (ersioning methods ith ne( ne means that a subclass is “hiding" a base class method

This is called (ersioning

#$amplene( public (oid :ra68J N ;; in O(al class

The purpose o& ne( is to declare that no de&ault basemethod should be used

Page 24: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 24/44

Class Casting in +0#T Troelsen Chapter 4, “Object Oriented Programming ith C!"

Casting beteen Class Types

=t is alays sa&e to re&erence a deri(ed class ith a

base class re&erence#mployee I ne 3anager689 ;; o23anager m 63anager8I9 ;; need casting

Hoe(er, e$plicit casting is necessary hen deri(edmembers are usedobject o ne 3anager6N89N1ireThisPerson6 6#mployee8o 89

Page 25: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 25/44

+0#T OOP >iDards Troelsen Chapter 4, p+ **5-**.

<toec2er Chapter @, %esson; %ab @

 'dd Class >iDard - :emo

 'dd 3ethod >iDard - :emo

Page 26: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 26/44

+0#T isual =nheritance 3<:0S +0#T :e(elopmentS isual <tudio +0#TS

isual ?asic and isual C!S Creating >indos 'pplicationsS>indos 1ormsS >indos 1orms =nheritance  'lso, Aalani Chapter @ 6@+, “sing isual =nheritance"8

and <toec2er Chapter *, %esson *

isual =nheritance <ho ho &orms can inherit &rom “?ase 1orms"

0e1orm) ?ase1orm9 The base &orm is made as a Class %ibrary 6dll8 and can be used

by other assemblies+ 'll controls and &eatures are inherited, andne 1orms “build on top"+

:emo 7 isual=nheritance inherits &rom:emo 7 ?ase1orm%ibrary Change control property “3odi&iers" on ?ase 1orm to indicate

hat inheritance changes are permitted 1orms can inherit &rom “?ase 1orms" (ia “'dd- =nherited 1orm"

public class 1orm*) :emoU7U?ase1orm%ibrary+1orm@ JN

Page 27: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 27/44

+0#T isual =nheritance 3<:0S +0#T :e(elopmentS isual <tudio +0#TS

isual ?asic and isual C!S Creating >indos 'pplicationsS>indos 1ormsS >indos 1orms =nheritance

:emo 7 isual=nheritance inherits &rom

:emo 7 ?ase1orm%ibrary 3a2ing a ?ase1orm%ibrary as speci&ied, adding the ?ase1ormRe&erence to the =nheriting1orm project, and programaticallyinheriting into a 1orm by ) ?ase1orm+1orm@ just or2sV

0OT#) :onFt get &rustrated ith the 3<:0 directions in the

re&erence abo(e+ sing the inheritance pic2er, the inherited1orm* does not ha(e a 3ain68 ith an appropriate 'pplication+Run6 ne 1orm*6889

 'lso, i& you try to do a ne clic2 on the pri(ate, it may insert codethat &ails to compile

=& the ?ase1orm is changed, the =nheriting1orm needs to

Re?uild to pic2 up the changes

Page 28: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 28/44

#$ceptions in +0#T Troelsen Chapter 5, “#$ceptions and Object %i&etime"

#$ception Handling

:emo @7 <imple#$ceptions

:emo @@ Custom#$ception

thro, tryJ , catchJ , &inallyJ sed across all +0#T languages, so uni&orm error

handling across managed binaries, applications,machines is possible

#$ceptions deri(e &rom <ystem+#rror  se thro to in(o2e an e$ception

thro ne #$ception6“= ha(e a problem"89

<hould establish a tryJ catch J  bloc2 to handle the

e$ception

Page 29: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 29/44

#$ceptions in +0#T Prosise Chapter *, “#$ception Handling", p 54-5

#$ception Handling, common prede&ined 1C% #$ceptions

Page 30: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 30/44

#$ceptions in +0#T Troelsen Chapter 5, “#$ceptions and Object %i&etime"

#$ception Handling

:emo @@ Custom#$ceptions  Car.cs de&ines a “Custom #$ception"

public class Car=s:ead#$ception ) <ystem+'pplication#$ceptionJ N

 App.cs establishs a tryJ catch J  bloc2 to handle thee$ception

 Car.cs de&ines a SpeedUp() method that ill thro an ArgumentOutO!ange"#ception and a customCar$sDead"#ception

3ultiple, or no, catchJ bloc2s can be used ith a tryJ 

bloc2+ 3ust be in order o& speci&ic to general+

Page 31: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 31/44

#$ceptions in +0#T Troelsen Chapter 5, “#$ceptions and Object %i&etime"

#$ception Handling, p+ @[email protected]

:emo @@ #$ceptions  App.cs establishs a &inallyJ  bloc2 that alays ill be

e$ecuted a&ter a tryJ bloc2 scope is e$ited

The &inallyJ bloc2 is an optional e$tension to the tryJ bloc2

0otice that the catchJ bloc2 is not reBuired &or the&inallyJ to be gi(en

Page 32: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 32/44

C in +0#T C! %< @+4, +. 6'utomatic 3emory 3anagement8

Prosise Chapter *, “0on-:eterministic :estruction", p -4* Troelsen Chapter 5, “#$ceptions and Object %i&etime"

Object %i&etime and the arbage Collector

)emory management- use ne to ma2e a heap

object, use null “to ma2e eligible &or C" +0#T runtime ill mar2 object hen “no longer needed", hen

the de&inition attached to object drops out o& scope or isnullFed 6called “non-deterministic &inaliDation"8

%i&etime issue is ho this happens

%i&etime issue is ho to impro(e the per&ormance o& the C

%i&etime issue is ho to destroy created objects, li2e &ilerelated objects and datasets, that need to close68

C! %< +. is great reading about the ambiguities and

insecurities o& automatic memory management

Page 33: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 33/44

C in +0#T Troelsen Chapter 5, “#$ceptions and Object %i&etime"

Object %i&etime and the arbage Collector 

:emo@* <imple1inaliDe To support an automatic %inali&e() method &or a

Class, use the destructor 'ClassName se hen the Class supports connections, datasets, other

releasable resources :estructors may actually postpone garbage collection due to

a “&inaliDation Bueue"

:emo@ :isposableCar   est advice. se $Disposable inter&ace inheritance to

enable a Class Dispose() implementation that is usedhene(er desired, to &ree resources+ ThatWs hat>indos 'pps do+

Page 34: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 34/44

C in +0#T Troelsen Chapter 5, “#$ceptions and Object %i&etime"

Object %i&etime and the arbage Collector 

:emo @4 1inaliDed'nd:isposed :emo @5 enerations

arbage Collection Only &orces a collection hen space doesnFt e$ist &or a ne 

Can be used (ia <ystem+C, &or e$ample C+Collect68 #(ery object that sur(i(es a collection is bumped up a

generation, until “gen *" hich means “sur(i(ed many

collection attempts" Collection occurs &or ne generations &irst, “gen 7" :emo @5 simply illustrates that object C is obscure Kour interactions ith the C should be minimiDed Consult Richter &or C algorithm details

Page 35: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 35/44

=O- Euic2 Concepts Prosise Chapt , “1ile and <tream =O","Te$t =;O","?inary =;O"

=;O can be done as string “readers" and “riters" <tring based <treamReaders and >riters do 0OT permit

random access, are serial

Te$t 1ile =;O Program, implements “X TKP# &ilename"<treamReader reader ne <treamReader6 &ilename89&or6 string line reader+Read%ine 689

line V null9 line reader+Read%ine 688J  Console+>rite%ine6 line89reader+Close 689

Page 36: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 36/44

=O- Euic2 Concepts Prosise Chapt , “1ile and <tream =O","Te$t =;O","?inary =;O"

=;O can be done as 1ile<tream stream6!@@ =O- :emo 7* ?asic1ile'pp8

Permit random access, are serial, byteQ oriented, do notreBuire separate readers and riters+

Te$t 1ile =;O Program, rites and reads &ile1ile<tream my1<tream ne 1ile<tream6 Ytest+datY,

1ile3ode+OpenOrCreate, 1ile'ccess+Read>rite89

&or6 int i79 iZ*5/9 i[[ 8J my1<tream+>rite?yte6 6byte8i89 my1<tream+Position 79&or6 int i79 iZ*5/9 i[[ 8J Console+>rite6 my1<tream+Read?yte6889 Console+>rite%ine689my1<tream+Close689

Page 37: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 37/44

<ystem+=O in +0#T Prosise Chapter , “The +0#T 1C%", p 5.-/5

<ystem+=O

 ' general procedure &or reading and riting &iles in amanaged application is as &ollos)

  Open the &ile using a %ileStream object+ 1or binary reads and rites, rap instances o& inary!eader  

and inary*riter  around the %ileStream object and callinary!eader and inary*riter methods such as !ead  and*rite to per&orm input and output+ ?etter, just use stream+

1or reads and rites in(ol(ing te$t, rap a Stream!eader  andStream*riter  around the %ileStream object and useStream!eader  and Stream*riter  methods such as !ead+ine and *rite+ine to per&orm input and output+

  Close the %ileStream object+

Page 38: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 38/44

<ystem+=O in +0#T Prosise Chapter , “The +0#T 1C%", p 5.-/5

<ystem+=O

1ile and <tream =O

<ystem+=O+<tream is a &undamental building bloc2 &or1iles, <oc2ets, Cryptography, e(en 3emory access

<treams ha(e =nput and Output, and also ha(e anadditional le(el o& abstraction called readers and

riters 6that may or may not be used8  Simple byte streams, or inary!eader and

inary*riter  are used &or binary =O+ e#t!eader  ande#t*riter  are used &or te$t+

Page 39: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 39/44

<ystem+=O in +0#T Prosise Chapter , “The +0#T 1C%", p 5.-/5

<ystem+=O

:emo 7 %ist

 Stream!eader  is used to implement a “Te$t 1ile:ump", li2e the “Type" batch command

:emo @ <cramble

 inary!eader and inary*riter  are used toimplement a &ile “2ey encryption"

#ncryption details are not important, but this program&ollos the “general procedure" &or &ile =O gi(en earlier 

Page 40: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 40/44

0e Toolbo$ Pic2s

<olutions Homeor2 0umericp:on

=nitialiDation properties- +0ame, +Te$t, +alue

sage

pri(ate (oid #mpPic2UalueChanged6+++8J  int i 6int8#mpPic2+alue9

  +++

Page 41: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 41/44

Homeor2 3a2e a >indos 'pp that manages #mployee data+ The

 'pp should be based on :emo 7@ #mployees, and isdemonstrated ithout source by Homeor2+e$e

The 'pp must sho encapsulation and inheritance use

isually inherit &rom a base &orm in an e$ternal assembly that has acompany logo “#mployee Co+" as a &i$ed label &ield+

Handle to ne 2inds o& #mployees, a Consultant and a HumanResource person+ 3a2e appropriate &ields, properties, ctorFs, andmethods+

HR person has int contacts property, and gets bonus o&contactsGamount

Consultant has &loat o(ertime property, and gets bonus o&o(ertimeGamount

Page 42: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 42/44

Homeor2 6cont8 3a2e a >indos 'pp that manages #mployee data+ The 'pp

should be based on :emo 7@ #mployees, and is demonstratedithout source by Homeor2+e$e

The 'pp must sho use o& polymorphism The 'pp should add ne employees o& the (arious 2ind into an

internal #mployee 'rray, and initialiDe data, &or any o& the (arious2inds o& employee+ arious screen entries can accomplish this,as gi(en in Homeor2+e$e+

The 'pp must ha(e &unctionality that ill clear all employees o&any type

The 'pp must be able to display the “stats" or employee in&o o&any selected employee

Compute a ee2ly payroll list ith names and amounts, andprint it to a &ile pay+t$t+

 ' payroll total should be displayed on the =+

Page 43: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 43/44

Homeor2 6cont8 3a2e a >indos 'pp that manages #mployee data+

The 'pp should be based on :emo 7@ #mployees, andis demonstrated ithout source by Homeor2+e$e

The 'pp must be able to distinguish deri(ed classmembers by type The payroll is computed by issuing bonus amounts 6belo8,

and then adding currPayFs into a total) 3anager- @777+77 <ales- @77+77 PT <ales- 57+77

HR- @7+77 Consultant-@77+77

#ITR' CR#:=T) <a(e the current employee data to a &ileemps+dat

#ITR' CR#:=T) Reco(er the sa(ed employees

Page 44: 03 C# Fundamentals- OOP

8/17/2019 03 C# Fundamentals- OOP

http://slidepdf.com/reader/full/03-c-fundamentals-oop 44/44

Homeor2 6cont8 3a2e a >indos 'pp that manages #mployee data+

The 'pp should be based on :emo 7@ #mployees, andis demonstrated ithout source by Homeor2+e$e

0OT# ?#0#) 1uture assignments ill build on the

current #mployee 'pp The #mployees class ill be impro(ed to

>or2 ill the “&oreach" iterator in a collection

Ha(e name, =:, and other inde$ers9 and a “[" operator 

?e ritten and reco(ered to a &ile

?e ritten and reco(ered to an ':O+0#T database