CSE 2341 - Honors Principles of Computer Science I

Post on 14-Feb-2016

33 views 0 download

description

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 - PowerPoint PPT Presentation

Transcript of CSE 2341 - Honors Principles of Computer Science I

Spring 2008

Mark Fontenotmfonten@engr.smu.edu

CSE 2341 - HonorsPrinciples of Computer Science I

Note Set 31

Take Note!!!

2

Next Tuesday,January 22, 2008

Follows a Monday Schedule!!!

Quick Look

3

Activity Diagrams and the UMLUse default and overloaded constructorsDeclare and work with an array of objectsValidate user input from within a class

UML®

4

“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

* Siegel, Jon. Introduction to OMG's Unified Modeling Language™ (UML®), http://www.omg.org/gettingstarted/what_is_uml.htm. July 2005

Our First UML Diagram – Activity Diagram

5

Activity Diagram models logiccan be used at various levels of detail (30,000 foot view or

microscopic view of)Somewhat similar to flowcharting

Sample Activity Diagram

Main Activity Diagram Artifacts

6

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)

7

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

8

If x is greater than 60print “YOU PASSED”

Examples with pseudocode

9

Initialize value to – 3Loop while value is less than 10

value = value + 2

Overloaded Constructors

10

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

}

Inve

ntoryItem.h

Overloaded Constructors

11

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

Inve

ntoryItem.h

Declaring Objects

12

Because of the overloaded constructors, can instantiate in different ways

InventoryItem ii;InventoryItem ii2(“Wrench”);InventoryItem ii3(“Hammer”, 10);

Constructors and Destructors

13

A class can only have one default constructorA class can have any number of overloaded constructorsA class can have only one destructor

Objects and Arrays

14

Possible to declare arrays of objects

Default constructor would be called for each element in the array.

InventoryItem store[10];

Objects and Arrays

15

May use initialization lists to call overloaded constructors

Square tres[3] = {5, 6, 10};

InventoryItem store[3] = { “Wrench”, “Hammer”, “Pliers” };

Objects and Arrays

16

Need more complex initialization list if number of args to constructor is greater than 1

InventoryItem inventory[3] = {

“Hammer”, InventoryItem(“Wrench”,17), }

First Element

Second Element

Third Element- Which constructor????

Objects and Arrays

17

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

18

Design an array class. The class should create an array of integers of any size at runtimeperform bounds checkingallows setting of a particular element of an arrayretrieves a particular element from the array

Intlist

19

#ifndef INTLIST_H#define INTLIST_Hclass 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

20

#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

21

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

22

bool IntList::get(int element, int &value){ if (isValid(element)) { value = list[element]; return true; } else return false;}

Intlist.cpp

Take Note!!!

23

Next Tuesday,January 22, 2008

Follows a Monday Schedule!!!

24

?