goals for today’s review

41
CSE 332: Midterm Review CSE 332 Midterm Review • Goals for today’s review – Review and summary of material in course so far • A chance to clarify and review key concepts/examples – Discuss details about the midterm exam • In class during the next class period (80 minutes) • One 8.5”x11” face of notes + pencils/pens allowed • All electronics must be off, including cell phones, etc. • Recommendations for exam preparation – Catch up on any studio exercises you’ve not done – Write up your notes page as you study – Ask questions here and on the message board

Upload: joshua-ellis

Post on 06-Jan-2018

217 views

Category:

Documents


1 download

DESCRIPTION

What 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 { public: typedef unsigned int UINT32; Simple (int i); void print_i (); void usage (); private: int i_; }; Definitions: static variable initialization, function implementation The part that turns into an executable program Usually kept in “source” (.cpp) files void Simple::print_i () { cout

TRANSCRIPT

Page 1: Goals for today’s review

CSE 332: Midterm Review

CSE 332 Midterm Review

• Goals for today’s review– Review and summary of material in course so far

• A chance to clarify and review key concepts/examples– Discuss details about the midterm exam

• In class during the next class period (80 minutes)• One 8.5”x11” face of notes + pencils/pens allowed• All electronics must be off, including cell phones, etc.

• Recommendations for exam preparation– Catch up on any studio exercises you’ve not done– Write up your notes page as you study– Ask questions here and on the message board

Page 2: Goals for today’s review

CSE 332: Midterm Review

What 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 { public: typedef unsigned int UINT32; Simple (int i); void print_i (); void usage ();private: int i_;};

• 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 compiler (or precompiler) to do something#include <vector>using namespace std;

Page 3: Goals for today’s review

CSE 332: Midterm Review

Writing a C++ Program

C++ source files(ASCII text) .cpp

Programmer(you)

emacseditor

C++ header files(ASCII text) .h

1 source file =

1 compilation unit

Makefile(ASCII text)

Also: .C .cxx .cc

Also: .H .hxx .hpp

readme(ASCII text)

EclipseVisual Studio

Page 4: Goals for today’s review

CSE 332: Midterm Review

Lifecycle of a C++ Program

C++ source code

Makefile

Programmer(you)

object code (binary, one per compilation unit) .o

make“make” utility

xterm

console/terminal/window

Runtime/utility libraries

(binary) .lib .a .dll .so

gcc, etc.compiler

linklinker

E-mail

executableprogram

Eclipse

debugger

precompiler

compiler

link

turnin/checkin

An “IDE”

WebCAT

Visual Studio

window

compile

Page 5: Goals for today’s review

CSE 332: Midterm Review

Using C++ vs. C-style Strings#include <string>#include <iostream>using namespace std;int main (int, char*[]){ char * w = “world”; string sw = “world”; char * h = “hello, ”; string sh = “hello, ”; cout << (h < w) << endl; // 0: why? cout << (sh < sw) << endl; // 1:why? h += w; // illegal: why? sh += sw; cout << h << endl; cout << sh << endl;

return 0;}

• C-style strings are contiguous arrays of char– Often accessed through

pointers to char (char *)• C++ string class (template)

provides a rich set of overloaded operators

• Often C++ strings do “what you would expect” as a programmer

• Often C-style strings do “what you would expect” as a machine designer

• Suggestion: use C++ style strings any time you need to change, concatenate, etc.

Page 6: Goals for today’s review

CSE 332: Midterm Review

C++ File I/O Stream Classes

#include <fstream>using namespace std;int main (){ ifstream ifs; ifs.open (“in.txt”); ofstream ofs (“out.txt”); if (ifs.is_open () && ofs.is_open ()) { int i; ifs >> i; ofs << i; } ifs.close (); ofs.close (); return 0;}

• <fstream> header file– Use ifstream for input– Use ofstream for output

• Other methods– open, is_open, close– getline– seekg, seekp

• File modes– in, out, ate, app, trunc, binary

Page 7: Goals for today’s review

CSE 332: Midterm Review

C++ String Stream Classes

#include <iostream>#include <fstream>#include <sstream>using namespace std;int main (int, char*[]){ ifstream ifs (“in.txt”); if (ifs.is_open ()) { string line_1, word_1; getline (ifs, line_1); istringstream iss (line_1); iss >> word_1; cout << word_1 << endl; } return 0;}

• <sstream> header file– Use istringstream for input– Use ostringstream for output

• Useful for scanning input– Get a line from file into string– Wrap string in a stream– Pull words off the stream

• Useful for formatting output– Use string as format buffer– Wrap string in a stream– Push formatted values into

stream– Output formatted string to file

Page 8: Goals for today’s review

CSE 332: Midterm Review

Parameter/Variable Declarations• Hint: read parameter and variable declarations right to left

int i; “i is an integer”int & r = i; “r is a reference to an integer (initialized with i)”int * p; “p is a pointer to an integer”int * & q = p; “q is a reference to a pointer to an integer (initialized with p)”

• Read function declarations inside out

“function main takes an integer and an array of pointers to char, and returns an integer”

int main (int argc, char * argv[]);

“function usage takes a pointer to char, and returns void (nothing)”void usage (char * program_name);

“function setstring takes a reference to a (C++) string, and returns void”void setstring (string & s);

Page 9: Goals for today’s review

CSE 332: Midterm Review

How Const Works in a Declaration• Making something const says that a value cannot be changed through it

int * p = 0; “p is a pointer to an integer”const int * q = 0; “q is a pointer to an integer that’s const”int const * r = 0; “r is a pointer to a const integer” (same as q, less common)int * const s = 0; “s is a const pointer to an integer” (similar to an array)const int * const t = 0; “t is a const pointer to an integer that’s const”

const int & u = i; “u is a reference to an integer that’s const”const int * & v = q; “v is a reference to a pointer to an integer that’s const”int * const & w = s; “w is a reference to a const pointer to an integer”

“x is a reference to a const pointer to an integer that’s const”const int * const & x = t;

• It’s ok for a const reference or pointer to access a non-const variable/object (but not the other way around)

const int i = 7; // value of i cannot be changedint j = 7; // value of j can be changedconst int & p = i; // p cannot be used to change iconst int & q = j; // q cannot be used to change jint & r = j; // r can be used to change j// cannot say int & s = i;

Page 10: Goals for today’s review

CSE 332: Midterm Review

What = and & Mean• In C++ the = symbol means either initialization or assignment

– If it’s used with a type declaration, it means initialization– If it’s used without a type declaration, it means assignmentint j(7); // j is initialized with value 7int k = 4; // k is initialized with value 4j = 3; // j is assigned value 3

• In C++ the & symbol also has a similar “dual nature”– If it’s used inside a type declaration, it means a reference (an alias)

• Arguments to function are always declared along with their types

– If it’s used outside a type declaration, it means “address of”int swap (int & i, int & j); // references to intint & s = j; // reference s initialized to refer to jint * p = & j; // pointer p initialized w/ j’s address

Page 11: Goals for today’s review

CSE 332: Midterm Review

Untangling Operator SyntaxSymbol Used in a

declarationUsed in a definition

unary & (ampersand)

reference, e.g.,int i; int &r = i;

address-of, e.g.,p = & i;

unary *(star)

pointer, e.g., int * p;

dereference, e.g.,* p = 7;

-> (arrow) member access via pointer, e.g.,C c; C * cp=&c; cp->add(3);

. (dot) member access via reference or object, e.g., C c; c.add(3); C & cr = c; cr.add(3);

Page 12: Goals for today’s review

CSE 332: Midterm Review

Review: What’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; // points to nothing

• Can change where it points– As long as pointer itself isn’t const – E.g., p = &i; // now points to i

0x7fffdad0

7

int i

int *p

Page 13: Goals for today’s review

CSE 332: Midterm Review

Review: What’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

0x7fffdad0

7

int i

int & r

Page 14: Goals for today’s review

CSE 332: Midterm Review

Aliasing and Pointers

int main (int argc, char *argv[]){ int i = 0; int j = 1; int * p = & i; int * q = & i; *q = 6; // i is now 6, j is still 1}

• Distinct variables have different memory locations– E.g., i and j

• A variable and all the pointers to it (when they’re dereferenced) all alias the same location– E.g., i, *p, and *q

• Assigning a new value to i, *p or *q changes value seen through the others

• But does not change value seen through j

0xefffdad06int i

int *p

1int j

0xefffdad0

int *q

Page 15: Goals for today’s review

CSE 332: Midterm Review

Pointers and Arraysint main (int argc, char **argv){ int arr [3] = {0, 1, 2};

int * p = & arr[0]; int * q = arr; // p, q, arr point to same place }

• An array holds a contiguous sequence of memory locations– Can refer to locations using either

array index or pointer notation– E.g., *arr vs. arr[0]– E.g., *(arr+1) vs. arr[1]

• Array variable essentially behaves like a const pointer– Like int * const arr;– Can’t change where it points– Can change locations unless

declared array-of-const– E.g., const int arr[3];

• Can initialize other pointers to the start of the array– Using array name, or using

address of 0th element

0xefffdad0

2int arr [3]

int *p

1

0xefffdad0int *q

0

Page 16: Goals for today’s review

CSE 332: Midterm Review

Pointer Arithmetic With Arrays

int main (int argc, char **argv){ int arr [3] = {0, 1, 2};

int * p = & arr[0]; int * q = arr; // p, q now point to same place ++q; // now q points to arr[1]}

• Adding or subtracting int n moves a pointer by n of the type to which it points– I.e., by n array positions– E.g., value in q is increased

by sizeof(int) by ++q• Can move either direction

– E.g., --q, ++p• Can jump to a location

– E.g., p+=2, q-=1• Remember that C++ (only)

guarantees that sizeof(char)==1– But compiler figures out

other sizes for you

0xefffdad0

2int arr [3]

int *p

1

0xefffdad0int *q

0

Page 17: Goals for today’s review

CSE 332: Midterm Review

Rules for Pointer Arithmetic

int 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 thing

0xefffdad0

2int arr [3]

int *p

1

0xefffdad0int *q

0

Page 18: Goals for today’s review

CSE 332: Midterm Review

Watch out for Pointer Arithmetic Errors

int main (int argc, char **argv){ int arr [3] = {0, 1, 2};

int * p = & arr[0]; int * q = arr; // p, q now point to same place int n; q+=n; // now where does q point?}

• Dereferencing a 0 pointer will crash your program

• Accessing memory location outside your program can– Crash your program– Let you read arbitrary values– Let you modify that location– Last two: hardest to debug

• Watch out for– Uninitialized pointers– Failing to check pointer for 0– Adding or subtracting an

uninitialized variable to a pointer

– Errors in loop initialization, termination, or increment

0xefffdad0

2int arr [3]

int *p

1

0xefffdad0int *q

0

Page 19: Goals for today’s review

CSE 332: Midterm Review

How Function Calls Work

• A function call uses the “program call stack”1. Stack frame is “pushed” when the call is made2. Execution jumps to the function’s code block3. Function’s code block is executed4. Execution returns to just after where call was made5. Stack frame is “popped” (variables in it destroyed)

• This incurs a (small) performance cost– Copying arguments, other info into the stack frame– Stack frame management– Copying function result back out of the stack frame

Page 20: Goals for today’s review

CSE 332: Midterm Review

Pass By Value

void foo (){ int i = 7; baz (i);}

void baz (int j){ j = 3;}

7

7 → 3

local variable i (stays 7)

parameter variable j (initialized with the value passed to baz, and then is assigned the value 3)

Think of this as declaration with initialization, along the lines of:int j = what baz was passed;

Page 21: Goals for today’s review

CSE 332: Midterm Review

Pass By Reference

void foo (){ int i = 7; baz (i);}

void baz (int & j){ j = 3;}

7 → 3 local variable i

j is initialized to refer to the variable thatwas passed to baz:when j is assigned 3,the passed variableis assigned 3.

7 → 3

again declarationwith initializationint & j = what baz was passed;

argument variable j

Page 22: Goals for today’s review

CSE 332: Midterm Review

Default Arguments• Watch out for ambiguous signatures

– foo(); and foo(int a = 2); for example

• Can only default the rightmost arguments– Can’t declare void foo(int a = 1, int b);

• Caller must supply leftmost arguments– Even if they’re the same as the defaults

Page 23: Goals for today’s review

CSE 332: Midterm Review

Overview of C++ Exceptions• Normal program control flow is halted

– At the point where an exception is thrown• The program call stack “unwinds”

– Stack frame of each function in call chain “pops”– Variables in each popped frame are destroyed– This goes on until an enclosing try/catch scope is

reached• Control passes to first matching catch block

– Can handle the exception and continue from there– Can free some resources and re-throw exception

Page 24: Goals for today’s review

CSE 332: Midterm Review

Rules of Thumb for C++ Exceptions• Use exceptions to handle any cases where the

program cannot behave normally• Put more specific catch blocks before more general• Don't let a thrown exception propagate out of main

– Instead, always catch any exceptions that propagate up– Then return a non-zero value to indicate program failure

• Do not use or rely on exception specifications– A false promise if you declare them, unless you have fully

checked all the code used to implement that interface– No guarantees that they will work for templates, because a

template parameter could leave them off and then throw

Page 25: Goals for today’s review

CSE 332: Midterm Review

Classes, Structs, and Access Permissions• Declaring access control scopes within a class

private: visible only within the classprotected: also visible within derived classes (more later)public: visible everywhere – Access control in a class is private by default

• but, it’s better style to label access control explicitly

• A struct is the same as a class, except – Access control for a struct is public by default– Usually used for things that are “mostly data”

• E.g., if initialization and deep copy only, may suggest using a struct

– Versus classes, which are expected to have both data and some form of non-trivial behavior

• E.g., if reference counting, etc. probably want to use a class

Page 26: Goals for today’s review

CSE 332: Midterm Review

Static Class Members

• Must define static members, usually outside of class– Initialized before any functions in

same compilation unit are called• Static member functions don’t

get implicit this parameter– Can’t see non-static class members– But non-static member functions

can see static members

class Date {public: // ... static void set_default(int,int,int);private: int _d, _m, _y; static Date default_date;};

Date::Date () : _d (default_date._d),

_m (default_date._m),_y (default_data._y)

{}

Date::operator= (const Date &d){ this->d_ = d.d_; this->m_ = d.m_; this->y_ = d.y_; }

Date Date::default_date(1, 1, 2004);

void Date::set_default(int m, int d, int y){

Date::default_date = Date(m, d, y);}

Page 27: Goals for today’s review

CSE 332: Midterm Review

Default and Copy Constructors• Default constructor takes no arguments

– Can supply default values via base/member list– Must do this for const and reference members– Compiler synthesizes one if no constructors are provided

• Does default construction of all class members (a.k.a member-wise)

• Copy constructor takes a reference to a class instance– Compiler provides one by default if you don’t

• Does (shallow) copy construction of all class members

• If you don’t want compiler to generate one of them– Declare private, don’t define, don’t use within class– But, it’s usually best do declare and define both of these

• Default / copy construction of built-in types – Default construction does nothing (leaves uninitialized)– Copy construction fills in the value given

Page 28: Goals for today’s review

CSE 332: Midterm Review

Destructors• Constructors initialize objects

– At start of object’s lifetime– implicitly called when object is

created (can also call explicitly)• Often want to make destructors

virtual– More on this when we discuss

inheritance• Destructors clean up afterward

– Compiler provides if you don’t• Does member-wise destruction

– Destructor is implicitly called when an object is destroyed

– Can make destructor private, call it from within a member function

• e.g., a public one called destroy() • Only allows heap allocation

class Calendar { public: Calendar (size_t s); virtual ~Calendar (); // etc ...

private: size_t size_; Date * dates_;};

Calendar::Calendar (size_t s) : size_(s), dates_(0) { if (size_ > 0) { dates_ = new Date[size_]; }}

Calendar::~Calendar () { delete [] dates_;}

Page 29: Goals for today’s review

CSE 332: Midterm Review

Assignment Operator• Compiler supplies if you don’t

– Does member-wise assignment• Similar to copy constructor

– But must deal with the existing values of object’s members

– And, no initialization list• Watch out for self-reference

– Assignment of an object to itself s = s; // perfectly legal syntax

• Efficiency, correctness issues• Watch out for correct aliasing

and copying cost trade-offs– Copying an int vs. an int *

vs. an int & vs. an int []– Can leverage copy constructor

to ensure safe reallocation– More on these issues

throughout the semester

class Date { public: Date & operator= (const Date &); // ...private: int d_, m_, y_;};

Date &Date::operator= (const Date &d){ d_ = d.d_; m_ = d.m_; y_ = d.y_; return *this;}

int main (int, char *[]){ Date a; // default constructor Date b(a); // copy constructor Date c = b; // copy constructor a = c; // assignment operator}

Page 30: Goals for today’s review

CSE 332: Midterm Review

Public, Protected, Private Inheritanceclass A {public: int i;protected: int j;private: int k;};

Class B : public A { // ...};Class C : protected A {// ...};Class D : private A {// ...};

• Class A declares 3 variables – i is public to all users of class A– j is protected. It can only be used by methods

in class A or its derived classes (+ friends) – k is private. It can only be used by methods in

class A (+ friends)

• Class B uses public inheritance from A– i remains public to all users of class B– j remains protected. It can be used by methods

in class B or its derived classes

• Class C uses protected inheritance from A– i becomes protected in C, so the only users of

class C that can access i are the methods of class C – j remains protected. It can be used by methods

in class C or its derived classes

• Class D uses private inheritance from A– i and j become private in D, so only methods of

class D can access them.

Page 31: Goals for today’s review

CSE 332: Midterm Review

Tips for Initialization and Destruction• Use base/member initialization list if you can

– To set pointers to zero, etc. before body is run – To initialize safe things not requiring a loop, etc.

• Do things that can fail in the constructor body– Rather than in the initialization list– For example, memory allocation, etc.

• Watch out for what can happen if an exception can leave a constructor– No guarantee destructor will be called in that case– Need to avoid having a partially initialized (“zombie”) object– Two kinds of approaches can be used (1st is often preferred)

• Keep the object in a safe default state at all times so it doesn’t matter• Use try/catch within the constructor and reset object to safe state

Page 32: Goals for today’s review

CSE 332: Midterm Review

Class and Member Construction Orderclass A {public: A(int i) :m_i(i) { cout << "A“ << endl;} ~A() {cout<<"~A"<<endl;}private: int m_i;};class B : public A {public: B(int i, int j) : A(i), m_j(j) { cout << “B” << endl;} ~B() {cout << “~B” << endl;}private:

int m_j;};int main (int, char *[]) { B b(2,3); return 0;}

• In the main function, the B constructor is called on object b– Passes in integer values 2 and 3

• B constructor calls A constructor– passes value 2 to A constructor via

base/member initialization list• A constructor initializes m_i

– with the passed value 2• Body of A constructor runs

– Outputs “A”• B constructor initializes m_j

– with passed value 3• Body of B constructor runs

– outputs “B”

Page 33: Goals for today’s review

CSE 332: Midterm Review

Class and Member Destruction Orderclass A {public: A(int i) :m_i(i) { cout << "A“ << endl;} ~A() {cout<<"~A"<<endl;}private: int m_i;};class B : public A {public: B(int i, int j) :A(i), m_j(j) { cout << “B” << endl;} ~B() {cout << “~B” << endl;}private:

int m_j;};int main (int, char *[]) { B b(2,3); return 0;}

• B destructor called on object b in main• Body of B destructor runs

– outputs “~B”• B destructor calls “destructor” of m_j

– int is a built-in type, so it’s a no-op• B destructor calls A destructor• Body of A destructor runs

– outputs “~A”• A destructor calls “destructor” of m_i

– again a no-op• Compare orders of construction and

destruction of base, members, body– at the level of each class, order of steps

is reversed in constructor vs. destructor– ctor: base class, members, body– dtor: body, members, base class

Page 34: Goals for today’s review

CSE 332: Midterm Review

C++ Polymorphism• Public inheritance creates sub-types

– Inheritance only applies to user-defined classes (and structs)– A publicly derived class is-a subtype of its base class– Known as “inheritance polymorphism”– Depends on dynamic typing

• Virtual member function/operator, base pointer/reference to derived object

• Template parameters also induce a subtype relation– Known as “interface polymorphism”– We’ll cover how this works in-depth after the midterm exam

• Liskov Substitution Principle (for both kinds of polymorphism)– if S is a subtype of T, then wherever you need a T you can use an S

Page 35: Goals for today’s review

CSE 332: Midterm Review

Static vs. Dynamic Type• The type of a variable is known

statically (at compile time), based on its declarationint i; int * p;Fish f; Mammal m;Fish * fp = &f;

• However, actual types of objects aliased by references & pointers to base classes vary dynamically (at run-time)Fish f; Mammal m;Animal * ap = &f;ap = &m;Animal & ar = get_animal();

Animal

Fish Mammal

• A base class and its derived classes form a set of typestype(*ap) {Animal, Fish,

Mammal}typeset(*fp) typeset(*ap)

• Each type set is open– More subclasses can be added

Page 36: Goals for today’s review

CSE 332: Midterm Review

Virtual Functionsclass A {public: void x() {cout<<"A::x";}; virtual void y() {cout<<"A::y";};};

class B : public A {public: void x() {cout<<"B::x";}; virtual void y() {cout<<"B::y";};};

int main () { B b; A *ap = &b; B *bp = &b; b.x (); // prints "B::x" b.y (); // prints "B::y" bp->x (); // prints "B::x" bp->y (); // prints "B::y" ap->x (); // prints "A::x" ap->y (); // prints "B::y" return 0;};

• Only matter with pointer or reference– Calls on object itself resolved statically– E.g., b.y();

• Look first at pointer/reference type– If non-virtual there, resolve statically

• E.g., ap->x();– If virtual there, resolve dynamically

• E.g., ap->y();• Note that virtual keyword need not be

repeated in derived classes– But it’s good style to do so

• Caller can force static resolution of a virtual function via scope operator– E.g., ap->A::y(); prints “A::y”

Page 37: Goals for today’s review

CSE 332: Midterm Review

Potential Problem: Class Slicing• Catch derived exception types by reference• Also pass derived types by reference• Otherwise a temporary variable is created

– Loses original exception’s “dynamic type”– Results in “the class slicing problem” where only the

base class parts and not derived class parts copy

Page 38: Goals for today’s review

CSE 332: Midterm Review

What’s a Design Pattern?• A design pattern has a name

– So when someone says “Adapter” you know what they mean– So you can communicate design ideas as a “vocabulary”

• A design pattern describes the core of a solution to a recurring design problem– So you don’t have to reinvent known design techniques– So you can benefit from others’ (and your) prior experience

• A design pattern is capable of generating many distinct design decisions in different circumstances– So you can apply the pattern repeatedly as appropriate– So you can work through different design problems using it

Page 39: Goals for today’s review

CSE 332: Midterm Review

Iterator Pattern• Problem

– Want to access aggregated elements sequentially• E.g., traverse a container of values, objects etc. and print them out

• Context– Don’t want to know/manage details of how they’re stored

• E.g., could be in an array, list, vector, or deque

• Solution core– Provide a common interface for iteration over a container:

(1) start; (2) access; (3) increment; (4) termination• Consequences

– Frees user from knowing details of how elements are stored– Decouples containers from algorithms (crucial in C++ STL)

• Examplelist<int>::iterator

Page 40: Goals for today’s review

CSE 332: Midterm Review

Factory Method Pattern• Problem

– You want a type to create a related type polymorphically• E.g., a container should create appropriate begin and end iterators

• Context– Each type knows which related type it should create

• Solution core– Polymorphic creation

• E.g., abstract method that different types override• E.g., provide traits and common interface (as in the STL we’ll use)

• Consequences– Type that’s created matches type(s) it’s used with

• Examplevector<double> v; vector<double>::iterator i = v.begin();

Page 41: Goals for today’s review

CSE 332: Midterm Review

CSE 332 Midterm

• Held during next class period (80 minutes)– Please see course web site for the room location – One 8.5”x11” face of notes + pencils/pens allowed– Electronics must be off, including cell phones, etc.

• Recommendations for exam preparation– Catch up on any studio exercises you’ve not done– Write up your notes page as you study– Ask questions you may have on the message board