240 likes | 345 Views
CSE 2341 - Honors Principles of Computer Science I. Spring 2008 Mark Fontenot mfonten@engr.smu.edu. Note Set 3. Take Note!!!. Next Tuesday, January 22, 2008 Follows a Monday Schedule!!!. Quick Look. Activity Diagrams and the UML Use default and overloaded constructors
E N D
CSE 2341 - HonorsPrinciples of Computer Science I Spring 2008 Mark Fontenot mfonten@engr.smu.edu Note Set 3
Take Note!!! Next Tuesday, January 22, 2008 Follows a Monday Schedule!!!
Quick Look Activity Diagrams and the UML Use default and overloaded constructors Declare and work with an array of objects Validate user input from within a class
UML® * Siegel, Jon. Introduction to OMG's Unified Modeling Language™ (UML®), http://www.omg.org/gettingstarted/what_is_uml.htm. July 2005 “helps you specify, visualize, and document models of software systems, including their structure and design”* Used heavily in industry to design software (and other things as well) Can be used to explore a current code base by reverse engineering to UML models UML specifies 13 different diagrammatic tools to use in design of system architecture
Our First UML Diagram – Activity Diagram Sample Activity Diagram • Activity Diagram • models logic • can be used at various levels of detail (30,000 foot view or microscopic view of) • Somewhat similar to flowcharting
Main Activity Diagram Artifacts Initial Node – indicates starting point of logic Final Node – indicates stopping point of logic Activity – rounded rectangle – represents the activities that are taking place Flow - arrow indicating flow of logic Decision – Diamond with one flow entering and two or more exiting Ambler, Scott. UML 2 Activity Diagrams, August 20 2006. http://www.agilemodeling.com/artifacts/activityDiagram.htm
Main Activity Diagram Artifacts (2) Merge – Diamond – two or more flows entering and one exiting Condition – text in square brackets indicated a condition that must be satisfied to traverse its flow. [credentials valid] [credentials not valid] Ambler, Scott. UML 2 Activity Diagrams, August 20 2006. http://www.agilemodeling.com/artifacts/activityDiagram.htm
Examples with pseudocode If x is greater than 60 print “YOU PASSED”
Examples with pseudocode Initialize value to – 3Loop while value is less than 10 value = value + 2
Overloaded Constructors InventoryItem.h #ifndef INVENTORYITEM_H #define INVENTORYITEM_H #include <cstring> using namespace std; class InventoryItem { private: char *description; int units; public: InventoryItem() { description = new char[51]; } InventoryItem(char *desc) { description = new char[strlen(desc)+1]; strcpy(description, desc); }
Overloaded Constructors InventoryItem.h InventoryItem(char *desc, int u) { description = new char [strlen(desc) + 1]; strcpy(description,desc); units = u; } ~InventoryItem() { delete[] description; } void setDescription(char *d) { strcpy(description, d); } void setUnits (int u) { units = u; } char *getDescription() { return description; } int getUnits() { return units; } }; #endif
Declaring Objects InventoryItem ii; InventoryItem ii2(“Wrench”); InventoryItem ii3(“Hammer”, 10); Because of the overloaded constructors, can instantiate in different ways
Constructors and Destructors A class can only have one default constructor A class can have any number of overloaded constructors A class can have only one destructor
Objects and Arrays InventoryItem store[10]; • Possible to declare arrays of objects • Default constructor would be called for each element in the array.
Objects and Arrays • Square tres[3] = {5, 6, 10}; • InventoryItem store[3] = { “Wrench”, “Hammer”, “Pliers” }; • May use initialization lists to call overloaded constructors
Objects and Arrays First Element InventoryItem inventory[3] = { “Hammer”, InventoryItem(“Wrench”,17), } Second Element Third Element- Which constructor???? Need more complex initialization list if number of args to constructor is greater than 1
Objects and Arrays InventoryItem inventory[3] = { InventoryItem(“Hammer”, 12), InventoryItem(“wrench”, 17), InventoryItem(“pliers”, 10) }; for (int i =0; i < 3; i++) { cout << inventory[i].getDescription(); cout << inventory[i].getUnits(); }
Design Time • Design an array class. The class should • create an array of integers of any size at runtime • perform bounds checking • allows setting of a particular element of an array • retrieves a particular element from the array
Intlist #ifndef INTLIST_H#define INTLIST_H class IntList { private: int *list; int numElements; bool isValid(int); public: IntList(int); ~IntList(); bool set(int, int); bool get(int, int &);};#endif Intlist.h
Intlist #include “IntList.h” IntList::IntList (int size){ list = new int[size]; numElements = size; for (int ndx =0; ndx < size; ndx++) list[ndx] = 0;} IntList::~IntList(){ delete [] list; } Intlist.cpp
Intlist bool IntList::isValid(int element){ if (element < 0 || element >= numElements) return false; else return true;}bool IntList::set(int element, int value){ if (isValid(element)) { list[element] = value; return true; } else return false;} Intlist.cpp
Intlist bool IntList::get(int element, int &value){ if (isValid(element)) { value = list[element]; return true; } else return false; } Intlist.cpp
Take Note!!! Next Tuesday, January 22, 2008 Follows a Monday Schedule!!!