The Big Picture - Weber State Universityicarus.cs.weber.edu/~dab/cs1410/slides/Chap01.pdf ·...
Transcript of The Big Picture - Weber State Universityicarus.cs.weber.edu/~dab/cs1410/slides/Chap01.pdf ·...
The Big PictureChapter 1
Copyright © 1998-2004 Delroy A. Brinkerhoff. All Rights Reserved.CS 1410 Chapter 1 Slide 1 of 18
Language LevelsWhere do they all go?
PHigh-level< Close to problem< System independent
PLow-level< Close to system< Doesn’t reflect problem
Java, C#
FORTRAN, COBOL, C++
C/C++
AssemblerMachine
CS 1410 Chapter 1 Slide 2 of 18
Relationship Between C and C++C++ is (almost) a perfect subset of C
ANSIC
C++
mainfunctionssyntaxoperatorsflow of controlorganizationcompilationlibraries
object-orientedmodel
prototypesconstinlinereferencesnew / delete
classesencapsulationinheritancepolymorphismRTTItemplatesctors/dtorsoverloading
CS 1410 Chapter 1 Slide 3 of 18
Computer LanguagesThe Lineage
FORTRAN º ALGOL58 (IAL) º ALGOL60 º CPL º BCPL º B
CObjective C
C++ º Java º C#
CS 1410 Chapter 1 Slide 4 of 18
The Development GoalFrom problem domain to working system
Procedural / Structured
Data Driven
Object-Oriented
Ad Hoc
CS 1410 Chapter 1 Slide 5 of 18
Procedural ModelThe oldest model
PFocuses on how (i.e., the algorithms) to solve a problemPDecomposes problem into procedures or subroutinesPTwo kinds of data (i.e., data defined in two different scopes)< Local data is defined in and is only accessible within a procedure< Global data is defined outside of a procedure and is accessible
throughout the programPGlobal data results in procedural coupling< Changes have wide spread and often unexpected effects< Global data makes the program fragile
PCoupled procedures must be< Developed as a unit< Debugged as a unit< Validated as a unit
CS 1410 Chapter 1 Slide 6 of 18
Data Driven ModelsAn early attempt to improve procedural programming
PData flow< Maps data input to data output< Design data structures first< Design processes / functions last
PData hiding< Packages data and the procedures that work
on the data together in a module (a file in C)< Data is still in global scope but access is
allowed only through the module functionsPAbstract Data Type (ADT)< Programmer created data type< struct in C
process
data
data
CS 1410 Chapter 1 Slide 7 of 18
Object ModelState of the art
PCharacteristics of functional & data modelsPA tool for managing complexityPChange resilient < Change is localized< Intra-object functions may be coupled< Extra-object functions are decoupled
PNatural organization for data and functions< Objects encapsulate data and functions
together< Supports ADTs: multiple objects of a type may
be created (class is a type specifier or ADT)< Supports data hiding: data access is controlled
through key words
Name
AttributesData / variables
BehaviorsOperationsFunctions
UML classsymbol
CS 1410 Chapter 1 Slide 8 of 18
Object-Oriented ModelThe big picture
P “Object-oriented modeling and design is a new way ofthinking about problems using models organized aroundreal-world concepts. The fundamental construct is theobject, which combines both data structure and behavior in asingle entity.”
James Rumbaugh, Object-Oriented Modeling and DesignPData Structure (attribute)< variable < instance field < data < data field< data member < instance variable < state
PBehavior< method < function< member function < operation< service< sending a message is equivalent to calling a function
CS 1410 Chapter 1 Slide 9 of 18
ObjectsThe central actor in the object model
PEntities that make sense in an application contextPSingle, specific instance of a class< Objects with the same attributes and data types are described by a
single class< Each object has a distinct identity or handle and is uniquely
addressablePData in each object is distinct from the data in all other
objects instantiated from the same class< An object has explicit boundaries
PEncapsulation is the first defining characteristic of the objectmodel< Objects and encapsulation are synonymous< Seals attributes and behaviors together into a single unit
CS 1410 Chapter 1 Slide 10 of 18
ClassesDefining characteristics
PAn abstraction of one or more objectsPDescribe “things” with similar attributes and behaviorsPProvides data hiding< Data is in a unique scope and access is controlled< Accessed through public interface (methods or member functions)
P Implements Abstract Data Types (ADT)< Creates a new type specifier< Separates implementation (data) from the interface (public
functions)PTemplate, blueprint, or cookie cutter
CS 1410 Chapter 1 Slide 11 of 18
AttributesDescribe an object
PCharacterize or distinguish an objectPAre data values (variables) held by objectsPAre the data an object is responsible for maintainingPShould be placed at the highest level in an inheritance
hierarchy where they remain applicable to each descendantP “Good” attributes depend on what is being modeled
CS 1410 Chapter 1 Slide 12 of 18
Behaviors / OperationsMember functions or methods
PA function or transformation that may be applied to or by anobject
POperations, behaviors, and services are logical (user visible);member functions are the physical functions that implementbehaviors or operations
PCalled through or bound to an object; that object is an implicittarget (the function operates on the data stored in the object)
PSome operations may be applied to many classes and arepolymorphic (i.e., implemented through multiple methods)
CS 1410 Chapter 1 Slide 13 of 18
foo my_foo;my_foo.function();
Class RelationshipsRepresenting systems as collections of related classes
PAttempt to mimic the relationships between objects in thereal world
PAre depicted as diagrams or connected graphs< Nodes or vertices are classes< Edges, arcs, or paths denote the relationship
PAllow objects to cooperate in the overall solution of aproblem
PAre supported by specific computer-language syntax
CS 1410 Chapter 1 Slide 14 of 18
InheritanceGeneralization, gen-spec, is-a, is-a-kind-of; is-like-a, simile
PThe child class inherits all ofthe attributes and memberfunctions collectively calledfeatures) owned by the parentclass
PThe second definingcharacteristic of the objectmodel
Base ClassSuperclassParent ClassGeneralizationAncestor
Derived ClassSubclassChild ClassSpecializationDescendantUML
CS 1410 Chapter 1 Slide 15 of 18
Methods With The Same NameOverloading vs Overriding
POverloading functions (and operators)< Defined in the same class< Have the same name < Must have different signatures< May have different return types
POverriding functions< Two or more classes related through inheritance< One function defined in a super class, an other in
a subclass class< Have the same name< Have the same signatures and return type
PCircle::draw overrides or hides Shape::drawPThe move functions are overloaded
Shape
Circle
draw( )
draw( )move(int,int)move(int)
CS 1410 Chapter 1 Slide 16 of 18
PolymorphismMany shapes: late, run-time, or dynamic binding; also dynamic dispatch
PSelection of the correct method is deferred until run-timewhen the selection is based on the current object
PObjects respond differently to the same messagePRequires Inheritance; virtual, overridden functions; address
variables (pointer or reference)PThird defining feature of the object-oriented model
CS 1410 Chapter 1 Slide 17 of 18
Polymorphism ExampleDynamic binding
positioncolor
draw( )erase( )move( )
Shape
radius
draw( )erase( )
Squareside
draw( )erase( )
Triangleside1side2side3
draw( )erase( )
Circle
Shape* s
Exact shape selecteddynamically at runtime,perhaps in response touser input.
s->draw( ); Which draw method iscalled? Cannotdetermine at compiletime-- selection deferreduntil runtime.
CS 1410 Chapter 1 Slide 18 of 18