OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

Download Report

Transcript OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

OOP Class
Lawrence D’Antonio
Lecture 3
An Overview of C++
Classes/Objects
 Basic unit of OOPL
 Autonomous entities
 State, behavior, identity
 Data encapsulation
 Encapsulation of responsibilities
 Information hiding
 Message passing
 Exception handling
Object Organization
 Inheritance (Single and multiple)
 Dynamic binding
 Generalization/Specialization
 Abstraction
 Has-a versus is-a relationship
 Uses relationship
 Polymorphism/ Virtual functions
Polymorphism
 Virtual functions
 Overloading
 Type conversions
 Templates
Generic programming
 Templates
 STL
 Traits
 Metaprogramming
 Boost library
 Programming with concepts
Typing
 Statically typed. Variables must be
declared before used. Exceptions: casts,
unions, temporary objects.
 Weakly typed (?). Variables are not bound
to a specific data type.
 Type conversions
 Subtyping
Code Reuse
 Parametric polymorphism
 Subtyping polymorphism
 Composition
 Patterns
What is a class?
 A class is a set of objects sharing common




features.
A class defines an object’s attributes and
behavior. Methods are provided to act on
an object and to pass messages between
objects.
A class is the basic unit of abstraction.
A class is the basic unit of modularity.
A class can be concrete or abstract.
What is an object?
 An object is an instance of a class.
 An object has state, behavior, identity.
What is an object?
Coad-Yourdon
An abstraction of something in a problem
domain, reflecting the capabilities of the
system to keep information about it, interact
with it, or both; an encapsulation of attribute
values and their exclusive services.
What is an object?
OMG
An object is a thing. It is created as the
instance of an object type. Each object has
a unique identity that is distinct from and
independent of any of its characteristics.
Each object offers one or more operations.
What is an object?
Firesmith
An object is defined as a software abstraction that
models all relevant aspects of a single tangible or
conceptual entity or thing from the application
domain or solution space. An object is one of the
primary entities in an object-oriented application,
typically corresponds to a software module, and
consists of a set of related attribute types,
messages, exceptions, operations, and optional
component objects.
What is an object?
Booch
From the perspective of human cognition, an
object is any of the following:
 A tangible and/or visible thing.
 Something that may be apprehended
intellectually.
 Something toward which thought or action is
directed.
What is an object?
Booch continued.
An object has state, behavior, and identity;
the structure and behavior of similar objects
are defined in their common class; the terms
instance and object are interchangeable.
What is an object?
Shlaer-Mellor
An object is an abstraction of a set of realworld things such that:
 All the things in the set have the same
characteristic.
 All instances are subject to and conform to
the same set of rules and policies.
What is an object?
Jacobson
An object is characterized by a number of
operations and a state which remembers the
effect of these operations.
What is encapsulation?
 Internal details of objects are concealed
from the objects users (information hiding).
 Both data and implementation may be
hidden. The object is a black box.
 Access to members is controlled through
the class definition.
 The accessible part of a class is called its
interface.
Data encapsulation example
class Clock {
private:
int hours; // 1-12 private
int minutes; // 0-59
public:
Clock(int h, int m) {
if (h < 1 || h > 12) {
throw(”Hours must be between 1 and
12″);
}
if (m < 0 || m > 59) {
throw(”Minutes must be between 0 and
59″);
}
h = hours;
m = minutes;
}
//...
};
Class Invariants
 The above is an example of “Programming by
Contract.”
 The class guarantees that
1  hours  12, 0  minutes  59
 These are called class invariants
What is a method?
 A method is a member function that acts
upon an object. A method is generally
called for one object (exception: static
members).
 Commonly found methods are
constructors, destructors, assignment,
mutators, accessors.
What is message passing?
 Messages are transfers of data or
requests for another object to take an
action.
What is polymorphism?
 Different types of objects respond to the
same message and use the appropriate
method.
 Parametric polymorphism parametrizes
the object type (e.g., a list class, where the
type of object stored is parametrized).
 Subtype (or inclusion) polymorphism
allows objects of a given type to be
substituted for by objects of a subtype.
What is inheritance?
 One class (derived/child) relies on the




definition of another class (base/parent).
Single vs. multiple inheritance
A method of sharing code or sharing
interface among classes.
Language may define a class tree (with
single root): Java, Smalltalk
Language may define a class forest: C++
What is typing?
 Static typing: Data type determined at
compile-time. Type must be declared.
 Dynamic typing: Data type may be
determined at run-time. Type need not be
declared.
 Strong typing: Variables are bound to a
specific type.
 Weak typing: A variable’s type may
change.
Varieties of typing
 Static and strong typing: Java, Pascal,
OCaml, Haskell
 Static and weak typing: C/C++
 Dynamic and strong typing: Python
 Dynamic and weak typing: PHP
Dynamic typing example
# Python example
class Cat: def speak(self): print "meow!"
class Dog: def speak(self): print "woof!"
class Bob: def speak(self): print "hello world!"
def command(pet): pet.speak()
pets = [ Cat(), Dog(), Bob() ]
for pet in pets:
command(pet)
Weak typing example
var x := 5;
var y := "37";
Print(x + y);
In Visual Basic this prints: 42
In JavaScript this prints: 537
What is exception handling?
 The mechanism used to report and
recover from abnormal states.
 When an error is detected, execution is
passed to a handler.