Goals for today’s review

Slides:



Advertisements
Similar presentations
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Advertisements

CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CSE 332: C++ classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
CSE 332: C++ copy control I Copy Control (Part I) Copy control consists of 5 distinct operations –A copy constructor initializes an object by duplicating.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
CSE 332: C++ functions Review: What = and & Mean In C++ the = symbol means either initialization or assignment –If it’s used with a type declaration, it.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
Defining New Types Lecture 21 Hartmut Kaiser
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 5 An Array Class Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
C++ Memory Overview 4 major memory segments Key differences from Java
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Navigating the C++ Development Environment
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
CSE 332: C++ data types, input, and output Built-In (a.k.a. Native) Types in C++ int, long, short, char (signed, integer division) –unsigned versions too.
Overview of C++ Polymorphism
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
CHAPTER 18 C – C++ Section 1: Exceptions. Error Handling with Exceptions Forces you to defend yourself Separates error handling code from the source.
C++ Functions A bit of review (things we’ve covered so far)
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Motivation for Generic Programming in C++
Overview 4 major memory segments Key differences from Java stack
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Motivation and Overview
Inheritance & Polymorphism
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Overview 4 major memory segments Key differences from Java stack
Overview of Memory Layout in C++
Built-In (a.k.a. Native) Types in C++
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
Overview of C++ Polymorphism
SPL – PS3 C++ Classes.
Presentation transcript:

Goals for today’s 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

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 << “i_ is ” << i_ << endl; } Directives: tell compiler (or precompiler) to do something #include <vector> using namespace std;

Writing a C++ Program Visual Studio Eclipse emacs Makefile (ASCII text) emacs editor 1 source file = 1 compilation unit C++ source files (ASCII text) .cpp Also: .C .cxx .cc Programmer (you) Also: .H .hxx .hpp C++ header files (ASCII text) .h readme (ASCII text)

Lifecycle of a C++ Program xterm An “IDE” window console/terminal/window Makefile WebCAT make turnin/checkin E-mail “make” utility Runtime/utility libraries (binary) .lib .a .dll .so Eclipse compile link Visual Studio C++ source code Programmer (you) debugger precompiler gcc, etc. link compiler linker executable program compiler object code (binary, one per compilation unit) .o

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.

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

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 Push formatted values into stream Output formatted string to file

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);

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 changed int j = 7; // value of j can be changed const int & p = i; // p cannot be used to change i const int & q = j; // q cannot be used to change j int & r = j; // r can be used to change j // cannot say int & s = i;

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 assignment int j(7); // j is initialized with value 7 int k = 4; // k is initialized with value 4 j = 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 int int & s = j; // reference s initialized to refer to j int * p = & j; // pointer p initialized w/ j’s address

Untangling Operator Syntax Symbol Used in a declaration Used 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);

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 int i 7 0x7fffdad0 int *p

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 int i 7 0x7fffdad0 int & r

Aliasing and Pointers 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 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 } int *p 0xefffdad0 6 int i int *q 0xefffdad0 1 int j

Pointers and Arrays int 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 int arr [3] 1 2 0xefffdad0 0xefffdad0 int *p int *q

Pointer Arithmetic With Arrays 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 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] } int arr [3] 1 2 0xefffdad0 0xefffdad0 int *p int *q

Rules for Pointer Arithmetic 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 int main (int argc, char **argv) { int arr [3] = {0, 1, 2}; int * p = & arr[0]; int * q = p + 1; return 0; } int arr [3] 1 2 0xefffdad0 0xefffdad0 int *p int *q

Watch out for Pointer Arithmetic Errors 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 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? } int arr [3] 1 2 0xefffdad0 0xefffdad0 int *p int *q

How Function Calls Work A function call uses the “program call stack” Stack frame is “pushed” when the call is made Execution jumps to the function’s code block Function’s code block is executed Execution returns to just after where call was made 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

Pass By Value void foo () { int i = 7; baz (i); } void baz (int j) local variable i (stays 7) Think of this as declaration with initialization, along the lines of: int j = what baz was passed; parameter variable j (initialized with the value passed to baz, and then is assigned the value 3) 7 → 3

Pass By Reference void foo () { int i = 7; baz (i); } again declaration with initialization int & j = what baz was passed; 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 that was passed to baz: when j is assigned 3, the passed variable is assigned 3. 7 → 3 argument variable j

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

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

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

Classes, Structs, and Access Permissions Declaring access control scopes within a class private: visible only within the class protected: 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

Static Class Members Date Date::default_date(1, 1, 2004); void Date::set_default(int m, int d, int y) { Date::default_date = Date(m, d, y); } 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_; } 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

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

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_;

Assignment Operator Compiler supplies if you don’t 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 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

Public, Protected, Private Inheritance class 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.

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

Class and Member Construction Order public: A(int i) :m_i(i) { cout << "A“ << endl;} ~A() {cout<<"~A"<<endl;} private: int m_i; }; class B : public A { B(int i, int j) : A(i), m_j(j) { cout << “B” << endl;} ~B() {cout << “~B” << endl;} 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”

Class and Member Destruction Order public: A(int i) :m_i(i) { cout << "A“ << endl;} ~A() {cout<<"~A"<<endl;} private: int m_i; }; class B : public A { B(int i, int j) :A(i), m_j(j) { cout << “B” << endl;} ~B() {cout << “~B” << endl;} 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

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

Static vs. Dynamic Type The type of a variable is known statically (at compile time), based on its declaration int 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) Animal * ap = &f; ap = &m; Animal & ar = get_animal(); Animal Fish Mammal A base class and its derived classes form a set of types type(*ap)  {Animal, Fish, Mammal} typeset(*fp)  typeset(*ap) Each type set is open More subclasses can be added

Virtual Functions Only matter with pointer or reference class A { public: void x() {cout<<"A::x";}; virtual void y() {cout<<"A::y";}; }; class B : public A { 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”

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

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

Iterator Pattern Problem Context Solution core Consequences Example 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) Example list<int>::iterator

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 Example vector<double> v; vector<double>::iterator i = v.begin();

Held during next class period (80 minutes) 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