CS代考计算机代写 c# c++ compiler cache python chain flex Java Abstraction in Software

Abstraction in Software

CPSC 3200 OODevelopment

Midterm Review

Microsoft, 2001
Proprietary Java (precursor: Visual J++)
.NET platform
Goals similar to Java
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

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


C++ 11/14
Backward compatible with C++ 98
Emphasis on improved support for
Performance => Move semantics
Stable Memory => Smart Pointers
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?

How do these difference impact?
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
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?
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

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
Set initial state
Allocate resources
=> No need for initialize()
get() – should be const
Controlled peek inside class
Mutators — minimize
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:
CPSC 3200 Dingle

CPSC 3200 Dingle
Copying: Key Design Decision
NO DECISION => Aliasing (Shallow Copy)
Bitwise copy => address copied

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

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

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


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
1:1 for inheritance => parent component constructed before child component
variable by design for composition
Permanent for inheritance
Possibly transient for composition
Temporary for containment
Copyright@2014 Taylor & Francis Adair Dingle All Rights Reserved


=> Class design starts with complete internal control

Class designer may choose what to
=> 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:
Set once
Property (Setter)
Increased dependency on client
Method (Interface)

Dingle 3200


Child object may serve as substitute for parent
PARENT interface defines utility of relationship

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
Class design only
Add keyword ‘virtual’ to method in base class
Add keyword ‘override’ to method in derived class

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


Leave a Reply

Your email address will not be published. Required fields are marked *