1 / 38

CST223 Week 6 Monday

CST223 Week 6 Monday. Questions? Hand back Midterm Extra Credit Opportunity: CSET Colloquium Talks May 23 rd 4-5:30pm, PV206 (Priscilla Oppenheimer, Cisco Systems) May 30 th 4-5:30pm, PV206 (David Lowe, xtranormal.com) Homework #2 (chapter 5) due on Wednesday in class.

redford
Download Presentation

CST223 Week 6 Monday

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CST223 Week 6 Monday • Questions? • Hand back Midterm • Extra Credit Opportunity: CSET Colloquium Talks • May 23rd 4-5:30pm, PV206 (Priscilla Oppenheimer, Cisco Systems) • May 30th 4-5:30pm, PV206 (David Lowe, xtranormal.com) • Homework #2 (chapter 5) due on Wednesday in class. • Homework #3 (chapter 6) due on Monday, May 14th • Discuss Final Project & Sign-Up • Functional Language vs. Applicative Language • Chapter 5: Names & Scoping

  2. Functional ≠ Recursion • Recursion is just one of the ways to apply the function to all elements. • Functional languages like Lisp, Scheme, Haskell use recursion as the fundamental control structure. • A variation of functional languages called Applicative languages don’t use recursion. • “Apply to all” or “Apply to a range” is implied. • C++’s built-in STL algorithms are applicative functions.

  3. CST223 Week 6 Wednesday • Questions? • Extra Credit Opportunity: • Project Symposium: May 21st • Talk: May 23rd 4-5:30pm, PV206 (Priscilla Oppenheimer, Cisco Systems) • Talk: May 30th 4-5:30pm, PV206 (David Lowe, xtranormal.com) • Final Project Sign-up • Schedule changes • Lab5 – F# -in-class exercise #6 • Lab6 – GameMaker comments • Homework #2 (chapter 5) due today • Homework #3 (chapter 6) due on Monday in class. • Topics: • Overview of Chapter 6: Data Types • Smart Pointers for C++

  4. Schedule Changes • Lab5 deadline has been extended to next Tuesday, May 15th. • Extra credit for those who have already completed. • Lab6 deadline has been extended to Tuesday, May 22nd. • Extra credit if done by next Tuesday, May 15th. • Lab7 will not be assigned until Week 8. • Please work on your final project during Week 7 if you are already done with Lab6. • Final Project Presentations start on Wednesday, May 30, and will continue during dead week (June 4 & June 6)

  5. In-Class Exercise #6 • Using this function: let divides m n = if m%n=0 then true else false • Write a F# function to remove all multiples of n from a list: let recremoveMultiple n list • Example: removeMultiple 2 [1;2;3;4;5] => [1;3;5]

  6. let divides m n = if m%n=0 then true else false let recremoveMultiple n list = match list with | [] -> [] | head::tail-> if (divides head n) then (removeMultiple n list.Tail) else head::(removeMultiple n list.Tail)

  7. Parameter(s) F# Function Return Values

  8. countAll‘X’ [‘X’;’Y’;’X’] let reccountAll x list = match list with | [] -> 0 | head::tail -> if head=x then 1 + …. else 2

  9. reverse [1;2;3] let rec reverse list = [] @ [3;2;1]

  10. replace [‘-’;’-’;’X’] 2 ‘O’ let rec replace l ist x pos = :: //use :: to build the list back up [‘-’;’O’;’X’]

  11. GameMaker • Not really a general-purpose programming language. • Interpreted not compiled.

  12. Scoping of variables • Local { vari, j; }

  13. Scoping of variables • Instance { vari, j; …. field[I, j] = 0; } Variables are dynamically allocated, dynamically typed and dynamically type-checked (it knows when an array subscript is out of range)

  14. When an object invokes a script, the scope of variables in the script is dynamically bound

  15. Scoping of variables • global { var I, j; …. global.field[I, j] = 0; } Global variables are available to all object instances

  16. Initialization in C++98 Containers require another container: int vals[]={10, 20, 30}; //init from another container const vector<int> cv(vals, vals+3); Member and heap arrays are impossible: class Widget { public: Widget(): data(???){} private: const int data[5]; //init? } const float *pData=new const float[4];//init?

  17. New Brace Initialization Syntax const int val1 {5}; const int val2 {5}; int a[] {1, 2, val1, val1+val2}; const Point p1 {10, 20}; const Point2 p2 {10, 20}; const vector<int> cv {a[0], 20, val2}; class Widget { public: Widget():data{1, 2, a[3], 4, 5}{} private: const int data[5]; }; const float * pData = new const float[4] {1.5, val1-val2, 3.5, 4.6};

  18. Uniform Initialization Syntax • You can use it everywhere: Point2 makePoint() { return {0, 0}; } //return expression;calls Point2 ctor void f(const vector<int>& v); f({val1, val2, 10, 20, 30});

  19. Uniform Initialization Syntax • Semantics differ for aggregates and non-aggregates: • Aggregates (e.g. arrays and structs) • Initialize members/elements beginning to end • Non-aggregates: • Invoke a constructor.

  20. Brace-Initializing Aggregates • Initialize members/elements from beginning to end: • Too many initializers => error • Too few initializers => remaining objects are value-initialized: • Built-in types initialized to 0. • User-defined types with constructors are default-constructed. • UDTs without constructors: members are value-initialized. struct Point1 {int x,y;}; const Point1 p1 = {10}; //same as (10, 0) const Point1 p2 = {1, 2, 3}; //Error Std::array<long, 3> larr = {1, 2, 4, 5}; //Error

  21. Brace-initializing non-aggregates • Invoke a constructor: class Point2 { public: Point2(int x, int y);}; int a,b; const Point2 p1{a, b}; //same as p1(a, b) const Point2 p2{10}; //error, too few args const Point2 p3{5, 10, 20}; //error,too many args vector<int> v {1,a,2,b,3}; //calls vector’s ctor

  22. Uniform Initialization Syntax • Use of “=“ with brace initialization typically allowed: const int val1 = {5}; const int val2 = {5}; int a[] = {1, 2, val1, val2}; struct Point1 {…}; const Point1 p1 = {10, 20}; class Point2 {…}; const Point2 p2 = {10, 20}; const vector<int> cv = {1, 2, 3};

  23. Uniform Initiazlization Syntax • But not always: class Widget { public: Widget(): data = {1, 2, 3, 4, 5} {} //error private: const int data[5]; }; const float *pData = new const float[4] = {1.5, 2.0, 3.0, 4.6}; //error Point2 makePoint() { return = {0, 0}; } //error

  24. Uniform Initialization Syntax • And “T var = expr” syntax can’t call explicit constructors: class Widget { public: explicit Widget(int); …}; Widget w1(10); //okay, direct init Widget w2{10}; //ditto Widget w3 = 10;//error, because of explicit Widget w4 = {10};//ditto • Develop the habit of using brace initialization without “=“

  25. Uniform Initialization Syntax • Uniform initialization syntax a feature addition, not a replacement. • Almost all initialization code valid in C++98 remains valid. Rarely a need to modify existing code. • Sole exception: implicit narrowing. • C++98 allows it via brace initialization, C++0x doesn’t: struct Point {int x, y;}; Point p1 {1.2, 5}; //Okay in C++98, but //error in C++0x Point p2 {1, static_cast<int>(2.5)}; //Okay in both

  26. Uniform Initialization Syntax • Direct constructor calls and brace initialization thus differ subtly: class Widget { public: Widget(unsigned u);…}; int i; unsigned u; Widget w1(i); //Okay Widget w2{i}; //error Widget w3(u); //Okay Widget w4{u}; //Okay

  27. Uniform Initialization Syntax • A mechanism to generalize array aggregate initialization: • Available to all user-defined types int x, y; int a[] {x, y, 7, 22, -13, 44}; vector<int> v {99, -8, x-y}; myType w {a[0], a[1], 25, 6}; • Available for more than just initialization, e.g. vector<int> v {}; //init v = {1, 2, 3}; //assignment v.assign({1, 2, 3}); //assign v.insert(v.end(), {99, 88, -1}); => Any function can use an “initializer” list.

  28. Uniform Initialization Syntax • Approach startlingly simple: • Brace initialization lists convertible to std::initializer_list objects. • Functions can declare parameters of this type. • std::initializer_list stores initializer values in an array and offer these member functions: • Size • begin • end

  29. Initializer Lists #include <initializer_list> //in std namespace string getName(int ID); Class Widget { public: Widget(initializer_list<int> il) { values.reserve(il.size()); for (auto v:il) values.push_back(getName(v)); } private: vector<string> values; }; Widget w {1, x, 25, 16};

  30. Initializer Lists • std::intializer_list parameter may be used with other parameters: class Widget { public: Widget(string& name, double d, initializer_list<int> il); …}; string name(“Buffy”); Widget w {name, 0.5, {5, 10, 15}}; =>Note the nested brace sets.

  31. Initializer Lists • They may be templatized: • Only homogeneous initializer lists allow type deduction to succeed: class Widget { public: template<typename T> Widget(initializer_list<T> il); …}; Widget w1 {-55, 25, 16}; // T = int Widget w2 {-55, 2.5, 16}; //Error

  32. Initializer List and Overload Resolution • When resolving constructor calls, initializer_list parameters are preferred for brace-deliminted arguments: class Widget { public: Widget(double v1, double v2); //#1 Widget(initializer_list<double> vs); //#2 ..}; double d1, d2; Widget w {d1, d2}; //calls #2

  33. Initializer List and Overload Resolution • initializer_list parameters are always preferred over other types class Widget { public: Widget(double v1, double v2); //#1 Widget(initializer_list<string> ss); //#2 ..}; double d1, d2; Widget w {d1, d2}; //tried to call #2 but //failed. Call #1

  34. Initializer List and Overload Resolution • Given multiple initializer_list candidates, best match is determined as long as it’s not a narrowing conversion: class Widget { public: Widget(initializer_list<int>); //#1 Widget(initializer_list<double>); //#2 Widget(initializer_list<string>); //#3 ..}; Widget w2 {1,0f, 2.0, 3.0}; //calls #2, float=>double string s; Widget w3 {s, “Init”, “lists”}; //calls #3 Widget w4 {1, 2.0, 3}; //Error if #2 if not //available

  35. Uniform Initialization Summary • Brace initialization syntax now available everywhere. • Implicit narrowing not allowed. • std::intializer_list parameters allow “initialization” lists to be passed to functions. • Not actually limited to initialization (e.g. vector::assign)

  36. C’s array Row Major inttwoD [3][4]; 0 1 2 4 5 6 7 8 9 10 11 12 How to find twoD[i][j] (eg. [2][3])?

  37. “ROW Major” Store the first index first intthreeD[2][3][4]; 0 1 2 3 4 5 6 7 8 9 10 11 3 12 13 14 15 2 1 16 17 18 19 0 4 8 12 16 20 20 21 22 23

  38. intthreeD[2][3][4]; How to find threeD[i][j][k] (eg. [1][2][3])? “Column Major” Store each slice/plane first 0 2 4 1 3 5

More Related