COMP 2710 Software Construction Midterm Exam 2 Review

  • Slides: 45
Download presentation
COMP 2710 Software Construction Midterm Exam 2 - Review Dr. Xiao Qin Auburn University

COMP 2710 Software Construction Midterm Exam 2 - Review Dr. Xiao Qin Auburn University http: //www. eng. auburn. edu/~xqin@auburn. edu

Chapters • • Ch 6 Structures and Classes Ch 7. 1 Constructors Ch 10

Chapters • • Ch 6 Structures and Classes Ch 7. 1 Constructors Ch 10 Pointers and Dynamic Arrays Linked List Use Cases Data Flow Diagram Function Oriented Design 1 -

Structure Types • Define struct globally (typically) • No memory is allocated – Just

Structure Types • Define struct globally (typically) • No memory is allocated – Just a "placeholder" for what our struct will "look like" • Definition: struct CDAccount. V 1 Name of new struct "type" { double balance; member names double interest. Rate; int term; }; Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 6 -3

Accessing Structure Members • Dot Operator to access members – account. balance – account.

Accessing Structure Members • Dot Operator to access members – account. balance – account. interest. Rate – account. term • Called "member variables" – The "parts" of the structure variable – Different structs can have same name member variables • No conflicts Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 6 -4

Structure Assignments • Given structure named Crop. Yield • Declare two structure variables: Crop.

Structure Assignments • Given structure named Crop. Yield • Declare two structure variables: Crop. Yield apples, oranges; – Both are variables of "struct type Crop. Yield" – Simple assignments are legal: apples = oranges; • Simply copies each member variable from apples into member variables from oranges Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 6 -5

Pointer Introduction • Pointer definition: – Memory address of a variable • Recall: memory

Pointer Introduction • Pointer definition: – Memory address of a variable • Recall: memory divided – Numbered memory locations – Addresses used as name for variable • You’ve used pointers already! – Call-by-reference parameters • Address of actual argument was passed Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 10 -6

Pointer Variables • Pointers are "typed" – Can store pointer in variable – Not

Pointer Variables • Pointers are "typed" – Can store pointer in variable – Not int, double, etc. • Instead: A POINTER to int, double, etc. ! • Example: double *p; – p is declared a "pointer to double" variable – Can hold pointers to variables of type double • Not other types! Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 10 -7

How to refer to …? • Recall: int *p 1, *p 2, v 1,

How to refer to …? • Recall: int *p 1, *p 2, v 1, v 2; p 1 = &v 1; • Two ways to refer to v 1 now: – Variable v 1 itself: cout << v 1; – Via pointer p 1: cout << *p 1; • Dereference operator, * – Pointer variable "derereferenced" – Means: "Get data that p 1 points to" Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 10 -8

The new Operator • Since pointers can refer to variables… – No "real" need

The new Operator • Since pointers can refer to variables… – No "real" need to have a standard identifier • Can dynamically allocate variables – Operator new creates variables • No identifiers to refer to them • Just a pointer! • p 1 = new int; – Creates new "nameless" variable, and assigns p 1 to "point to" it – Can access with *p 1 • Use just like ordinary variable Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 10 -9

Nodes and Linked Lists • Linked list – Simple example of "dynamic data structure"

Nodes and Linked Lists • Linked list – Simple example of "dynamic data structure" – Composed of nodes • Each "node" is variable of struct or class type that’s dynamically created with new – Nodes also contain pointers to other nodes – Provide "links" 17 -

Display 17. 1 Nodes and Pointers Copyright © 2010 Pearson Addison. Wesley. All rights

Display 17. 1 Nodes and Pointers Copyright © 2010 Pearson Addison. Wesley. All rights reserved. 17 -11

Node Definition • struct List. Node { string item; int count; List. Node *link;

Node Definition • struct List. Node { string item; int count; List. Node *link; }; typedef List. Node* List. Node. Ptr; • Order here is important! – Listnode defined 1 st, since used in typedef • Also notice "circularity" 17 - Copyrig ht © 2010 Pearson Addison

Head Pointer • Box labeled "head" not a node: List. Node. Ptr head; –

Head Pointer • Box labeled "head" not a node: List. Node. Ptr head; – A simple pointer to a node – Set to point to 1 st node in list • Head used to "maintain" start of list • Also used as argument to functions 17 - Copyrig ht © 2010 Pearson Addison

Example Node Access • (*head). count = 12; – Sets count member of node

Example Node Access • (*head). count = 12; – Sets count member of node pointed to by head equal to 12 • Alternate operator, -> – Called "arrow operator" – Shorthand notation that combines * and. – head->count = 12; • Identical to above • cin >> head->item – Assigns entered string to item member 17 - Copyrig ht © 2010 Pearson Addison

End Markers • Use NULL for node pointer – Considered "sentinel" for nodes –

End Markers • Use NULL for node pointer – Considered "sentinel" for nodes – Indicates no further "links" after this node • Provides end marker similar to how we use partially-filled arrays 17 - Copyrig ht © 2010 Pearson Addison

Display 17. 2 Accessing Node Data Copyright © 2010 Pearson Addison. Wesley. All rights

Display 17. 2 Accessing Node Data Copyright © 2010 Pearson Addison. Wesley. All rights reserved. 17 -16

Class Definitions • Defined similar to structures • Example: class Day. Of. Year name

Class Definitions • Defined similar to structures • Example: class Day. Of. Year name of new class type { public: void output(); member function! int month; int day; }; • Notice only member function’s prototype – Function’s implementation is elsewhere Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 6 -

Declaring Objects • Declared same as all variables – Predefined types, structure types •

Declaring Objects • Declared same as all variables – Predefined types, structure types • Example: Day. Of. Year today, birthday; • Declares two objects of class type Day. Of. Year • Objects include: – Data • Members month, day – Operations (member functions) • output() Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 6 -

Class Member Access • Members accessed same as structures • Example: today. month today.

Class Member Access • Members accessed same as structures • Example: today. month today. day – And to access member function: today. output(); Invokes member function Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 6 -

Class Member Functions • Must define or "implement" class member functions • Like other

Class Member Functions • Must define or "implement" class member functions • Like other function definitions – Can be after main() definition – Must specify class: void Day. Of. Year: : output() {…} • : : is scope resolution operator • Instructs compiler "what class" member is from • Item before : : called type qualifier Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 6 -

Public and Private Members • Data in class almost always designated private in definition!

Public and Private Members • Data in class almost always designated private in definition! – Hide data from user – Allow manipulation only via operations • Which are member functions • Public items (usually member functions) are "user-accessible" 6 -

Public and Private Example • Modify previous example: class Day. Of. Year { public:

Public and Private Example • Modify previous example: class Day. Of. Year { public: void input(); void output(); private: int month; int day; }; • Data now private • Objects have no direct access 6 -

Accessor and Mutator Functions • Object needs to "do something" with its data •

Accessor and Mutator Functions • Object needs to "do something" with its data • Call accessor member functions – Allow object to read data – Also called "get member functions" – Simple retrieval of member data • Mutator member functions – Allow object to change data – Manipulated based on application 6 -

Structures versus Classes • Structures – Typically all members public – No member functions

Structures versus Classes • Structures – Typically all members public – No member functions • Classes – Typically all data members private – Interface member functions public • Technically, same – Perceptionally, very different mechanisms 6 -

Constructors • Initialization of objects – Initialize some or all member variables – Other

Constructors • Initialization of objects – Initialize some or all member variables – Other actions possible as well • A special kind of member function – Automatically called when object declared • Very useful tool – Key principle of OOP 7 -

Constructor Notes • Notice name of constructor: Day. Of. Year – Same name as

Constructor Notes • Notice name of constructor: Day. Of. Year – Same name as class itself! • Constructor declaration has no return-type – Not even void! • Constructor in public section – It’s called when objects are declared – If private, could never declare objects! 7 -

Calling Constructors • Declare objects: Day. Of. Year date 1(7, 4), date 2(5, 12);

Calling Constructors • Declare objects: Day. Of. Year date 1(7, 4), date 2(5, 12); • Objects are created here – Constructor is called – Values in parens passed as arguments to constructor – Member variables month, day initialized: date 1. month 7 date 2. month 5 date 1. dat 4 date 2. day 12 7 -

Constructor Equivalency • Consider: – Day. Of. Year date 1, date 2 date 1.

Constructor Equivalency • Consider: – Day. Of. Year date 1, date 2 date 1. Day. Of. Year(7, 4); date 2. Day. Of. Year(5, 5); // ILLEGAL! • Seemingly OK… – CANNOT call constructors like other member functions! 7 -

Constructor Code • Constructor definition is like all other member functions: Day. Of. Year:

Constructor Code • Constructor definition is like all other member functions: Day. Of. Year: : Day. Of. Year(int month. Value, int day. Value) { month = month. Value; day = day. Value; } • Note same name around : : – Clearly identifies a constructor • Note no return type – Just as in class definition 7 -

Alternative Definition • Previous definition equivalent to: Day. Of. Year: : Day. Of. Year(int

Alternative Definition • Previous definition equivalent to: Day. Of. Year: : Day. Of. Year(int month. Value, int day. Value) : month(month. Value), day(day. Value) {…} • Third line called "Initialization Section" • Body left empty • Preferable definition version 7 -

Goals of Use Case Analysis • Look at the system as a whole •

Goals of Use Case Analysis • Look at the system as a whole • • Express software requirements Identify all the major uses of the system Express computer application workflow It is a functional description of the entire system. Note: use cases are not design tools. Just functional descriptions not software structure. 1 -31

Formal Definition - Actor • A role that a user can play. • Examples:

Formal Definition - Actor • A role that a user can play. • Examples: instructor, advisor, student. • Actors do not have to be human. An actor such as a sensor may cause a system reaction. • An actor is always outside of the system boundary, an external entity. 1 -32

Formal Definition - User • Someone who uses the system. • The same user

Formal Definition - User • Someone who uses the system. • The same user can play multiple roles. • Example Prof. John Doe plays the role of an instructor and the role of an advisor. • A user is an instance of an actor. Can you give an example in which one user can play multiple roles? Paper submission systems 1 -33

Formal Definition - Views • Two or more actors interacting with a use case.

Formal Definition - Views • Two or more actors interacting with a use case. • Example: Register for Course involves both the Student registering for the course and the Bursar's office sending out a bill for it. Each actor views this task from a different perspective. Can you give another example? Customer -> deposit money <- Bank clerk 1 -34

UML Symbols There could be different sequences of individual steps for the same use

UML Symbols There could be different sequences of individual steps for the same use case. These are called scenarios. 1 -35

Use Case Diagrams UML use case relationship -symbols Relationships: Use cases may be related

Use Case Diagrams UML use case relationship -symbols Relationships: Use cases may be related to other use cases. 1 -36

Function-oriented design • Design with functional units • Thousands of systems have been developed

Function-oriented design • Design with functional units • Thousands of systems have been developed using this approach • Supported directly by most programming languages • Most design methods are functional in their approach

Functional design process • Data-flow design – Model the data processing in the system

Functional design process • Data-flow design – Model the data processing in the system using data-flow diagrams • Structural decomposition – Model how functions are decomposed to subfunctions using graphical structure charts • Detailed design – The entities in the design and their interfaces are described in detail.

Data flow diagrams • Show an input data item is functionally transformed by a

Data flow diagrams • Show an input data item is functionally transformed by a system into an output data item • Are an integral part of many design methods • May be translated into either a sequential or parallel design.

DFD notation Get design name Design name Entity names Design database Get entity names

DFD notation Get design name Design name Entity names Design database Get entity names Sorted names Data dictionary Produce link report Look up entity names and Link descriptions Design entity descriptions Link report Node report and Sort by type Integrate reports Integrated report Produce node report Node descriptions Print report

Decomposition guidelines • For business applications, the top-level structure chart may have three functions

Decomposition guidelines • For business applications, the top-level structure chart may have three functions namely input, process and output • Data validation functions should be subordinate to an input function • Coordination and control should be the responsibility of functions near the top of the hierarchy

Coupling and cohesion

Coupling and cohesion

Process steps • Identify system processing transformations – Transformations in the DFD which are

Process steps • Identify system processing transformations – Transformations in the DFD which are concerned with processing rather than input/output activities. • Identify input transformations – Transformations concerned with reading, validating and formatting inputs. Group under the input function • Identify output transformations – Transformations concerned with formatting and writing output. Group under the output function

Detailed design • Produce a short design specification of each function. This should describe

Detailed design • Produce a short design specification of each function. This should describe the processing, inputs and outputs • These descriptions should be managed in a data dictionary

Example: Data dictionary entries

Example: Data dictionary entries