C++ ReviewC++ Review11Cpt S 223. School of EECS, WSUPurpose of Review Review some basic C++Familiarize us with Weiss’sstyleFamiliarize us with Weisss style Some good coding practicesdf flf Introduce specific constructs useful for implementing data structures STL overviewNote: Not all slides shown in PPT;2 Refer to the PDF version of slides for thatCpt S 223. School of EECS, WSUClass The Class defines the data structure and the operations that access and pmanipulate it Member data Member functions or methods Encapsulation = data + methods Information hiding Public vs. private vs. protected33Cpt S 223. School of EECS, WSUCommentConstructorConstructor4Cpt S 223. School of EECS, WSUEncapsulInformationHidingation5Cpt S 223. School of EECS, WSUSeparation of Interface and Implementation Interface (.h) fileDefines class and its member data andDefines class and its member data and functionsImplementation ( cc or cpp) fileImplementation (.cc or .cpp) file Provides implementations of member functionsfunctions6Cpt S 223. School of EECS, WSUExtra Syntax Default parametersInitializer listInitializer list Explicit constructorbf Constant member function Accessor methods Mutator methods7Cpt S 223. School of EECS, WSUDefault ExplicitParameterspConstructorInitializerLi tAccessorListAccessorMutatorIntCell obj;obj = 37;Mutator8Cpt S 223. School of EECS, WSUPreprocessorDirectives9IntCell class interface in file IntCell.h.Cpt S 223. School of EECS, WSUPreprocessorDirectiveScopinggOperatorClassName::member10IntCell class implementation in file IntCell.cpp.Cpt S 223. School of EECS, WSUPreprocessorDirectivesDirectivesDefault classProgram using IntCell class in file TestIntCell.cpp.11Cpt S 223. School of EECS, WSUC++ Details PointersParameter passingParameter passing Return passingfbl Reference variables Destructor, copy constructor, operator=12Cpt S 223. School of EECS, WSUPointersAddress-of operator &IntCell icObj;IntCell *m = & icObj;No garbage collection in C++13Cpt S 223. School of EECS, WSUParameter Passing Call by valueSmall objects not altered by functionSmall objects not altered by function Call by constant referenceLarge objects not altered by functionLarge objects not altered by function Call by reference Objects altered by functiondouble avg (const vector<int> & arr int n bool & errorFlag);14double avg (const vector<int> & arr, int n, bool & errorFlag);Cpt S 223. School of EECS, WSU15What’s wrong with this code?Return passingCpt S 223. School of EECS, WSUReference Variables As seen, can be used for parameter passingpassing Also used as synonyms for the objects they referencethey reference Avoid cost of copyingstring x = findMax (a);cout << x << endl;const string & x = findMax (a);cout << x << endl;16Cpt S 223. School of EECS, WSUDestructor Default definitions for all classesDestructorDestructor Called when object goes out of scope or subject to a delete By default, calls destructor on all data membersMi ht t tdlbj t t dMight want to delete objects created using newMight want to close any opened files17Might want to close any opened files.Cpt S 223. School of EECS, WSUCopy Constructor Copy constructor Declaration duringinitializationIntCellB=C;IntCell B = C; IntCell B (C); Object passed using call by value (instead of by &orconst &)or const &) Object returned by value (instead of by & or const & )Simple assignment for all members withSimple assignment for all members with primitive data types (e.g., int, double, …) Calls copy constructors on all member objects18py jCpt S 223. School of EECS, WSUoperator= Copy assignment operator: operator=Called when objects on both sides ofCalled when objects on both sides of assignment already constructed E.g.,IntCell B(0);g,IntCell C(1);B = C; By default, operator= called on each data member of objects19data member of objectsCpt S 223. School of EECS, WSU20Default destructor, copy constructor and operator= for IntCellCpt S 223. School of EECS, WSUProblems with Defaults21Cpt S 223. School of EECS, WSUProblems with DefaultsOt t?22Output?Cpt S 223. School of EECS, WSUFixing the Defaults23Cpt S 223. School of EECS, WSUTemplates Designing type-independent data structures and algorithmsstructures and algorithms Function templatesClass templatesClass templates24Cpt S 223. School of EECS, WSUFunction Templates25Cpt S 223. School of EECS, WSUFunction Templates26Cpt S 223. School of EECS, WSUClass Templates27Cpt S 223. School of EECS, WSUClass Templates28Cpt S 223. School of EECS, WSUC++ Standard Template Library: BasicsSGI’s web reference:SGIs web reference:http://www.sgi.com/tech/stl/29Cpt S 223. School of EECS, WSUKey concepts ContainersIteratorsIterators30Cpt S 223. School of EECS, WSUExample Problem Find the maximum in an (i) linked list of integers(i) linked list of integers (ii) array of integers31Cpt S 223. School of EECS, WSUCoding using STL Linked List Array*curr;*curr;Do you see the advantage of using iterators in the above example?32Do you see the advantage of using iterators in the above example?Cpt S 223. School of EECS, WSUList of containersSTL container: Data structure that it implements:vectorArrayvectorArraylistDoubly-linked listslistSingly-linked listgyqueueFIFO structurestackLIFO structuredequeArray-like structure with efficient insertion & removal at both endssetSet of unique elements33qCpt S 223. School of EECS, WSUAPI for the containersFunction: Purpose:push_front Inserts elements before the first (not available for vector)pop frontRemoves the first element (not available for vector)pop_frontRemoves the first element (not available for vector)push_back Appends element at the endpop_back Removes element from the endemptyBoolean indicating if the container is empty pyoo a d a g o a s p ysizeReturns number of elementsinsertInsert an element in a positioneraseRemoves an element at a positionclearRemoves all elementsresizeResizes the containerfrontReturns a reference to the first element34BackReturns a reference to the last element[] Subscripting access without bounds checkingatSubscripting access with bounds checkingCpt S 223. School of EECS, WSUSummary Basic C++TemplatesTemplates Tools for easing the design of type-independent data structures andindependent data structures and algorithmsPlease refer to
View Full Document