CSE 332 Course ReviewWhat Goes Into a C++ Program?Lifecycle of a C++ ProgramWhat’s a Reference?R-Values, L-Values, and References to EitherWhat’s a Pointer?Rules for Pointer ArithmeticC++ Input/Output Stream ClassesExpressions: Operators and OperandsC++ StatementsC++ Exceptions Interrupt Control FlowDetails on Catching C++ ExceptionsPass by ValuePass by ReferenceC++ Memory OverviewOperators new and delete are InversesC++11 Smart PointersC++ Class StructureAccess ControlInitialization and Destruction with InheritanceVirtual FunctionsMultiple InheritanceAll Base Class Constructors are CalledBase Pointer/Reference Type Restricts InterfaceRun-Time Type IdentificationPointer to Data MemberPointer to Member FunctionCopy ControlOperator OverloadingSequence Containers (e.g. vector)Associative ContainersIteratorsKey Ideas: Concepts and ModelsIterator Concept HierarchyCan Extend STL Algorithms with Callable ObjectsCallable Objects and AdaptersFunction TemplatesClass TemplatesConcept Refinement Motivates OverridingFully Specialize to Override Function TemplatesOverview of C++ Specialized Library FacilitiesCSE 332 Final ExamsCSE 332: Course ReviewCSE 332 Course Review•Goals for today’s review–Review and summary of material this semester•A chance to clarify and review key concepts/examples–Discuss details about the final exams•Please see course web site for exam times and locations•One 8.5”x11” page (with notes on 1 or 2 sides) allowed•All electronics must be off, including cell phones, etc. •Recommendations for exam preparation–Catch up on any studios/readings you’ve not done–Write up your notes page as you study–Ask questions here as you have them todayCSE 332: Course ReviewWhat Goes Into a C++ Program?•Declarations: data types, function signatures, classes–Allows the compiler to check for type safety, correct syntax–Usually kept in “header” (.h) files–Included as needed by other files (to keep compiler happy)class Simple { typedef unsigned int UINT32; public: Simple (int i); int usage (char * program_name); void print_i (); private: struct Point2D { int i_; double x_;}; double y_; };•Definitions: static variable initialization, function implementation–The part that turns into an executable program –Usually kept in “source” (.cpp) filesvoid Simple::print_i () {cout << “i_ is ” << i_ << endl;} •Directives: tell precompiler or compiler to do something–E.g., #include <vector> or using namespace std;CSE 332: Course ReviewLifecycle of a C++ ProgramC++ source codeMakefileProgrammer(you)object code (binary, one per compilation unit) .omake“make” utilityxtermconsole/terminal/windowRuntime/utility libraries(binary) .lib .a .dll .sogcc, etc.compilerlinklinkerE-mailexecutableprogramEclipsedebuggerprecompilercompilerlinkturnin/checkinAn “IDE”WebCATVisual StudiowindowcompileCSE 332: Course ReviewWhat’s a Reference?•Also a variable holding an address–Of what it “refers to” in memory•But with a nicer interface–A more direct alias for the object–Hides indirection from programmers•Must be typed –Checked by compiler–Again can only refer to the type with which it was declared–E.g., int & r =i; // refers to int i•Always refers to (same) something–Must initialize to refer to a variable–Can’t change what it aliases 0x7fffdad07int iint & rCSE 332: Course ReviewR-Values, L-Values, and References to Either•A variable is an l-value (has a location)–E.g., int i = 7; •Can take a regular (l-value) reference to it–E.g., int & lvri = i; •An expression is an r-value–E.g., i * 42 •Can only take an r-value reference to it (note syntax)–E.g., int && rvriexp = i * 42; •Can only get r-value reference to l-value via move–E.g., int && rvri = std::move(i); –Promises that i won’t be used for anything afterward–Also, must be safe to destroy i (could be stack/heap/global)CSE 332: Course ReviewWhat’s a Pointer?•A variable holding an address–Of what it “points to” in memory•Can be untyped–E.g., void * v; // points to anything•However, usually they’re typed –Checked by compiler–Can only be assigned addresses of variables of type to which it can point–E.g., int * p; // only points to int•Can point to nothing– E.g., p = 0; // or p = nullptr; (C++11)•Can change where it points–As long as pointer itself isn’t const –E.g., p = &i; // now points to i0x7fffdad07int iint *pCSE 332: Course ReviewRules for Pointer Arithmeticint main (int argc, char **argv){ int arr [3] = {0, 1, 2}; int * p = & arr[0]; int * q = p + 1; return 0;} •You can subtract (but not add, multiply, etc.) pointers–Gives an integer with the distance between them•You can add/subtract an integer to/from a pointer–E.g., p+(q-p)/2 is allowed but (p+q)/2 gives an error •Note relationship between array and pointer arithmetic –Given pointer p and integer n, the expressions p[n] and *(p+n) are both allowed and mean the same thing0xefffdad02int arr [3]int *p10xefffdad0int *q0CSE 332: Course ReviewC++ Input/Output Stream Classes#include <iostream>using namespace std;int main (int, char*[]){ int i; // cout == std ostream cout << “how many?” << endl; // cin == std istream cin >> i; cout << “You said ” << i << “.” << endl; return 0;}•E.g., <iostream> etc.–Use istream for std input–Use ostream for std output–Use ifstream and ofstream for file input and output–Use istringstream and ostringstream for strings•Overloaded operators<< ostream insertion operator>> istream extraction operator•Other methods–ostream: write, put–istream: get, eof, good, clear•Stream manipulators–ostream: flush, endl, setwidth, setprecision, hex, boolalphaCSE 332: Course ReviewExpressions: Operators and Operands•Operators obey arity, associativity, and precedenceint result = 2 * 3 + 5; // assigns 11•Operators are often overloaded for different typesstring name = first + last; // concatenation•An lvalue gives a location; an rvalue gives a value–Left hand side of an assignment must be an lvalue–Prefix increment and decrement take and produce lvalues–Posfix versions (and &) take lvalues, produce rvalues•Beware accidentally using the “future equivalence” operator, e.g., if (i = j) instead of if (i == j)•Avoid type conversions if you can, and only use named casts (if you must explicitly
View Full Document