cs 4/69995 cs iii: programming patterns

19
CS 44001/54001 CS III: Programming Patterns Mikhail Nesterenko

Upload: newton

Post on 05-Jan-2016

40 views

Category:

Documents


4 download

DESCRIPTION

CS 4/69995 CS III: Programming Patterns. Mikhail Nesterenko. Procedural Programming. without classes: using standalone functions, primitive types and simple control structures. Variables. primitive ( basic/built-in ) types – string , int , bool , char , double exist but won’t need - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 4/69995 CS III: Programming Patterns

CS 44001/54001CS III: Programming Patterns

Mikhail Nesterenko

Page 2: CS 4/69995 CS III: Programming Patterns

Procedural Programming

without classes: using standalone functions, primitive types and simple control structures

Page 3: CS 4/69995 CS III: Programming Patterns

Before We Go

• a computer program is governed by rules

• what are

– syntax rules?

– semantic rules?

– stylistic rules?

3

Page 4: CS 4/69995 CS III: Programming Patterns

Types, Variables

• primitive (basic/built-in) types – string, int, bool, char, double– exist but won’t need

• int may be long or short, signed or unsigned• char may be signed or unsigned• floating point types may be float, double or long double

– auto – type to be determined by compiler (C++11)

auto i =7; // nice for type of loop variables

could also be auto & and const auto– decltype(expr) – type is same as expr (C++11)

decltype(i) j =8;

• variable – denotes memory location of particular type, holds values

– what is block? what is local variable? Can a variable be local to a block? what is scope of a variable?

4

Page 5: CS 4/69995 CS III: Programming Patterns

Operators, Arity

operators – built-in functions

• fixed number of operands (parameters)

arity – the number of operands

• unary

++ and -- prefix/sufix(postifx) preincrement/postincrement or predecrement /postdecrement

• use prefix porm

• prefix form returns an l-value (what’s that?)

• binary

– assingment, compound assingment

• ternary – what’s that?

What are these operators? What is their arity?

= ! - ++ += << ||

5

Page 6: CS 4/69995 CS III: Programming Patterns

Expressions

• what is a constant? Literal constant? What other kind of constant is there?

– who is contant type determined?

• What is expression? What is expression evaluation? How is expression type determined?

6

Page 7: CS 4/69995 CS III: Programming Patterns

Conditionals and Loops

• conditionals

– if/else

– switch

– ternary expression

• loops

– while

– do-while

– for• for(init_statement; expression; post_statement) action

• range-based-for (C++11)

int a[] = {10,20,30,40};

for(auto e: a)

cout << e;

how do I write a range-based-for to add 5 to each element of the array?

7

Page 8: CS 4/69995 CS III: Programming Patterns

Functions

• what is function invocation/call/definition/declaration?

• what is the difference between argument and parameter?

• in procedural programming functions are standalone

• function definition – includes head and body (implementation)

• can be declared (with prototype/signature – definition that omits body)

• function overloading – multiple different functions within the same scope provided that they have different signatures

double max(double, double);

int max(int, int);• resolution (of function call) – compile time process of associating function

invocation with function declaration

int i = max(10, 20); // resolved to second declaration• client/caller – function that invokes another function

– when dealing with programming patterns, function that uses the pattern• void – no return value, if no parameters – keep parentheses empty

void myFunc();

8

Page 9: CS 4/69995 CS III: Programming Patterns

Function Parameters, Default Values

• parameters may be passed by value, by reference, what’s the difference?

• default values

– default parameter value may be specified at function declaration

void move(int from, int to=0, int by =1);

– client has an option of specifying parameter or using default value

move (2, 3, 4);

move (2, 3);

move (2);

– provides convenient alternative to overloading

– only trailing parameters may have default values

void move(int from, int to=0, int by); // illegal

9

Page 10: CS 4/69995 CS III: Programming Patterns

References

reference – alias (another name) for data

• declared as type&• has to be initialized at declaration and cannot be changed

int& b = a;

++b; // changes a• can hold only l-values – values that refer to memory location, can be used

on the left-hand-side of assignment

• used for parameter passing

void swap(int &a; int &b){

int tmp = a;

a = b;

b = tmp;

}

10

Page 11: CS 4/69995 CS III: Programming Patterns

References (cont.)

reference can be used to return values

• in which case function can be used on the left-hand side of assignment

int& getElement(int x[], int i) {

return x[i];

}

...

int a[] = {10, 20, 30};

cout << getElement(a, 1);

getElement(a, 2) = 55;

• careful with returning local function variables by reference: they are destroyed when function returns, why?

11

Page 12: CS 4/69995 CS III: Programming Patterns

Pointers, Arrays and Dynamic Memory(review)

• what is stack, heap, frame?

• what is the relationship between array name and a pointer?

• what is dynamic variable and how is it allocated? deallocated? why is it needed?

• what is dynamic array and how is it allocated?

12

Page 13: CS 4/69995 CS III: Programming Patterns

Pointers and Functions

• function may also return a pointer

int* getElement(int x[], int i) {

return &x[i];

}

...

int a[] = {10, 20, 30};

cout << *getElement(a, 2);

*getElement(a, 5) = 55;

13

Page 14: CS 4/69995 CS III: Programming Patterns

nullptr

• nullptr – null pointer constant (C++11 addition)

– is of type pointer unlike NULL and 0, which are integer type

• what’s wrong with NULL and zero?

void myFunc(int); // overloaded functionvoid myFunc(char *);

func(NULL); // invokes first function

• use nullptr to signify uninitialized pointer

14

Page 15: CS 4/69995 CS III: Programming Patterns

Const

• keyword

• uses

– named constants: const int MyConst = 55;– parameter protection void myFunction(const double myValue);– reference protection void myFunction(const MyClass& myObj);

• const casting - allows a constant to be modified

const int i = 3;

const_cast<int>(i) = 1;

• what’s constant pointer? pointer to constant? can I have both?

15

Page 16: CS 4/69995 CS III: Programming Patterns

Arrays

• What does these lines do?

int myArray[10];

int myArray[10]={0}; // this one is tricky

int myArray[10]={2};

16

Page 17: CS 4/69995 CS III: Programming Patterns

Strings

• what is the difference between c-style and C++ style strings?

• how to convert c-style <-> c++-style string?

• what is argc argv ?

• what is concatentation?

• what do find() substr() insert(), replace(), erase() do?

• what is the difference between

myfunc(string); myfunc(string &) and myfunc(const string&);

17

Page 18: CS 4/69995 CS III: Programming Patterns

Static Variables

• static variables are initialized only once and retain their value across function invocations

void login() {

static int number1 = 0;

int number2 = 0;

++number1; ++number2;

cout << number1 << number2;

}

int main(){

login();

login();

login();

}• prints 112131

18

Page 19: CS 4/69995 CS III: Programming Patterns

Naming Conventsion

24