OOP Class Lawrence D - PowerPoint PPT Presentation

About This Presentation
Title:

OOP Class Lawrence D

Description:

OOP Class Lawrence D Antonio Lecture 3 An Overview of C++ Polymorphism Virtual functions Overloading Type conversions Templates Typing Statically typed. – PowerPoint PPT presentation

Number of Views:142
Avg rating:3.0/5.0
Slides: 29
Provided by: Lawrence133
Category:

less

Transcript and Presenter's Notes

Title: OOP Class Lawrence D


1
OOP ClassLawrence DAntonio
  • Lecture 3
  • An Overview of C

2
Classes/Objects
  • Basic unit of OOPL
  • Autonomous entities
  • State, behavior, identity
  • Data encapsulation
  • Encapsulation of responsibilities
  • Information hiding
  • Message passing
  • Exception handling

3
Object Organization
  • Inheritance (Single and multiple)
  • Dynamic binding
  • Generalization/Specialization
  • Abstraction
  • Has-a versus is-a relationship
  • Uses relationship
  • Polymorphism/ Virtual functions

4
Polymorphism
  • Virtual functions
  • Overloading
  • Type conversions
  • Templates

5
Generic programming
  • Templates
  • STL
  • Traits
  • Metaprogramming
  • Boost library
  • Programming with concepts

6
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

7
Code Reuse
  • Parametric polymorphism
  • Subtyping polymorphism
  • Composition
  • Patterns

8
What is a class?
  • A class is a set of objects sharing common
    features.
  • A class defines an objects 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.

9
What is an object?
  • An object is an instance of a class.
  • An object has state, behavior, identity.

10
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.

11
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.

12
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.

13
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.

14
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.

15
What is an object?
  • Shlaer-Mellor
  • An object is an abstraction of a set of
    real-world 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.

16
What is an object?
  • Jacobson
  • An object is characterized by a number of
    operations and a state which remembers the effect
    of these operations.

17
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.

18
Data encapsulation example
  • class Clock
  • private
  • int hours // 1-12 private
  • int minutes // 0-59
  • public
  • Clock(int h, int m)
  • if (h lt 1 h gt 12)
  • throw(Hours must be between 1 and
  • 12?)
  • if (m lt 0 m gt 59)
  • throw(Minutes must be between 0 and
  • 59?)
  • h hours
  • m minutes
  • //...

19
Class Invariants
  • The above is an example of Programming by
    Contract.
  • The class guarantees that
  • These are called class invariants

20
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.

21
What is message passing?
  • Messages are transfers of data or requests for
    another object to take an action.

22
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.

23
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

24
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 variables type may change.

25
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

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

27
Weak typing example
  • var x 5
  • var y "37"
  • Print(x y)
  • In Visual Basic this prints 42
  • In JavaScript this prints 537

28
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.
Write a Comment
User Comments (0)
About PowerShow.com