string name = "John Xhu"; cout << name << endl; string::size_type space_index = name.find(' '); cout << "Index of space: " << space_index << endl; if ( space_index == string::npos ) cout << "Not found" << endl; cout << "Initials: " << name[0] << name[space_index + 1] << endl;
todo.cpp
string operator[](int ix) { .... }We will discuss this later!!
+---------------+ |Type | | | |field: ___ | |field: ___ | +----------------+ |field: O------+------------->|Type | | | | | +---------------+ |field: ____ | +----------------+
class A { public: int f(); private: int num; };
// untested, from http://tinyurl.com/zy8scsu #include <math.h> double sqrt(double x) { if (x <= 0) return 0; // if negative number throw an exception? int exp = 0; x = frexp(x, &exp); // extract binary exponent from x if (exp & 1) { // we want exponent to be even exp--; x *= 2; } double y = (1+x)/2; // first approximation double z = 0; while (y != z) { // yes, we CAN compare doubles here! z = y; y = (y + x / y) / 2; } return ldexp(y, exp / 2); // multiply answer by 2^(exp/2) }
class ToDo { public: ToDo(); ToDo(string first_item); bool empty(); bool full(); void add(string next_item); string next_item(); // returns the next item capitalized; this is to illustrate converting // a string to upper case; use tolower to convert to lower case. string next_item_capitalized(); private: static constexpr int MAXITEMS = 100; string items[MAXITEMS]; int count; }; ToDo::ToDo() : count{0} { } ToDo::ToDo(string first_item) : count{1} { items[0] = first_item; } bool ToDo::empty() { return count <= 0; } bool ToDo::full() { return count >= MAXITEMS; } void ToDo::add(string next_item) { if ( full() ) throw "Adding item to full todo list."; items[count] = next_item; ++count; } string ToDo::next_item() { if ( empty() ) throw "Removing item from empty todo list."; string result = items[0]; for(int i = 0; i < count - 1; ++i) items[i] = items[i + 1]; --count; return result; } string ToDo::next_item_capitalized() { string item = next_item(); // note use of size_type to initialize i; this avoids warnings when // comparing i to item.length() (since item.length() is generally unsigned) for(string::size_type i = 0; i < item.length(); ++i) item[i] = toupper(item[i]); return item; }
void set_up_today() { ToDo stuff_to_do; stuff_to_do.add("sleep"); stuff_to_do.add("study"); stuff_to_do.remove("sleep"); ...
ToDo *todays_activities = new ToDo(); todays_activities->add("eat");
->
instead of .
*distance = 4.5;
g++ -S pointers.cppis in the file pointers.s
float *nums = new float[100];allocates 100 floats on the heap and stores it in
nums
float *temp = new float[200]; for(int i = 0; i < 100; ++i) temp[i] = nums[i]; nums = temp;
ToDo
class:
class ToDo { public: ToDo(); bool empty(); bool full(); void add(string next_item); string next_item(); private: string *items; int count, capacity; }; ToDo::ToDo() : count{0}, capacity{100}, items{new string[100]} { } bool ToDo::empty() { return count == 0; } bool ToDo::full() { return false; } // WHY? void ToDo::add(string next_item) { if ( count >= capacity ) { string *old_items = items; items = new string[capacity * 2]; capacity *= 2; for(int i = 0; i < count; ++i) items[i] = old_items[i]; delete[] old_items; } items[count] = next_item; ++count; } string ToDo::next_item() { if ( empty() ) throw "Removing item from empty todo list."; string result = items[0]; for(int i = 0; i < count - 1; ++i) items[i] = items[i + 1]; --count; return result; }