


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





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.









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++

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)
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 rec removeMultiple n list
Example:
removeMultiple 2 [1;2;3;4;5] => [1;3;5]
let divides m n = if m%n=0 then true else false
let rec removeMultiple n list =
match list with
| [] -> []
| head::tail-> if (divides head n) then (removeMultiple n list.Tail) else
head::(removeMultiple n list.Tail)
Parameter(s)
F# Function
Return Values
countAll ‘X’ [‘X’;’Y’;’X’]
let rec countAll x list =
match list with
| [] -> 0
| head::tail ->
if head=x then 1 + ….
2
else
reverse [1;2;3]
let rec reverse list =
[]
@
[3;2;1]
replace [‘-’;’-’;’X’] 2 ‘O’
let rec replace l ist x pos =
::
//use :: to build the list back up
[‘-’;’O’;’X’]


Not really a general-purpose programming
language.
Interpreted not compiled.

Local
{
}
var i, j;

Instance
{
var i, j;
….
field[I, j] = 0;
}
Variables are dynamically allocated,
dynamically typed and dynamically typechecked (it knows when an array subscript
is out of range)
When an object invokes a script, the scope of variables in the script is
dynamically bound

global
{
var I, j;
….
global.field[I, j] = 0;
}
Global variables are available to all object
instances
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?
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};
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});
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.
Brace-Initializing Aggregates
 Initialize members/elements from beginning to end:
 Too many initializers => error
 Too few initializers => remaining objects are valueinitialized:
 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
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
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};
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
Uniform Initialization Syntax
 And “T var = expr” syntax can’t call explicit
constructors:
class Widget { public:
explicit Widget(int);
…};
Widget
Widget
Widget
Widget
w1(10);
//okay, direct init
w2{10};
//ditto
w3 = 10; //error, because of explicit
w4 = {10}; //ditto
 Develop the habit of using brace initialization
without “=“
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
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);
Widget w2{i};
Widget w3(u);
Widget w4{u};
//Okay
//error
//Okay
//Okay
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.
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
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};
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.
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
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
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
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>);
Widget(initializer_list<double>);
Widget(initializer_list<string>);
..};
Widget
string
Widget
Widget
//#1
//#2
//#3
w2 {1,0f, 2.0, 3.0};
//calls #2, float=>double
s;
w3 {s, “Init”, “lists”}; //calls #3
w4 {1, 2.0, 3};
//Error if #2 if not
//available
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)
C’s array
int twoD [3][4];
Row Major
0
1
2
4
5
6
7
8
9
10
How to find twoD[i][j] (eg. [2][3])?
11
12
“ROW Major”
Store the first index first
int threeD[2][3][4];
2
3
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1
0
4
12 16
8
20
int threeD[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
Descargar

Today’s Topics - Oregon Institute of Technology