Classes and Object-Oriented Programming by xiaopangnv


									              Classes and Object-Oriented
• The essence of object-oriented programming is that
  you write programs in terms of objects in the domain
  of the problem you are trying to solve, so part of the
  development process is designing a set of types to
  suit the context.
• For a program to analyze bandy scores, you may
  have types such as Player and Team.
• The variables of the basic types don’t allow you to
  model real-world objects adequately.

Classes and Object-        tMyn                            1
• A structure as we have defined it up to now is a big
  step forward, but there is more to it than that.
• As well as the notion of user-defined types, object-
  oriented programming implicitly incorporates a
  number of additional important ideas, namely
  encapsulation, data hiding, inheritance and

Classes and Object-       tMyn                           2
•   An object will contain a specific set of data values
    that describe the object in sufficient detail for your
•   An object will also contain a set of functions that
    operate on it – functions that will use or change the
    set of data values.
•   They define the set of operations that can be applied
    to the object – what you can do with it, or to it.
•   Every object of a given type will incorporate the same
    combination of these things: the set of data values as
    data members, and the set of operations as member
Classes and Object-         tMyn                         3
• This packaging of data values and functions within an
  object is referred to as encapsulation.

Classes and Object-       tMyn                        4
• Wikipedia:

    The term encapsulation is often used interchangeably
    with information hiding, while some make distinctions
    between the two. It seems that people, however, fail
    to agree on the distinctions between information
    hiding and encapsulation though one can think of
    information hiding as being the principle and
    encapsulation being the technique. A software
    module hides information by encapsulating the
    information into a module or other construct which
    presents an interface.

Classes and Object-        tMyn                         5
                             A loanAccount object

                                                    The data members define
                              balance: 16000€
                                                    the properties that
An object contains            interestRate: 22%
                                                    distinguish the object.
everything necessary
to define its properties
                                                    The member functions of
and define its operations.     calcInterest()       the object define what you
                                                    can do with it.

 Classes and Object-                   tMyn                                  6
  Data hiding:
• Ideally, the data members of a loanAccount object
  would be protected from direct outside interference,
  and would only be modifiable in a controlled way.
• The ability to make the data values for an object
  generally inaccessible is called data hiding.

Classes and Object-       tMyn                           7

    In computer science, the principle of information
    hiding is the hiding of design decisions in a computer
    program that are most likely to change, thus
    protecting other parts of the program from change if
    the design decision is changed. Protecting a design
    decision involves providing a stable interface which
    shields the remainder of the program from the
    implementation (the details that are most likely to
    change). In modern programming languages, the
    principle of information hiding manifests itself in a
    number of ways, including encapsulation (given the
    separation of concerns) and polymorphism.
Classes and Object-         tMyn                         8
                        A loanAccount object

Generally, the data
                                               The data members
members should           balance: 16000€
                                               of an object should
not be accessible        interestRate: 22%
                                               generally be hidden.
from outside.
                                               The member functions of the
The member functions
                          calcInterest()       object provide the tools to
can provide the means
                                               alter the data members
to alter data members
                                               in a controlled way.
when necessary.

 Classes and Object-              tMyn                                9
• The member functions of the object can provide a
  mechanism that ensures any changes to the data
  members follow a particular policy, and that the
  values set are appropriate.
• Hiding the data belonging to an object prevents it
  from being accessed directly, but you can provide
  access through functions that are members of the
  object, either to alter a data value in a controlled way,
  or simply to obtain its value.
• Hiding the data within an object is not mandatory, but
  it’s generally a good idea for at least a couple of

Classes and Object-         tMyn                         10
• Maintaining the integrity of an object requires that you
  control how changes are made.
• Direct access to the values that define an object
  undermines the whole idea of object-oriented
• Object-oriented programming is supposed to be
  programming in terms of objects, not in terms of the
  bits that go to make up an object.
• You can think of the data members as representing
  the state of the object, and the member functions that
  manipulate them as representing the object’s
  interface to the outside word.

Classes and Object-        tMyn                         11
• A program using the class interface is only
  dependent on the function names, parameter types
  and return types specified for the interface.
• The internal mechanics of these functions do not
  affect the program creating and using objects of the

Classes and Object-       tMyn                           12
• Inheritance is the ability to define one type in terms of
• So inheritance is a relationship called generalization
  which is a specialization/generalization relationship in
  which objects of the specialized element (the child)
  are substitutable for objects of the generalized
  element (the parent).
• It is the mechanism by which more-specific elements
  incorporate the structure and behaviour of more-
  general elements.

Classes and Object-         tMyn                         13
• Graphically, generalization is rendered as a solid
  directed line with a large open arrowhead, pointing to
  the parent.
• A class that has exactly one parent is said to use
  single inheritance; a class with more than one parent
  is said to use multiple inheritance.

Classes and Object-       tMyn                         14

                      move(d: direction; s: speed)

      steeringMotor                                  mainMotor

Classes and Object-                    tMyn                      15
• Wikipedia:

    Inheritance is a way to form new classes using
    classes that have already been defined. The new
    classes, known as derived classes, take over (or
    inherit) attributes and behaviour of the pre-existing
    classes, which are referred to as base classes. It is
    intended to help reuse of existing code with little or
    no modification.

Classes and Object-          tMyn                            16
    Inheritance is also sometimes called generalization,
    because the is-a relationships represent a hierarchy
    between classes of objects. For instance, a "fruit" is a
    generalization of "apple", "orange", "mango" and
    many others. One can consider fruit to be an
    abstraction of apple, orange, etc. Conversely, since
    apples are fruit (i.e. an apple is-a fruit), apples may
    naturally inherit all the properties common to all fruit,
    such as being a fleshy container for the seed of a

Classes and Object-          tMyn                          17
    One common reason to use inheritance is to create
    specializations of existing classes. In specialization,
    the new class has data or behavior aspects that are
    not part of the inherited class. For example, a "Bank
    Account" class might have data for an "account
    number", "owner", and "balance". An "Interest
    Bearing Account" class might inherit "Bank Account"
    and then add data for "interest rate" and "interest
    accrued" along with behavior for calculating interest

Classes and Object-          tMyn                         18
•   The word polymorphism means the ability to assume
    different forms at different times.
•   It means that, in a hierarchy of classes, you can
    specify operations with the same signature at
    different points in the hierarchy.
•   Ones in the child classes override the behaviour of
    ones in the parent classes.
•   When a message is dispatched at run time, the
    operation in the hierarchy that is invoked is chosen
    polymorphically – that is, a match is determined at
    run time according to the type of the object.

Classes and Object-        tMyn                        19
• Collectively, the name of an operation plus its
  parameters (including its return type, if any) is called
  the operation’s signature.
• Polymorphism in C++ always involves calling a
  member function of an object, using either a pointer
  or a reference.
• Such a function call can have different effects at
  different times.
  Polymorphism means that objects belonging to a
  family of inheritance-related classes can be passed
  around and operated on using base class pointers
  and references.

Classes and Object-         tMyn                             20
• The particular function called through the pointer is
  not decided when your program is compiled, but
  when your program executes.
• Thus, the same function call can do different things
  depending on what kind of object the pointer points

Classes and Object-        tMyn                           21
• Wikipedia:

    Polymorphism is the ability of objects belonging to
    different types (classes) to respond to method calls
    (member function calls) of methods of the same
    name, each one according to an appropriate type-
    specific behaviour. The programmer (and the
    program) does not have to know the exact type of the
    object in advance, so this behavior can be
    implemented at run time (this is called late binding or
    dynamic binding).

Classes and Object-         tMyn                         22
    In practical terms, polymorphism means that if class
    B inherits from class A, it doesn’t have to inherit
    everything about class A; it can do some of the things
    that class A does differently. This means that the
    same member function can result in different actions
    as appropriate for a specific class.

Classes and Object-         tMyn                        23
•   A class is a user-defined data type.
•   It is a description of a set of objects that share the
    same attributes, operations, relationships and
•   The variables and functions declared within a class
    are called members of the class.
•   The variables are called data members and the
    functions are called member functions.
•   The member functions of a class are sometimes
    referred to as methods. More generically, a method is
    the implementation of an operation.

Classes and Object-         tMyn                        24
• Having defined a class, we can declare variables of
  the class type (also called instances of the class).
• Each instance will be an object of the class.
• An object is a concrete manifestation of an
  abstraction; an entity with a well-defined boundary
  and identity that encapsulates state and behaviour;
  an instance of a class.
• OOP is a programming style based on the idea of
  defining your own data types as classes.
• It involves the ideas of encapsulation of data, class
  inheritance and polymorphism.

Classes and Object-        tMyn                           25

To top