140 likes | 255 Vues
This assignment provides comprehensive tips and insights into advanced Object-Oriented Programming (OOP) concepts in C++. Key topics include the use of structs, function pointers, macros, containers, and iterators. It covers how to define and manipulate structs, delegate functions using pointers, utilize macros for cleaner code, and understand the intricacies of containers and their iterators. Additionally, examples illustrate key concepts such as equality checks, string literal concatenation, and idiomatic iteration, equipping students with practical skills for robust C++ programming.
E N D
Assignment 1 Tips Advanced OOPCS 440/540Spring 2014Kenneth Chiu
structs • 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: • 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()?
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.
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);
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”); }
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?
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;}
// 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;}
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 = …;}
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]
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");}
String Literals • What’s wrong with this? • void foo(char *);...foo(“hello”);