CS120 - Day 11: Writing Function Templates & Classes -------------------------------------------- A few more STL Details ----------------------- - if use vector of pointers to dynamically allocated memory, erasing element does not delete the object pointed to Lists ------------------------------------------------------------------ - doubly linked list w/pointers - efficient for insert/delete in the middle - list ops: splice, merge, front(), push_front(val), pop_front(), remove(val), unique() (must sort first), reverse() - sorting - list.sort() relies on < operator - list.sort(cmp) uses predicate comparator function cmp (pointer to function?!) bool compare(const Card & x, const Card & y) { return x.face < y.face; } Algorithms in STL ---------------------------------------------------- - #include warning: limitations on the types of iterators that will work - act on container elements (data inside), not on actual container Searching/Sorting ----------------- void sort(is, ie) void sort(is, ie, bool func(valtype, valtype)) iter find(is, ie, val) - returns iter to 1st occurance of val in range iter find_if(is, ie, bool func(valtype)) bool binary_search(is, ie, val) - container must be sorted first bool binary_search(is, ie, val, bool func(valtype, valtype)) [if time: Associative Containers ] WRITING FUNCTION TEMPLATES ----------------------------------- - can use to define generic function with abstract param types - use template before prototype and function definition - use Tname as type in function - compiler generates the actual functions by substituting types of arguments in the function call EXAMPLE: print functions for vectors & lists using iterator EXAMPLE: template // generic class type name T T cubeit(T num) // header using type T { return num*num*num; // assumes * defined for type T } EXAMPLE - write mergesort using container? template void mergesort(vector v); // or list ? ------------------------------------------------------------------------- Classes vs. Structs - class members are private by default - struct members are public by default - otherwise can do much of the same with either - organization of program - header files with class definitions (interface) - program file with class implementations (implementation) - main() with variable declarations (instances) function calls - OO features - data abstraction, encapsulation, hiding, modelling - public interface, private implementation - polymorphism - templates/generics - function overloading - operator overloading - inheritance - abstract base classes - classes (abstraction & modelling) - data members (properties) - can be of different types or classes - function members (behaviors) - can be defined there or elsewhere - assume full access to data members Class Basics (FirstClass) - information hiding of class members - public parts - anybody can access - private parts - access by same class members - constructors - default - 1param usage as type conversions (conversion constructor) - copy - shallow vs. deep (not called with = operator) - copy constructors invoked - explicitly when initializing an object in declaration - implicitly when passing an object call by value - implicitly when returning an object by value - initializer lists for data members - init in order of declaration, not list order - all data members init even if not listed explicitly - body of constructor can be used to change initializations - destructors - important for memory management - inline function definitions - need semicolon: int Myclass:: getX() { return x ; } ; - instance variable is not a reference, as in Java, it has memory when it is declared, and initialized by the constructor - 'this' pointer to current object - use this->datamember for clarification (as in + op) - use *this as name for current object (as in != op) - syntax for interface (*.h file) class classname { memberlist }; // semicolon is very important - constant member functions - use const at end of function header to say that it can't modify any data members of the current object - use const in prototype and implementation header - can be applied to const and non-const qualified objects - non-const function can not be applied to const qualified object - can't do: Point :: Point () : x=0, y=0 { } - must do: Point :: Point () : x(0), y(0) { } - combined constructors using default values - defaults only appear in the header - must be defaulted from right to left Point (int xy=0, int yv=0); // prototype - implementation is as before - calling options: leave out right most params Point p1(3, 4); // use both Point p2(3); // use x, y defaults to 0 Point p3; // both default to 0 - 'static' members are class-wide info, shared by all objects - declare data: static int sharedint; - can be accessed through class object or classname:: (if public) - declare functions: static int changeshared (int); - function can only be static if it only accesses static data - no 'this' for static members - develop Bag (Set) class example together - dynamic memory allocation - static data and function to change - deep copy constructor - destructor - upgrade to template class? - class composition - objects in classes - class data members are constructed in order of declaration and before the class constructor is called - do rectangle class with 2 points for data members DYNAMIC MEMORY IN C++ --------------------------------------------- Using new/delete for dynamic memory allocation: - arrays int *iray = new int[size]; // makes array of size ints delete [] iray; // frees memory of array - new is NOT used to create objects like in JAVA - more to come later...