1 / 8

Initial Composition

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

lapis
Download Presentation

Initial Composition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Initial Composition December 13th, 2012 Erik Fredericks

  2. 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.

  3. 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; };

  4. 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]; };

  5. 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

  6. 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

  7. 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

  8. Composition Steps Taken • Data consumer (structSorter) • Pull data from composed module into necessary location • In this case, Sorter.numbers_

More Related