Presentation is loading. Please wait.

Presentation is loading. Please wait.

Goals for today’s review

Similar presentations


Presentation on theme: "Goals for today’s review"— Presentation transcript:

1 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

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

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

4 Lifecycle of a C++ Program
xterm An “IDE” window console/terminal/window Makefile WebCAT make turnin/checkin “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

5 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.

6 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

7 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

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

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

10 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

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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

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

29 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

30 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.

31 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

32 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”

33 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

34 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

35 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

36 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”

37 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

38 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

39 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

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

41 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


Download ppt "Goals for today’s review"

Similar presentations


Ads by Google