Download presentation
Presentation is loading. Please wait.
Published byDrusilla Welch Modified over 9 years ago
1
Aggregation/Composition Programming in C++ Fall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu
2
Aggregation/Composition Aggregation is the act of embedding an object of one class into another as a member data of another class. Some developers use the term Composition to refer to a stronger relationship where the embedded object would typically not exist independent of the container class.
3
Aggregation/Composition Why do this? – Computers are complex machines and the only way we “humans” can deal with them is by layering the complexity. – “Something complex is just really many many simple things put together.” – The idea is to build a general purpose tool that performs some primitive or common function and use this instead of re-writing the code.
4
Aggregation/Composition Let’s say we want to write a program that uses a “Stack”. What is a stack? – It is a list. – It has a head where the most current values exist. – You can push things on a stack. – You can pop things off of a stack. – You can ask if the stack is empty. – It would be handy if ask how many items were in the stack. – It would be handy to print the stack.
5
Aggregation/Composition If a “stack” were the only data structure in existence I would not have a problem. I write a stack class that contains all of the necessary data structures, pointers, variables, etc and continue on my merry way. However, what about – Queue – Dequeue – Ordered List – Unordered List – Binary Tree – Traverse the list in reverse – Etc.
6
Double Link List Class 0 4 8 23 79 45 9999 Head Tail Curr
7
Stack Class 67 3 5 9 7 4 Head
8
Stack Class Header File #ifndef STACK1_H #define STACK1_H #include “dllist.h” class stack: { public void Push(int val); int Pop(); bool isempty(); int NumItems(); void ListStack; };
9
Class Double Link List #ifndef DLLIST1_H #define DLLIST1_H class dllist: public: struct dlist_type { struct * prev; int val; struct * next; }; dllist (); // constuctor dllist (int lo, int hi); struct dlist_type *FindVal(int val); int DelRecord(struct dlist_type *loc); struct dlist_type *InstertVal(int val); struct dlist_type *ReturnHead(); struct dlist_type *ReturnTail(); int InsertBefore(struct dlist_type *loc, int val);
10
Class Double Link List int InsertAfter(struct dlisttype *Loc, int val); void ListDLL(); struct dlist_type *ReturnCurr(); int ReturnNumberInList(); private: struct dlist_type *Curr; struct dlist_type *Head; struct dlist_type *tail; int NumberOfRecords; }; #endif
11
Aggregation/Composition Things to remember – When an object that has an embedded object is created the constructor runs. Inside of the constructor of the called class, you must remember to call the constructor of the embedded class! – If there is nothing special, it will invoke the default constructer. – In the case of the double link list, this is not sufficient. – You may not use all of the features of the embedded clas… that’s ok and usually normal.
12
Aggregation/Composition Stack Double Link List
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.