80 likes | 178 Views
Initial Composition. December 13 th , 2012 Erik Fredericks. Composition. C++ Class has-a relationship with another class Car has-a carburetor (1:1) Lake has-a duck * (1:many) When parent class is destroyed, composed classes are as well Aggregation does not destroy child classes
E N D
Initial Composition December 13th, 2012 Erik Fredericks
Composition • C++ • Class has-a relationship with another class • Car has-a carburetor (1:1) • Lake has-aduck* (1:many) • When parent class is destroyed, composed classes are as well • Aggregation does not destroy child classes • Achieve through inheritance, templates, etc.
Composition • Limiting to C forces a focus on non-OOP methods, but we can build from that methodology • Structs struct Person { int age; char *name; enum{ male, female } gender; };
Composition • Function pointers struct Sorter { Sorter(int* input); Sorter(void* sorting_function); void print(); void call_sort(); int* (*sort_fn)(int*, int, int); intnumbers_[SIZE]; };
Sorting Codebase Structure • All code compiles under GCC • Sorting struct • Contains an integer array and a function pointer • Separate functions for each sorting method • Currently these are “floating” modules
Sorting Composition • Sorting structure required a single modification in this instance • Addition of function pointer to pull in a sorting method • Existing sort methods setup to modify numbers array in place
Composition Steps Taken • Data producer (structSorter) • Allow generic call to swap sorting method • Provide access to internal data storage • Composed module (method QuickSort) • Create function pointer • Provide function pointer to data producer • Call composed module method • If necessary, wrap module method
Composition Steps Taken • Data consumer (structSorter) • Pull data from composed module into necessary location • In this case, Sorter.numbers_