Home

Programming

Page under construction...

Object-Oriented

Object-oriented (OO) technology has evolved as a means of managing the complexity inherent in many different kinds of systems.
the task of the software development team is to engineer the illusion of simplicity.
Which is the right way to decompose a compex system---by algorithms or by objects?  Both views are important: The algorithmic view higlights the ordering of events, and the object-oriented view emphasizes the agents that either cause action or are the subjects on which these operations act.

OBJECT-ORIENTED PROGRAMMING (OOP): Object-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of a hierarchy of classes united via inheritance relationships.

OBJECT-ORIENTED DESIGN (OOD): Object-oriented design is a method of design encompassing the process of object-oriented decomposition and a notation for depicting both logical and physical as well as static and dynamic models of the system under design.

OBJECT-ORIENTED ANALYSIS (OOA): Object-oriented analysis is a method of analysis that examines requirements from the perspective of the classes and objects found in the vocabulary of the problem domain.

The beauty of OO technology and C++ is that public inheritance can be used to reflect the fundamental, immutable properties of the problem domain and the domain model.  In this way, the public inheritance relationships model the problem domain

fundamental concepts of object-oriented technology: objects, classes, inheritance
in addition: polymorphism and dynamic binding [with above, five features]
goals of OO: abstraction, encapsulation, comprehensibility, changeability, reusability

present and justify OO as an effective tool for achieving the desired business results rather than as a "cool" techie thing.  Show why it's relevant.

The world is adopting OO technology because OO can handle change more effectively than its predecessors

OO minimizes the ripple effect of enhancements.  OO 1) allows the past to make use of the future (via inheritance, polymorphism, dynamic binding) and 2) designs for comprehensibility (via abstraction and specification)

An object-oriented language allows, among other things, the complexity of a program to be hidden.
instead of trying to mold the problem into something familiar to the computer, the object-oriented approach adapts the computer to the problem.  The problem is examined for independent entities that relate to other parts of the problem---these entities are not chosen based on their "computerizability," but because they actually exist and have some physical or conceptual boundary that devides them from the rest of the problem.  These entities are represented as /objects/ in the computer program.  The goal is to have a one-to-one correspondence between entities in the physical problem and objects in the program.

How are OOA, OOD and OOP related? Basically, the products of object-oriented analysis serve as the models from which we may start an object-oriented design; the products of object-oriented design can be used as blueprints for completely implementing a system using object-oriented programming methods.

four major elements of the object model: 1. Abstraction, 2. Encapsulation, 3. Modularity, 4. Hierarchy
three minor elements of the object model: 1. Typing, 2. Concurrency, 3. Persistence

Benefits of the object model:
1) the use of the object model helps us to exploit the expressive power of object-based and object-oriented programming languages
2) the use of the object model encourages the reuse not only of software but of entire designs, leading to the creation of reusable application frameworks.
3) the use of the object model produces systems that are built on stable intermediate forms, which are more resilient to change.  This also means that such systems can be allowed to evolve over time, rather than be abandoned or completely redesigned in response to the first major change in requirements.

OOP gives a "mechanical advantage", also, you can thin of the problem in its own terms rather than the terms of the computer, OOP makes it easier for you to conceptualize the problem and keep the complexity of the solution under control.  The class mechanism creates a "firewall" between pieces of code, which makes it much easier to assemble the system and to change the design during assembly as your insights grow.  Also, and possibly most important, OOP makes it easy to extend the system, so it reduces the cost of maintenance.

programming in an object-oriented language means creating new types of data (called /classes/) and "teaching" those data types how to handle /messages/.  You "teach" a class what to do with a message by creating a /method/, which is simply a function associated with the class.  The user creates variables of a data type (objects or /instances/) and sends messages to those objects.  Objects can even seem to take on life: "send a message to an object, and let the object figure out what to do with it."

it is suggested that object-oriented programming is a direct result of the chaos that occurs when conventional languages are applied to very large problems

Technically, the domain of OOP is abstract data typing, inheritance, and polymorphism

OO should be viewed as behaviour-centric, not data-centric

object-oriented programming extends object-based programming to provide for type/subtype relationships.  This is achieved through a mechanism referred to as /inheritance/.

Types

every operand has a *type*

a C++ class is a type

declaring a new class in a program creates a new type: class design is /type/ design

By creating new types to describe the problem domain, C++ allows the programmer to write applications that are easier to understand.  The class facility allows the programmer to separate the details associated with the underlying implentation of the new type, about which only the implementor of the new type cares, from the definition of the interface and operations that are provided for users of the type.  With this separation there is less concern for the various bookkeeping aspects that make programming tedious.  Types fundamental to the application can be implemented once and reused.
The C++ class mechanism allows users to define their own data types.
classes are often called user-defined types
classes are typically used to define abstractions that do not map naturally to the built-in data types.

C++ has two kinds of types: built-in types and class types

types pervade C++ programs.  Every object, expression, and function has a type, and the type of any entity determines its behaviour.  With a single exception (the type of an object within an inheritance hierarchy that is accessed through a pointer or reference), the type of every entity is known at compile time.
in C++, types can be thought of as ways of structuring and accessing memory as well as ways of defining operations that can be performed on objects of the type.  That is, types specify both properties of data and operations on that data.
Although this book concentrates on using and building higher-level data structures, it is important to understand the primitive types used to build them.  These primitive types represent common abstractions that are close to the hardware, such as numbers (integral and floating point), characters (including wide characters for international character sets), truth values, and machine addresses (pointers, references, and arrays).  Literals, also often called constants, represent integer, floating-point, Boolean, character, or string values.

most features in C++ revolve around the ability to create new data types

a type does more than describe the constraints on a set of objects; it also has a relationship with other types.

a C++ class is a type

every name and every expression has a type that determines the operations that may be performed on it.
C++ offers a variety of fundamental types, which correspond directly to hardware facilities

every name (identifier) in a C++ program has a type associated with it

C++ has a set of fundamental types corresponding to the most common basic storage units of a computer and the most common ways of using them to hold data
boolean, character, integer types are called integral types
integral and floating-point types are collectively called arithmetic types
enumerations and classes are called user-defined types
the integral and floating-point types are provided in a variety of sizes to give the programmer a choice of the amount of storage consumed, the precision, and the range available for computations
the assumption is that a computer provides bytes for holding characters, words for holding and computing integer values, some entity most suitable for floating-point computation, and addresses for referring to those entities.  The C++ fundamental types together with pointers and arrays present these machine-level notions to the programmer in a reasonably implementation-independent manner.

every declaration must specify a type

/Data types/ define the way you use storage (memory) in the programs you write.  By specifying a data type, you tell the compiler how to create a particular piece of storage, and also how to manipulate that storage.
data types can be built-in or abstract.
C and C++ have four basic built-in data types: char, int, float, double.
specifiers: signed, unsigned, short, long

Objects

OBJECT: An object is an entity that has state, behaviour, and identity.  The structure and behaviour of similar objects are defined in their common class.  The terms /instance/ and /object/ are interchangeable.

object: software entity that combines state and behaviour

class: a programming construct that defines the common state and behaviour of a group of similar objects; that is, a class has a name, and it describes the state (member data) and services (member functions) provided by objects that are instances of that class.  The runtime system creates each object based on a class definition. anology: home is object, blueprint is class
int and float can be thought of as classes, variables of these types can be thought of as objects of the associated class

What is an object?
to a programmer, an object is a region of storage with associated semantics
to a designer, an object is any identifiable component in the problem domain

An ideal object is a service provider that is alive, resposible, and intelligent

You "teach" a class what to do with a message by creating a /method/, which is simply a function associated with the class.  The user creates variables of a data type (objects or /instances/) and sends messages to those objects.  Objects can even seem to take on life: "send a message to an object, and let the object figure out what to do with it."

objects should reflect behaviours and services

objects are intelligent agents that export useful services

Classes

CLASS: A class is a set of objects that share a common structure, common behaviour, and common semantics.

class: a programming construct that defines the common state and behaviour of a group of similar objects; that is, a class has a name, and it describes the state (member data) and services (member functions) provided by objects that are instances of that class.  The runtime system creates each object based on a class definition. anology: home is object, blueprint is class
int and float can be thought of as classes, variables of these types can be thought of as objects of the associated class

Classes are the fundamental packaging unit of OO technology.  Classes are a way to localize all the state (data) and services (typically member functions) associated with a cohesive concept.

the real purpose of a class is to provide services
a class's interface should be designed from the outside in

a C++ class is a type

declaring a new class in a program creates a new type: class design is /type/ design

By creating new types to describe the problem domain, C++ allows the programmer to write applications that are easier to understand.  The class facility allows the programmer to separate the details associated with the underlying implentation of the new type, about which only the implementor of the new type cares, from the definition of the interface and operations that are provided for users of the type.  With this separation there is less concern for the various bookkeeping aspects that make programming tedious.  Types fundamental to the application can be implemented once and reused.
The C++ class mechanism allows users to define their own data types.
classes are often called user-defined types
classes are typically used to define abstractions that do not map naturally to the built-in data types.

strive for class interfaces that are complete and minimal
a /complete/ interface is one that allows clients to do anything they might reasonably want to do.
a /minimal/ interface is one with as few functions in it as possible, one in which no two member functions have overlapping functionality
avoid data members in the public interface

You "teach" a class what to do with a message by creating a /method/, which is simply a function associated with the class.  The user creates variables of a data type (objects or /instances/) and sends messages to those objects.  Objects can even seem to take on life: "send a message to an object, and let the object figure out what to do with it."

Together with namespaces, classes are also a primary mechanism for information hiding.

a C++ class is a type
the key to writing good programs is to design classes so that each cleanly represents a single concept

key concept in C++ is class
a class is a user-defined type

the aim of the C++ class concept is to provide the prograamer with a tool for creating new types that can be used as conveniently as the built-in types

Polymorphism

In C++ inheritance facilitates polymorphism and dynamic binding.  Polymorphism allows objects of one class to be used as if they were objects of another, related class.

Advantages of polymorphism and dynamic binding
allow old code to call new code in a substitutable fashion
polymorphism and dynamic binding enable the ability to treat objects of derived classes as if they were objects of the base class

polymorphism is the ability of a parent type to refer to any of the subtypes that are inherited from it.  Dynamic binding is the ability to resolve at run-time which operation to execute based on the acual type of the polymorphic object.  In C++, this is handled through the virtual function mechanism.

Polymorphism is a concept in type theory wherein a name may denote instances of many different classes as long as they are related by some common superclass.  Any object denoted by this name is thus able to respond to some common set of operations in different ways.
Without polymorphism, the developer ends up writing code consisting of large case or switch statements.  The existence of a switch statement that selects an action based on the type of an object is often a warning sign that the developer has failed to apply polymorphic behaviour effectively.  With polymorphism, large case statements are unnecessary because each object implicitly knows its own type.  Polymorphism and late binding go hand in hand.  In the presence of polymorphism, the binding of a method to a name is not determined until execution.  In C++, the developer may control whether a member function uses early or late binding.  Specifically, if the method is declared as virtual, then late binding is employed, and the function is considered to be polymorphic.  If this virtual declaration is omitted, then the method uses early binding and thus can be resolved at the time of compilation.  Java simply performs late binding without the need for an explicit declaration such as virtual.

The fact that we can use a derived type where a pointer or reference to the base is expected is an example of a key concept in OOP called *polymorphism*.  This word, from the Greek /polymorphos/, meaning "of many forms," was already in use in English in the mid-nineteenth century.  In a programming context, it refers to the ability of one type to stand in for many types.  C++ supports polymorphism through the dynamic-binding properties of virtual functions.  When we call a virtual through a pointer or reference, we make a polymorphic call.  The type of the reference (or pointer) is fixed, but the type of the object to which it refers (or points) can be the type of the reference (or pointer) or any type derived from it.  Thus, we can potentially call one of many functions through a single type.

Polymorphism---implemented in C++ with virtual functions--means "different forms."  In object-oriented programming, you have the same face (the common interface in the base class) and different forms using that face: the different versions of the virtual functions.

polymorphism requires data abstraction and inheritance
if it isn't late binding, it isn't polymorphism

Polymorphism (implemented in C++ with *virtual* functions) is the third essential feature of an object-oriented programming language, after data abstraction and inheritance.
It provides another dimension of separation of interface from implementation, to decouple /what/ from /how/.  Polymorphism allows improved code organization and readability as well as the creation of /extensible/ programs that can be "grown" not only during the original creation of the project, but also when new features are desired.

Abstraction

ABSTRACTION: An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.

An extensible, domain-specific framework models the key abstractions of the problem domain.

abstraction: the ability to ignore details is characteristic of maturing technologies.  For example, early automobiles broke down so often that every driver had to be an amateur mechanic.
We define abstraction as selective ignorance---concentrating on the ideas that are relevant to the task at hand, and ignoring everything else---and we think that it is the most important idea in modern programming.  The key to writing a successful program is knowing which parts of the problem to take into account, and which parts to ignore.
functions, data structures, classes, and inheritance are all abstractions

An abstraction is a simplified view of an object in the user's own vocabulary.
In OO and C++, an abstraction is the simplest interface to an object that provides all the features and services the intended users expect.
An abstraction tells users everything they need to know anout an object but nothing else.
It is the well-defined, unambiguously specified interface.
The key to a good abstraction is deep knowledge of the problem domain
Abstractions should be user-centric (not developer-centric)

C++ supports multiple entities and libraries using /data abstraction/.  Data abstraction means you can combine the data structure and the operations on that data structure together into a new /abstract data type/.  An abstract data type behaves just like data types that are built into the language, except they aren't part of the language definition; they are something the programmer has created.

Abstraction is one of the fundamental ways that we as humans cope with complexity.
An abstraction focuses on the outside view of an object and so serves to separate an object's essential behaviour from its implementation

An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.

Abstraction focuses on the essential characteristics of some object, relative to the perspective of the viewer

Encapsulation

ENCAPSULATION: Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behaviour; encapsulation serves to separate the contractual interface of an abstraction and its implementation.

Encapsulation provides the explicit boundary between an object's abstract interface (its abstraction) and its internal implemenation details.

Encapsulation puts the implementation details "in a capsule".

in C++ the class, not the individual object, is the unit of encapsulation

combining like functions and data is called /encapsulation/

Encapsulation creates new data types by combining characteristics and behaviours.

Inheritance

inheritance is a powerful facility that can improve the clarity and extensibility of software

Inheritance is a powerful tool that enables extensibility.  It allows the software to capture the is-a or kind-of relationship (although as will be shown in FAQ 7.01, the phrase, "is substitutable for," more accurately captures the true meaning of inheritance).

The beauty of OO technology and C++ is that public inheritance can be used to reflect the fundamental, immutable properties of the problem domain and the domain model.  In this way, the public inheritance relationships model the problem domain

In C++ inheritance facilitates polymorphism and dynamic binding.  Polymorphism allows objects of one class to be used as if they were objects of another, related class.  Dynamic binding ensures that the code that is executed is always consistent with the type of the object

What are private inheritance and protected inheritance?
Has-a, not is-a.
[as opposed to public inheritance]

In C++, inheritance is for subtyping. It lets developers model the kind-of relationship. Inheritance allows developers to make one class a kind-of another class. 'base class' and 'derived class'

proper inheritance: when the advertised behaviour of the derived class is substitutable for that of the base class
proper inheritance and substitutable behaviour can be guaranteed when the specifications of the member functions of the derived class require no more and promise no less than the specifications of the corresponding member functions in the base class(es).

benefit of proper inheritance: substitutability and extensibility

subsets have nothing to do with inheritance

the main purpose of inheritance is to express an externally meaningful relationship that describes the behaviour of two entities within the problem domain.  This relationship is called the is-substitutable-for relationship, although sometimes the less accurate terms is-a or kind-of are used instead.

*inheritance*: the basic idea is that we can often think of one class as being just like another, except for some extensions.

use inheritance because you want to reuse a class that isn't /quite/ right, or to express a problem as a hierarchy of classes
inheritance is a way of classifying concepts.  Many problems can be cast into a hierarchy or tree of these concepts; the tree shows which concepts share common features.

*Inheritance* allows us to model classes that are similar to one another with exceptions.

Semantically, inheritance denotes an "is a" relationship.

inheritance is the most important "is a" hierarchy

inheritance is most useful in modelling large, complex systems.

The inheritance graph of C++ hierarchies should be a forest of short trees.

a type does more than describe the constraints on a set of objects; it also has a relationship with other types.  Inheritance expresses this similarity between types using the concept of base types and derived types.  A base type contains all of the characteristics and behaviours that are shared among the types derived from it.  You create a base type to represent the core of your ideas about some objects in your system.  From the base type, you derive other types to express the different ways that this core can be realized.

inheritance is like "cloning" and then making additions to/modifying a class, except that if the original class (/base/ or /super/ or /parent/ class) is changed, the modified "clone" (called the /derived/ or /inherited/ or /sub/ or /child/ class) also reflects those changes.

when you inherit, you say: "this new class is an old one, plus a few additions and modifications" (or "with a few restrictions").  The compiler effectively makes a copy of the original class into the new one and allows you to add or modify members without corrupting the original class code.
inheritance 1) conserves coding effort, and you don't have to make a copy of the original code for your new project
inheritance 2) the object-oriented concept of /subclassing/ helps the programmer organize a solution to make it easy to maintain and extend.

object-oriented programming extends object-based programming to provide for type/subtype relationships.  This is achieved through a mechanism referred to as /inheritance/.

inheritance is used to represent commonality among concepts

Dynamic binding

In C++ inheritance facilitates polymorphism and dynamic binding.  Dynamic binding ensures that the code that is executed is always consistent with the type of the object

Advantages of polymorphism and dynamic binding
allow old code to call new code in a substitutable fashion
polymorphism and dynamic binding enable the ability to treat objects of derived classes as if they were objects of the base class

Dynamic binding is the ability to resolve at run-time which operation to execute based on the acual type of the polymorphic object.  In C++, this is handled through the virtual function mechanism.

The phrase /dynamic binding/ captures the notion that functions may be bound at run time, as opposed to static bindings that happen at compile time.

*Dynamic binding* refers to the ability to select at run time which function to run based on the actual type of the object on which the function is called.  Dynamic binding is in effect for calls to virtual functions made through a pointer or a reference.  The fact that a function is virtual is inherited, and need not be repeated in the derived classes.

Heap/Stack

dynamic allocation is also known as allocating from the heap

Abstraction/Encapsulation

Encapsulation protects abstractions.  Encapsulation is the bodyguard; abstraction is the VIP.

Encapsulation provides the explicit boundary between an object's abstract interface (its abstraction) and its internal implemenation details.

Abstraction provides business value; encapsulation "protects" these abstractions.

Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behaviour; encapsulation serves to separate the contractual inteface of an abstraction and its implementation.

Information Hiding

Together with namespaces, classes are also a primary mechanism for information hiding.

information hiding separating the public class interface from the private implementation

information hiding: division between the public interface and private implementation of a class
benefits of information hiding p33 LL

/information hiding/ is used within the class definition to declare the internal representation and implementation of a class as private, whereas the operations to be performed on class objects by the program are public.  The private internal representation is said to be /encapsulated/, and the public portion of the class is referred to as the /class interface/.
/information hiding/ is a formal mechanism for preventing the functions of a program to access directly the internal representation of a class type.

removing data from public view is called /data hiding/

information hiding protects user code from changes to the class representation.  also, the internal state of the class object is protected from random program modification

Composition/Aggregation

Composition (sometimes called aggregation) is the process of putting an object (a part) inside another (the composite).  It models the has-a relationship (equivalently, part-of).  Do not use inheritance (kind-of) when you should use composition

composition: create member objects of other classes inside your new class (an object can be composed of other objects) "this class is some of these, and some of those, and one of these"

/composition/ (more generally, aggregation): "has-a" relationship, composing a new class from existing classes
look to composition before inheritance when creating new classes, composition is simpler and more flexible

Composition vs Inheritance

composition is generally used when you want the features of an existing class inside your new class, but not its interface.  That is, you embed an object to implement features of your new class, but the user of your new class sees the interface you've defined rather than the interface from the original class

Scope

Scope is the mechanism by which programmers can limit the visibility of declarations in their programs

C++ supports three forms of scope: local scope, namespace scope, and class scope

keep scopes as small as possible so the visibility and lifetime of your objects are as small as possible

Templates

container classes (templates) are used to create objects that hold other objects

arrays are dangerous, use template container classes instead

templates share source code among structurally similar families of classes and functions.  Many data structures and algorithms can be defined independently of the type of data they manipulate.  A template allows the separation of the type-dependent part from the type-independent part.  The result is a significant amount of code sharing.

templates support the design of parameterized types

a class template is a prescription for creating a class in which one or more types or values are parameterized

C++

C++ is good because: large user community, multiparadigm language, performance, legacy code access

C++ strong static type checking

Bibliography

Sources: Eckel (1993), Lippman (1996), Meyers (1996), Stroustrup (1997), Cline, Lomow and Girou (1998), Lippman and Lajoie (1998), Meyers (1998), Eckel (2000), Koenig and Moo (2000), Glassborow (2004) and Booch, et al. (2007)





Martin Sewell