Aggregation/Composition Programming in C++ Fall 2008 Dr. David A. Gaitros

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
The Stack Data Structure. Classic structure What is a Stack? An abstract data type in which accesses are made at only one end Last In First Out (LIFO)
Stack & Queues COP 3502.
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Helpful C++ Transitions
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CMSC 202 Lesson 23 Templates II. Warmup Write the templated Swap function _______________________________ void Swap( ________ a, ________ b ) { _______________.
Templates Zhen Jiang West Chester University
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Introduction to Data Structures Fall 2008 Dr. David A. Gaitros
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Linked Structures See Section 3.2 of the text.. First, notice that Java allows classes to be recursive, in the sense that a class can have an element.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
CS Fall 2009 Linked List Introduction. Characteristics of Linked Lists Elements are held in objects termed Links Links are 1-1 with elements, allocated.
1. The user will be able to Search 1,000,000 Records by Part.No and display the sought record in no more than.5 seconds. The user will be able to.
C++ Functions A bit of review (things we’ve covered so far)
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Chapter 4 Stacks
Chapter 22 - C++ Templates
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Pointers and Linked Lists
Chapter 12 – Data Structures
Pointers and Linked Lists
Week 4 - Friday CS221.
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Data Structures and Algorithms
Programming Abstractions
Stack and Queue APURBO DATTA.
CSC 172 DATA STRUCTURES.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
CMSC 341 Lecture 5 Stacks, Queues
Helpful C++ Transitions
Chapter 19: Stacks and Queues.
Lesson Objectives Aims
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Data Structures and Algorithms
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CSI 1340 Introduction to Computer Science II
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
COP 3330 Object-oriented Programming in C++
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Presentation transcript:

Aggregation/Composition Programming in C++ Fall 2008 Dr. David A. Gaitros

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.

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.

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.

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.

Double Link List Class Head Tail Curr

Stack Class Head

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

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);

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

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.

Aggregation/Composition Stack Double Link List