Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assignment 1 Background Advanced OOP CS 440/540 Spring 2015 Kenneth Chiu.

Similar presentations


Presentation on theme: "Assignment 1 Background Advanced OOP CS 440/540 Spring 2015 Kenneth Chiu."— Presentation transcript:

1 Assignment 1 Background Advanced OOP CS 440/540 Spring 2015 Kenneth Chiu

2 struct s struct A { char c; double x; int array[1]; }; Can you do this? – A a1, a2; … a1 = a2; // Assignment. if (a1 == a2) { … }; // Equality.

3 Function Pointers Syntax is like this: – int foo(double x); int (*fp)(double x) = foo; i = (*fp)(3.14); Can reassign: – int foo2(double x); int foo3(double x); int foo4(int x); fp = foo2; fp = foo3; fp = 0; fp = nullptr; // C++11 fp = foo4; Can be combined with structs to look like methods: – struct Foo {... int (*foo2)(Foo *, double x); };... // Assuming that the function pointer has been // properly initialized, it can be used as below. f->foo2(f, 3.14); – Why is f being passed to foo2() ?

4 String Literal Concatenation Any problems with the below? – printf(“A long, long, long, long,” “long, long sentence.\n”); Two string literals right next to each other are automatically concatenated at compile-time. Handy for long strings.

5 Macros Can be used for token replacement: – #define PI area = PI*r*r; – #define GREETING “Hola!” printf(“%s\n”, GREETING); printf(GREETING); printf(“\n”); – #define FOREVER while (1) { FOREVER printf(“Stuck in a loop...\n”); } – #define TIMES * #define GETS = a GETS 3 TIMES 2; Can have parameters: – #define alloc(type) (type *) malloc(sizeof(type)) struct Foo *f = alloc(Foo); – #define SQR(x) x*x area = PI*SQR(r); a2 = PI*SQR(2+5); x = 3; x2 = SQR(x++);

6 Consider: – #define M(t) \ int foo_##t() { \ printf(“%s\n”, “A_” #t); \ } What does M(int) do? – The backslash is a continuation character for long macros. – The ## splices two preprocessor tokens together into one. – The # operator stringifies the following token by putting “” around it. – Recall that two string literals that are right next to each other are automatically joined by the compiler into one string literal. Result is: – int foo_int() { printf(“%s\n”, “A_int”); }

7 Containers and Iterators A container holds values. An iterator is essentially a “pointer” (or “cursor”) into a container. Every container has a way to obtain an iterator pointing to the first element. Every container has a special iterator value, the “end” value. It represents one past the last element of the container. What happens if you decrement the “begin” iterator? How about increment the “end” iterator?

8 Example (assume std ): – list l1; l1.push_back(1); l1.push_back(2); for (list ::iterator it = l1.begin(); it != l1.end(); ++it) { cout << *it << endl; } – list l2; l2.push_back(new MyClass(“A”)); l2.push_back(new MyClass(“B”)); for (list ::iterator it = l2.begin(); it != l2.end(); ++it) { cout << *it << endl; }

9 – // C++11 list l1{1, 2}; for (auto it = l1.begin(); it != l1.end(); ++it) { cout l2{new MyClass(“A”), new MyClass(“B”)}; for (auto p: l2) { cout << p << endl; }

10 Why Doesn’t End Iterator in the Assignment Point to the Last Element? Glib answer: That’s the way the C++ standard library does it. What if the end iterator pointed to the last element instead? – for (auto it = l.begin(); ???; ++it) { //... } – Empty list?

11 Why Doesn’t End Iterator in the Assignment Point to the Last Element? Glib answer: That’s the way the C++ standard library does it. Keep in mind: – Iterators in C++ are modeled after pointers. – Idiomatic iteration over arrays in C/C++: for (int i = 0; i < n; i++) { a[i] = … ; } – Common way to iterate using pointers: int *begin = new int[n], *end = begin + n; for (int *p = begin; p < end; p++) { *p = …; } But we could do: – for (int i = 0; i <= n - 1; i++) { a[i] = …; } – int *begin = new int[n], *end = begin + n – 1; for (int *p = begin; p <= end; p++) { *p = …; }

12 To iterate over a region of length n in the middle: – for (int i = pos; i < pos + n; i++) { … } – for (int *p = pos; p < pos + n; p++) { … } – for (int i = pos; i <= pos + n - 1; i++) { … } – for (int *p = pos; p <= pos + n - 1; p++) { … } [Show end.cpp]

13 Arrays and String Literals What does this print? – #include int main() { char *p; char array[20]; printf("%zd, %zd, %zd\n", sizeof p, sizeof array, sizeof "hello"); }

14 String Literals What’s wrong with this? – void foo(char *);... foo(“hello”);

15 What’s a Double-Ended Queue?


Download ppt "Assignment 1 Background Advanced OOP CS 440/540 Spring 2015 Kenneth Chiu."

Similar presentations


Ads by Google