DOC PREVIEW
WSU CPTS 223 - Lecture Notes

This preview shows page 1-2-16-17-18-34-35 out of 35 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

C++ ReviewC++ Review11Cpt S 223. School of EECS, WSUPurpose of Review Review some basic C++Familiarize us with Weiss’sstyleFamiliarize 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) fileDefines class and its member data andDefines class and its member data and functionsImplementation ( cc or cpp) fileImplementation (.cc or .cpp) file Provides implementations of member functionsfunctions6Cpt S 223. School of EECS, WSUExtra Syntax Default parametersInitializer listInitializer 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 PointersParameter passingParameter 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 valueSmall objects not altered by functionSmall objects not altered by function Call by constant referenceLarge objects not altered by functionLarge 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 classesDestructorDestructor 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 dMight want to delete objects created using newMight want to close any opened files17Might want to close any opened files.Cpt S 223. School of EECS, WSUCopy Constructor Copy constructor Declaration duringinitializationIntCellB=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 withSimple 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 ofCalled 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 templatesClass 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 ContainersIteratorsIterators30Cpt 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++TemplatesTemplates Tools for easing the design of type-independent data structures andindependent data structures and algorithmsPlease refer to


View Full Document

WSU CPTS 223 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?