代写代考 COSC1076 Week 02

Strings, Classes & Pointers
COSC1076 Week 02

Additional Notes

Copyright By PowCoder代写 加微信 powcoder

Advanced Pointers – Casting
Pointer types can be cast to pointers to another type
• The underlying memory will is re-interpreted as the new type
For example:
float testFloat = 20;
float* ptrF = &testFloat;
int* ptrI = (int*) ptrF;
std::cout << ptrI << " " << ptrF << std::endl; std::cout << * ptrI << " " << *ptrF << std::endl; Of course, just because this is possible, doesn’t mean it is a good idea Week 02 | Strings, Classes & Pointers COSC1076 Advanced Pointers - void* The most generic form of a pointer is the type * Any pointer can be cast to the type * • This was in the past useful for writing “generic” functions that didn’t care about the type of the pointer • Generic functions are done with templates (to be seen later in the course) Any void* pointer can be cast into another type • But that doesn’t mean the dereferenced pointer will be correctly interpreted Week 02 | Strings, Classes & Pointers COSC1076 Advanced Pointers - Pointer Arithmetic Pointers (and memory addresses) are hexadecimal values (ie numbers). This means that pointers work with arithmetic operations • Pointer arithmetic is sensitive to the type of the pointer and increments the address by the correct amount relative to the type of the dereferenced value int a = 1; int b = 2; int c = 3; int* p = &b; cout << *p << endl; cout << *(p+1) << endl; cout << *(p-1) << endl; In general, pointer arithmetic is not directly used. • BUT! It does appear when working with arrays - as we will see next week. Week 02 | Strings, Classes & Pointers COSC1076 Using Multiple Files C/C++ has two types of files • Code files (cpp) • Header files (h or hpp) Header files have declarations Code files have definitions • Definitions in header files are included in the code file #include “header.h” - For local header files, use double-quotes - Use relative-path to the header file from the code file Week 02 | Strings, Classes & Pointers COSC1076 Using Multiple Files For reference, STL library includes which look like: #include
• Tells the compiler to look for the file called iostream in the library locations
• Use angle-brackets
Week 02 | Strings, Classes & Pointers COSC1076

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com