C++ ReviewPurpose of ReviewClassSlide Number 4Slide Number 5Extra SyntaxSlide Number 7Separation of Interface and ImplementationSlide Number 9Slide Number 10Slide Number 11C++ DetailsPointersParameter PassingSlide Number 15Reference VariablesDestructor, Copy Constructor, operator=Destructor, Copy Constructor, operator=Destructor, Copy Constructor, operator=Slide Number 20Problems with DefaultsProblems with DefaultsFixing the DefaultsTemplatesFunction TemplatesFunction TemplatesOperator OverloadingClass TemplatesClass TemplatesSummary11C++ ReviewCptS 223 – Advanced Data StructuresLarry HolderSchool of Electrical Engineering and Computer ScienceWashington State University2Purpose of Review Review some basic C++ Familiarize us with Weiss’s style Introduce specific constructs useful for implementing data structures33Class The Class defines the data structure and the operations that access and manipulate it Member data Member functions or methods Encapsulation = data + methods Information hiding Public vs. private4ConstructorConstructorComment5InformationHidingEncapsulation6Extra Syntax Default parameters Initializer list Explicit constructor Constant member function Accessor methods Mutator methods7Default ParametersInitializerListExplicitConstructorAccessorIntCell obj;obj = 37;8Separation of Interface and Implementation Interface (.h) file Defines class and its member data and functions Implementation (.cc or .cpp) file Provides implementations of member functions9IntCell class interface in file IntCell.h.PreprocessorDirectives10PreprocessorDirectiveScopingOperatorClassName::memberIntCell class implementation in file IntCell.cpp.11Program using IntCell class in file TestIntCell.cpp.PreprocessorDirectivesDefault class12C++ Details Pointers Parameter passing Return passing Reference variables Destructor, copy constructor, operator=13Address-of operator &IntCell icObj;IntCell *m = & icObj;No garbage collection in C++Pointers14Parameter Passing Call by value Small objects not altered by function Call by constant reference Large objects not altered by function Call by reference Objects altered by functiondouble avg (const vector<int> & arr, int n, bool & errorFlag);15Return PassingMake sure the object returned will persist after returning from the function call.16Reference Variables As seen, can be used for parameter passing Also used as synonyms for the objects they reference Avoid cost of copyingstring x = findMax (a);cout << x << endl;const string & x = findMax (a);cout << x << endl;17Destructor, Copy Constructor, operator= Default definitions for all classes Destructor Called when object goes out of scope or subject to a delete By default, calls destructor on all data members Might want to delete objects created using new Might want to close any opened files.18Destructor, Copy Constructor, operator= Copy constructor Declaration with initialization IntCell B = C; IntCell B (C); Object passed using call by value (instead of by &or const & ) Object returned by value (instead of by & or const & ) Simple assignment for all members with primitive data types (e.g., int, double, …) Calls copy constructors on all member objects19Destructor, Copy Constructor, operator= Copy assignment operator: operator= Called when objects on both sides of assignment already constructed E.g., By default, operator= called on each data member of objectsIntCell B(0);IntCell C(1);B = C;20Default destructor, copy constructor and operator= for IntCell21Problems with Defaults22Problems with DefaultsOutput?23Fixing the Defaults24Templates Designing type-independent data structures and algorithms Function templates Class templates25Function Templates26Function Templates27Operator Overloading Define the meaning of a built-in operatorclass IntCell{public:bool operator< (const IntCell & rhs) const{ return storedValue < rhs.storedValue; }void print (ostream & out) const{ out << “ IntCell(” << storedValue << “)”; }...}ostream & operator<< (ostream & out, const IntCell & rhs){ rhs.print (out); return out; }28Class TemplatesZero may not be a valid Object29Class Templates30Summary Basic C++ Templates Tools for easing the design of type-independent data structures and
View Full Document