1 / 14

Assignment 1 Tips

Assignment 1 Tips. Advanced OOP CS 440/540 Spring 2014 Kenneth Chiu. 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. Function Pointers. Syntax is like this:

toby
Download Presentation

Assignment 1 Tips

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. Assignment 1 Tips Advanced OOPCS 440/540Spring 2014Kenneth Chiu

  2. structs • 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 = foo4; • Can be combined with structs to look like methods: • structFoo { ... 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 3.14159area = 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))structFoo *f = alloc(Foo); • #define SQR(x) x*xarea = PI*SQR(r);a2 = PI*SQR(2+5);

  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<int> l1;l1.push_back(1);l1.push_back(2);for (list<int>::iterator it = l1.begin(); it != l1.end(); ++it) {cout << *it << endl;} • list<MyClass *> l2;l2.push_back(new MyClass(“A”));l2.push_back(new MyClass(“B”));for (list<MyClass *>::iterator it = l2.begin(); it != l2.end(); ++it) {cout << *it << endl;}

  9. // C++11list<int> l1{1, 2};for (auto it = l1.begin(); it != l1.end(); ++it) {cout << *it << endl;}// Same as above.for (auto i: l1) {cout << i << endl;}list<MyClass*> 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. • Keep in mind: • Iterators in C++ are modeled after pointers. • Idiomatic iteration over arrays in C/C++: • for (inti = 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 (inti = 0; i <= n - 1; i++) { a[i] = …;} • int *begin = new int[n], *end = begin + n – 1;for (int *p = begin; p <= end; p++) { *p = …;}

  11. To iterate over a region of length n in the middle: • for (inti = 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]

  12. Arrays and String Literals • What does this print? • #include <stdio.h>int main() {  char *p;  char array[20];printf("%zd, %zd, %zd\n",sizeof p, sizeof array, sizeof "hello");}

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

  14. What’s a Double-Ended Queue?

More Related