Pointers and Classes
We’ll talk about today Pointers Arrays Strings Classes “new” operator
Pointers Stores the memory address where the data is stored int* numberPointer; Can access the data that is pointed to with * *numberPointer
Pointers and references int number; int* numberPointer = &number; Two sides of the same data numberPointer Memory address pointed to *numberPointer The value pointed to by numberPointer Dereference number The value of number &number The memory address where number is stored
Pointers are Memory Addresses
Arrays and Pointers Arrays are pointers to the first element char* aString = char[5]; Equivalent aString[3] *(aString+3) String Implemented around a char* string anotherString = “words”; anotherString[2] == ‘r’;
Objects Same objective as Java Different syntax OO-programming public/private sections separate declarations and definitions Operator overloading We’ll see an example class in the code
Objects an Pointers “new” returns a pointer to the new object OurClass* classPointer = new OurClass(); Don’t need to copy the entire object Needed for efficiency when working with large data structures Pass-by-pointer By default in Java C++ give you the option If you use pointers obj->function();