Abstraction in Software
CPSC 3200 OODevelopment
Midterm Review
C#
Microsoft, 2001
Proprietary Java (precursor: Visual J++)
.NET platform
Goals similar to Java
Portability
Safety (Managed Code)
Garbage collection
Run-time checks
Pointer construct only viable in UNSAFE code
delegate construct (like function pointer)
+ Programmer Productivity
Differences from Java
Limited operator overloading
Getter/setter (properties)
No checked exceptions
Choice of method binding
CPSC 3200 Dingle
What is the computing environment in the 1990s?
Efficiency not a concern
processors cheaper and faster
more cache
BUT memory access bottleneck
Faster development time
BUT Java 2X or more slower than C++
CPSC 3200 Dingle
C++
Bjarne Stroustrup, ATT Bell labs, 1985
OO extension of C => backward compatible with C
Support all C features
C code compiled by C++ compiler
Efficiency still primary goal
“pay only for what you use”
Stack allocation default
Static binding default
HL abstraction with low-level manipulations permitted
=> the pointer construct
Quickly became industry standard
Not considered safe
Omission of run-time checks
Memory management problems
3
3
C++ 11/14
Backward compatible with C++ 98
Emphasis on improved support for
Performance => Move semantics
Stable Memory => Smart Pointers
Multithreading
Generics
Appeal
Like other PLs, move closer to Python & functional languages
Support for lambda and regular expressions
Software Construction for Longevity
Self-documenting code & Design consistency
CPSC 3200 Dingle
C# vs C++
What are the major differences?
Relevant to memory?
OOD?
How do these difference impact?
Client?
Class design?
CPSC 3200 Dingle
C# vs C++ differences
Object allocation
All C# objects allocated via new on the heap
C++ objects allocated on stack by default
C++ heap objects allocated via new and addressed via pointers
Object deallocation
C# implicit deallocation — garbage collector
C++ ‘automatic’ deallocation of stack objects
Compiler patches in calls to destructors of all objects on stack frame
C++ explicit deallocation of heap objects via delete
Copying
Shallow copying (of addresses) yields aliases
C++ class design requires explicit decisions wrt copying
Modern C++ should define move semantics alongside deep copying
CPSC 3200 Dingle
C# vs C++ difference
C# zero initializes data; C++ does not. Why?
Arrays
No bounds checking in C++. Why?
=> Must check indices for security
C# array of objects == array of references
C++ array of objects depends on provision of no-argument constructor
Polymorphism
Both use static binding as default (for efficiency)
=> Must tag methods as virtual to get dynamic binding
C++ clients must use pointers. Why?
CPSC 3200 Dingle
Elements of Class Design
Constructor(s)
Set initial state
Allocate resources
=> No need for initialize()
Accessors
get() – should be const
Controlled peek inside class
Mutators — minimize
set()
Controlled alteration of state
Check values
discard out-of-bounds values
provide default values
Key TYPE functionality
Public methods that provide client with expected functionality
Private/protected methods to support internal control & type consistency
Dingle CPSC 3200
Structural Decomposition
=> Relationships
Has-A Composition
Holds-A Containment
Is-A Inheritance
When to use each type of relationship?
What are the effects/costs/benefits of each design?
What is the impact of each relationship on:
CLIENT?
CLASS DESIGNER?
CPSC 3200 Dingle
CPSC 3200 Dingle
Copying: Key Design Decision
NO DECISION => Aliasing (Shallow Copy)
Bitwise copy => address copied
DANGER
Suppress Copying
C++: private copy constructor and operator=
Support (Deep) Copying
C++: DEFINE public copy constructor and operator=
C#: DEFINE public ShallowCopy() and DeepCopy() methods
C++11: MOVE SEMANTICS
Design for efficiency WHEN DEEP Copying (saves new/delete)
Use ‘&&’ to flag compiler
Copying replaced with ownership transfer by compiler
When appropriate (TEMPORARIES)
Enhances performance (does not yield persistent unintended aliases)
10
Show memory diagrams
C# Cloning Design: Internal Type reclamation
public class uCopy
{ private anotherClass refd;
…
private object Clone()
{ uCopy local = this.MemberwiseClone() as uCopy;
local.refd = this.refd.Clone() as anotherClass;
return local;
}
…
public uCopy ShallowCopy()
{ return (uCopy)this.MemberwiseClone(); }
// internal type reclamation
public uCopy DeepCopy()
{ uCopy u2 = this.Clone() as uCopy;
}
}
Dingle 3200
The other class also needs to have implemented Clone() and used ICloneable interface
11
Suppress Copying
Deep copying more expensive than shallow copying.
Shallow copying may lead to data corruption.
Copying may be suppressed when undesirable
Do NOT duplicate large registries (hash tables, etc. )
Preserve sole ownership of resource
Data integrity
Generation of temporaries
Transient use
Functionality using resource may not be exercised
Dingle 3200
12
C++ deep copying
// private utility for copying
void noLeak::copy(const noLeak& src)
{ size = src.size;
heapData = new int[size];
for (int k = 0; k < size; k++)
heapData[k] = src.heapData[k];
}
// copy constructor supports call by value
noLeak::noLeak(const noLeak& src)
{ copy(src); }
// overloaded= supports deep copying; check for self-assignment
noLeak& noLeak::operator=(const noLeak& src)
{ if (this = = &src) return *this; // guard clause
delete[] heapData;
copy(src);
return *this;
}
Dingle 3200
Return type on = could be void since it simply works on itself, but then no chaining is allowed (because nothing is returned)
13
C++ 11 move semantics: denoted by “&&”
// move constructor supports efficient call by value
// heap memory transferred
// caller loses access to heap memory
noLeak::noLeak(noLeak&& src)
{
// simply copy size and address to heap memory
size = src.size;
heapData = src.heapData;
// zero out caller’s handle to heap memory
src.size = 0;
src.heapData = nullptr;
}
Dingle 3200
“move” constructor & “move” assignment operator
14
C++ 11 move semantics: denoted by “&&”
// move assignment exchanges ownership
// reference returned to support chained assignment
noLeak& noLeak::operator=(noLeak&& src)
{
swap( size, src.size);
// LHS swaps ownership with RHS
// no need for distinct copy if RHS goes out of scope
swap( heapData, src.heapData);
return *this;
}
Dingle 3200
“move” constructor & “move” assignment operator
15
Structural Design Details
Cardinality: How many subObjects?
1:1 for inheritance
1-many for composition
Variable for containment
Ownership
isA: Child owns Parent component => parent component may NOT be released
hasA: Composing object may stub out/replace subObject
holdsA: None, usually, since containers are mostly just a data store
Lifetime
1:1 for inheritance => parent component constructed before child component
variable by design for composition
Association
Permanent for inheritance
Possibly transient for composition
Temporary for containment
Copyright@2014 Taylor & Francis Adair Dingle All Rights Reserved
Composition
Encapsulation
=> Class design starts with complete internal control
Class designer may choose what to
Vary
Expose
=> More responsibility, More control
CPSC 3200 Dingle
Dependency Injection
Dependencies must go somewhere
…
Selective externalization of resource
=> increase cllent responsibility
=> increase class design for error response
Three approaches – more than one may be used in a class design:
Constructor
Set once
Stable
Property (Setter)
Flexible
Increased dependency on client
Method (Interface)
Isolated
Dingle 3200
Inheritance
Substitutability
Child object may serve as substitute for parent
PARENT interface defines utility of relationship
Polymorphism
Type extensibility
Heterogeneous collections
CPSC 3200 Dingle
Static vs. Dynamic Binding
Static binding
translates function calls directly into jump statements
function invoked at the point of call cannot vary
No run-time overhead
No run-time flexibility
Dynamic binding
postpones function call resolution until run-time
function invoke at the point of call can vary
great flexibility
Run-time overhead
supports polymorphism and heterogeneous collections.
Copyright@2014 Taylor & Francis Adair Dingle All Rights Reserved
C#/C++ dynamic binding
C#
Class design only
Add keyword ‘virtual’ to method in base class
Add keyword ‘override’ to method in derived class
C++
Class design
Add keyword ‘virtual’ to method in base class
C++11 keyword ‘override’ for method in derived class: OPTIONAL
Application code
Use base class pointers
CPSC 3200 Dingle
Heterogeneous Collection
Class hierarchy uses virtual functions
variant behavior (based on subtype)
Traversal of heterogeneous collection
Yields streamline execution of varying functionality
Masks subtype construction and evaluation
Type extensibility supported
Stable client code even if new subtype(s) added
Copyright@2014 Taylor & Francis Adair Dingle All Rights Reserved
/docProps/thumbnail.jpeg