mit6_096iap11_lec10
TRANSCRIPT
-
7/30/2019 MIT6_096IAP11_lec10
1/11
IntroductiontoC++ January26,2011MassachusettsInstituteofTechnology 6.096
Lecture10Notes: AdvancedTopics II1 StuffYouMayWanttoUse inYourProject1.1 FilehandlingFilehandling inC++worksalmost identicallytoterminal input/output. Tousefiles,youwrite#include atthetopofyoursourcefile. Thenyoucanaccesstwoclassesfromthestdnamespace:
ifstream
allows
reading
input
from
files
ofstreamallowsoutputtingtofiles
Eachopenfile isrepresentedbyaseparateifstreamoran ofstreamobject. Youcanuseifstreamobjectsinexcatlythesamewayascinandofstreamobjectsinthesamewayascout,exceptthatyouneedtodeclarenewobjectsandspecifywhatfilestoopen.Forexample:
1 #include 2 using n a m e s p a c e std;34 int main() {5 ifstream source( "source- f i l e.txt" ) ;6 ofstream d e s t i n a t i o n( "dest- f i l e.txt" ) ;7 int x ;8 source >> x ; // Reads one int from source- f i l e.txt9 source.close(); // close file as soon as we re done using it10 d e s t i n a t i o n
-
7/30/2019 MIT6_096IAP11_lec10
2/11
Youcanspecifyasecondargumenttotheconstructorortheopenmethodtospecifywhatmodeyouwanttoaccessthefileinread-only,overwrite,writebyappending,etc.Checkdocumentationonlinefordetails.
1.2 ReadingStringsYoull likelyfind thatyouwant to read some text input from theuser. Weve so far seenonlyhowtodoints,chars,etc.Itsusuallyeasiest tomanage textusing theC++ stringclass. Youcan read ina stringfromcinlikeothervariables:
1 string mo b i le C a r ri e r ;2 cin >> mo b i le C a r ri e r ;However, thismethod only reads up to the firstwhitespace; it stops at any tab, space,newline,etc. Ifyouwanttoreadmultiplewords,youcanusethegetlinefunction,whichreadseverythingupuntiltheuserpressesenter:
1 string s e n t e n c e ;2 g e t l i n e ( c i n , s e n t e n c e ) ;
1.3 enumInmanycasesyoullfindthatyoullwanttohaveavariablethatrepresentsoneofadiscreteset
of
values.
For
instance,
you
might
be
writing
acard
game
and
want
to
store
the
suit
of
acard,whichcanonlybeoneofclubs,diamonds,hearts,andspades. Onewayyoumightdothis isdeclaringabunchof const ints,eachofwhich isanID foraparticularsuit. IfyouwantedtoprintthesuitnameforaparticularID,youmightwritethis:
1 const int CLUBS = 0 , DIAMONDS = 1 , HEARTS = 2 , SPADES = 3;2 void p r i n t _ s u i t(const int suit) {3 const char *names[] = { "Clubs", "Diamonds",4 "Hearts", "Spades"};5 return names[suit];6 }Theproblemwith this is that suit couldbe integer, notjustoneof the setofvaluesweknowitshouldberestrictedto.Wedhavetocheckinourfunctionwhethersuitistoobig.Also,theresnoindicationinthecodethattheseconst intsarerelated.Instead,C++allowsustouseenums. Anenumjustprovidesasetofnamed integervalueswhicharetheonlylegalvaluesforsomenewtype.Forinstance:
2
-
7/30/2019 MIT6_096IAP11_lec10
3/11
1 enum suit_t {CLUBS, DIAMONDS, HEARTS, SPADES};2 void p r i n t _ s u i t(const suit_t suit) {3 const char *names[] = { "Clubs", "Diamonds",4 "Hearts", "Spades"};5
return
names[
suit
];
6 }Now, it is illegal topassanythingbutCLUBS,DIAMODNS,HEARTS,orSPADES intoprint suit. However, internally the suit tvaluesare stilljust integers, andwe canusethemassuch(asinline5).Youcanspecifywhichintegersyouwantthemtobe:
1 enum suit_t { C L U B S=18, DIAMONDS=91, HEARTS= 2 4 1 , SPADES=13};Thefollowingrulesareusedbydefaulttodeterminethevaluesoftheenumconstants:
Thefirstitemdefaultsto0. Everyotheritemdefaultstothepreviousitemplus1.
Just like any other type, an enum type such as suit t can be used for any arguments,variables,returntypes,etc.
1.4 StructuringYourProjectManyobject-orientedprograms likethoseyourewriting foryourprojectsshareanoverallstructureyouwill likelywant touse. Theyhave somekindofmanagingclass (e.g., Game,Directory, etc.) thatmaintains all the other objects that interact in the program. Forinstance, inaboardgame,youmighthaveaGameclassthat isresponsible formaintainingPlayerobjectsandtheBoardobject.Oftenthisclasswillhavetomaintainsomecollectionofobjects,suchasalistofpeopleoradeckofcards;itcandosobyhavingafieldthatisanSTLcontainer.maincreatesasingleinstanceofthismanagingclass,handlestheinteractionwith the user (i.e. asking the userwhat to do next), and callsmethods on themanagerobjecttoperformtheappropriateactionsbasedonuserinput.
2 ReviewSome of the new conceptswell cover require familiaritywith conceptsweve touched onpreviously.Theseconceptswillalsobeusefulinyourprojects.
3
-
7/30/2019 MIT6_096IAP11_lec10
4/11
2.1 ReferencesReferencesareperfectlyvalid types,just likepointers. For instance,just like int * isthepointertoanintegertype,int &isthereferencetoanintegertype.Referencescanbepassedasargumentstofunctions,returnedfromfunctions,andotherwisemanipulatedjustlikeanyothertype.Referencesarejustpointersinternally;whenyoudeclareareferencevariable,apointertothevaluebeingreferencediscreated,anditsjustdereferencedeachtimethereferencevariableisused.Thesyntaxforsettingareferencevariabletobecomeanaliasforanothervariableisjustlikeregularassignment:
1 int & x = y ; // x and y are now two names for the same variableSimilarly,whenwewanttopassargumentstoa functionusingreferences,wejustcallthefunctionwith the argumentsasusual, andput the & in the functiondefiniton,where theargumentvariablesarebeingsettotheargumentsactuallypassed:
1 void sq(int & x ) { // & is part of the type of x2 // - x is an int r e f e r e n c e3 x *= x ;4 }5 sq( y ) ;Notethatonthe last line,wherewespecifywhatvariablexwillbeareferenceto,wejustwritethenameofthatvariable;wedontneedtotakeanaddresswith&here.Referencescanalsobereturnedfromfunctions,asinthiscontrivedexample:
1 int g ; // Global variable2 int &getG() { // Return type is int r e f e r e n c e3 return g ; // As before, the value we re makin g a4 // r e f e r e n c e *to* doesnt get an & in front of it5 }67 // ...S o m e w h e r e in main8 int &gRef = getG(); // gRef is now an alias for g9 gRef = 7; // Modifies gIfyourewritingaclassmethodthatneedstoreturnsomeinternalobject,itsoftenbesttoreturnitbyreference,sincethatavoidscopyingovertheentireobject.Youcouldalsothenuseyourmethodtodosomethinglike:
1 vector & c a r d L i s t4
-
7/30/2019 MIT6_096IAP11_lec10
5/11
2 = d e c k . g e t L i s t ( ) ; // getList declared to return a r e f e r e n c e3 c a r d L i s t . p o p _ b a c k ( ) ;Thesecondlineheremodifiestheoriginallistindeck,becausecardListwasdeclaredasareferenceandgetListreturnsareference.
2.2 const2.2.1 Convertingbetween constandnon-constYoucanalwaysprovideanon-constvaluewhereaconstonewasexpected. For instance,youcanpassnon-constvariablestoafunctionthattakesaconstargument.Theconst-nessoftheargumentjustmeansthefunctionpromisesnottochangeit,whetherornotyourequirethatpromise.Theotherdirectioncanbeaproblem: youcannotprovideaconstreferenceor
pointer
where
anon-const
one
was
expected.
Setting
anon-const
pointer/reference
to
a
constonewouldviolatethe lattersrequirementthat itnotbechangeable. Thefollowing,forinstance,doesnotwork:
1 int g ; // Global variable2 const int &getG() { return g ; }34 // ...S o m e w h e r e in main5 int &gRef = getG();This fails because gRef is a non-const reference, yetwe are trying to set it to a constreference
(the
reference
returned
by
getG).
Inshort,thecompilerwillnot letyouconvertaconstvalue intoanon-constvalueunlessyourejustmakingacopy(whichleavestheoriginalconstvaluesafe).2.2.2 const functionsFor simple values like ints, the concept of const variables is simple: a const int cantbemodified. Itgetsa littlemorecomplicatedwhenwe starttalkingabout constobjects.Clearly, no fields on a const object should bemodifiable, butwhatmethods should beavailable? Itturnsoutthatthecompilercantalwaystell for itselfwhichmethodsaresafetocallonconstobjects,soitassumesbydefaultthatnoneare.Tosignalthatamethodissafetocallonaconstobject,youmustputtheconstkeywordattheendofitssignature,e.g. int getX() const;. constmethods that returnpointers/references to internalclassdatashouldalwaysreturnconstpointers/references.
5
-
7/30/2019 MIT6_096IAP11_lec10
6/11
3 ExceptionsSometimes functions encounter errors thatmake it impossible to continue normally. Forinstance, a getFirst function thatwas called on an empty Array objectwould have noreasonablecourseofaction,sincethereisnofirstelementtoreturn.Afunctionscansignalsuchanerrortoitscallerbythrowing anexception.Thiscausesthefunctiontoexitimmediatelywithnoreturnvalue.Thecallingfunctionhastheopportunitytocatch theexceptiontospecifyhow itshouldbehandled. If itdoesnotdoso, itexitsimmediately aswell, the exception passes up to the next function, and so onup the callstack (thechainoffunctioncallsthatgotustotheexception).Anexample:
1 const int DIV_BY_0 = 0;2 int divide(const int x, const int y ) {3 if( y == 0)4 throw DIV_BY_0;5 return x / y ;6 }78 void f (int x, int **arrPtr) {9 try {10 *arrPtr = new int[divide(5, x )];11 } catch(int error) {12 // cerr is like cout but for error me ss age s13 cerr
-
7/30/2019 MIT6_096IAP11_lec10
7/11
5 }67 void f (int x, int **arrPtr) {8 try {9 *arrPtr = new int[divide(5, x )];10 }11 catch(b a d _ a l l o c &error) {//new throws e x c e p t i o n s of this type12 cerr
-
7/30/2019 MIT6_096IAP11_lec10
8/11
Wecanspecifythatagivenexternalfunctiongetsfullaccessrightsbyplacingthesignatureofthe function insidetheclass,precededbytheword friend. Forexample, ifwewanttomakethefieldsoftheUSCurrencytypefromthepreviouslectureprivate,wecanstillhaveourstream insertionoperator (theoutputoperator,
-
7/30/2019 MIT6_096IAP11_lec10
9/11
6 CastingCastingistheprocessofconvertingavaluebetweentypes.WevealreadyseenC-stylecastse.g. 1/(double)4. SuchcastsarenotrecommendedinC++;C++providesanumberofmorerobustmeansofcastingthatallowyoutospecifymorepreciselywhatyouwant.AllC++-stylecastsareoftheformcast type(value),wheretypeisthetypeyourecastingto.Thepossiblecasttypestoreplacecast typewithare:
static castThis isby farthemostcommonlyusedcast. Itcreatesasimplecopyof thevalueofthe specifiedtype. Example: static cast(x),where x isanint,givesafloatcopyof x.
dynamic cast Allows convertingbetweenpointer/reference typeswithinan inheritancehierarchy. dynamic cast checkswhether value isactuallyof type type. Forinstance,wecouldcastaVehicle *calledvtoaCar *bywritingdynamic cast(v). If v is in factapointer toa Car,nota Vehicleof someother type suchasTruck, this returns a valid pointer of type Car *. If v does not point to a Car, itreturnsnull.dynamic castcanalsobeusedwithreferences: ifvisaVehicle &variable,dynamic cast(v)willreturnavalidreferenceif visactuallyareferencetoaCar,andwillthrowabad castexceptionotherwise.
reinterpret castDoesnoconversion;justtreatsthememorycontainingvalueasthoughitwereoftypetype
const castUsedforchangingconstmodifiersofavalue.Youcanusethistotellthecompilerthatyoureallydoknowwhatyouredoingandshouldbeallowedtomodifyaconstvariable.Youcouldalsouseittoaddaconstmodifiertoanobjectsoyoucanforceuseoftheconstversionofamemberfunction.
7 ThatsAll!Thisistheendofthe6.096syllabus,buttherearelotsofreallyneatthingsyoucandowithC++thatwehaventeventouchedon. Justtogiveyouataste:
Unionsgroupmultipledatatypestogether;unlikeclasses/structs,though,thefieldsof
aunion
are
mutually
exclusive
only
one
of
them
is
well-defined
at
any
time
Namespacesallowyou towrapupallyourcode, classes, etc. intoadirectoryofnames,likestd
AdvancedSTLmanipulation allowsyou todo all sortsofwacky thingswithSTLcontainersanditerators
9
-
7/30/2019 MIT6_096IAP11_lec10
10/11
voidpointerspointerstodataofanunknowntype virtual inheritance the solution to the dreadeddiamond problem described inLecture8
Stringstreamsallowyoutoinputfromandoutputtostringobjectsasthoughtheywerestreamslikecinandcout
Run-time type information (RTTI)allowsyou toget informationon the typeofavariableatruntime
vtableshowthemagicofvirtualfunctionsactuallyworksIfyouare interested in learningmoreaboutthesesubjectsoranythingwevediscussed,weencourage you to look through online tutorials, perhaps even to buy aC++book andmostimportantly,tojustplayaroundwithC++onyourown!
10
-
7/30/2019 MIT6_096IAP11_lec10
11/11
MIT OpenCourseWare
http://ocw.mit.edu
6.096 Introduction to C++
January (IAP) 2011
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
http://ocw.mit.edu/http://ocw.mit.edu/termshttp://ocw.mit.edu/termshttp://ocw.mit.edu/