Part 3

Object-Oriented Terminology

Object-oriented programming

Classes

Friends

Polymorphism

Copyright © 1992-1997 Thomas P. Sturm

CLASSES AND OBJECTS IN C++

C++ is object oriented - can define objects, and the operators and functions to process them

C++ allows the definition of classes - a way of grouping similar data items or objects, can act as an object “factory”

data abstraction - allows designers to think about the types of operations they would like to provide for a data item, rather than the statements that the programming language supports

methods - are the functions and operators for manipulating data in an abstract data type in a controlled way and for sending messages to objects

messages - are the parameters passed and the values returned in a controlled way from objects

polymorphism - allow different classes to respond to the same message in different (but hopefully analogous) ways

encapsulation - allows for the creation of an impenetrable barrier between the internals of the class instance/object and the outside programming environment so that only intended behavior can occur

inheritance and derived classes - allows the definition of subclasses that can inherit the properties (functions and data) of the base class

friends - allows the definition of functions in one class that can access data that would normally be restricted in another class

templates - allows the definition of rules to generate functions and classes parametrically as needed

OBJECT ORIENTATION

A CLASS is a group of related objects

An OBJECT is an entity that has behavior with which you can communicate - it may have data associated with it. It can also be used to implement an abstract data type, in which case it is a data type encapsulated by functions

Classes contain functions that are MEMBERS of the class. If there are data items in the class, they can be restricted so that they can only be accessed through the member functions

Another name for the functions within a class that access the data is a METHOD

The process of insuring that you can only get at a data item through the class functions is called ENCAPSULATION

The operators used on the data within the class (via functions that define the operator's behavior) are PROTOCOLS

The values that are passed to and from the member functions to set, alter, and retrieve encapsulated data are called MESSAGES

OBJECT-ORIENTED PROGRAMMING

Structured programming focuses on the organization of process

Object-oriented programming focuses on the organization of data

The "Copernican Revolution" in data processing dictates that we focus on a data-centered universe

In structured programming, we want

- control over which modules execute which code

- discipline in the logical flow

- ability to place frequently used code in standard modules

- no "back doors" or "side effects" in the code

This allows us to think abstractly about what a standard module does

By analogy, in object-oriented programming, we want

- control over which modules access which data

- discipline in the transfer of data values

- ability to create new data types

(with enforceable rules, in a standard, portable package)

- no "back doors" or "side effects"

(data values can't be altered without our knowledge)

This allows us to think abstractly about how a data item is processed

CLASSES

A class is a way of controlling access to (encapsulating) data

What is encapsulated is a set of data declarations and a set of functions (methods) for accessing and updating the data

Functions within a class are divided into categories.

- Public functions/operators can be accessed from "outside"

- Private functions/operators can be accessed only from within the class

Subclasses can be defined (derived) from other (base) classes

Subclasses can share (inherit) the operations from the base class (parent class) they were derived from

Classes and subclasses can be organized into hierarchies of inclusion, with the most general (abstract) class at the root of the hierarchy tree

The functions defined within the class are called methods

The data within the class that is encapsulated is put in the private area. This data cannot be accessed directly from an arbitrary C++ function. However, functions declared within the class definition have unrestricted access to the private data

The public functions declared within the class are the means (methods) by which data can be inserted, altered, manipulated, and accessed by "the world"

DERIVED CLASSES

Object-oriented programming languages provide a whole new way of handling "slight" variation. A class that differs in some respects from another, but represents a “subset” or “refinement” of the class, can be derived from the original (base) class.

The whole concept of object-oriented design and programming hinges upon the ability to properly construct a set of classes the have the appropriate sharing of data and functions.

For example, if we have both permanent and contract employees, we can extract the common information, name and age, into a base class. Then, we will construct two derived classes, one of which will add the employee number for permanent employees, and the other will add the service time for contract employees.

We don't wish to write three sets of code, however. We wish to write one set of code for the common information, and then one set of code for each set of unique information.

The class hierarchy is graphed as follows:

Person

PermanentContract

POLYMORPHISM

  • The concept of polymorphism allows different objects to respond to a message in different (but analogous) ways. In the case of shapes, we can define an abstract base class shape. From this, we can derive two classes, TRIANGLE and RECTANGLE. TRIANGLE and RECTANGLE both need to respond to messages for area and perimeter, but they each need to do so in their own way.
  • As much functionality should be brought back into the base class as possible to avoid duplication of effort and provide a single point of change. For example, a function to add the areas of two shapes should be defined in the base class.
  • However, the base class needs to call (the correct version of) the area function in implementing any function to add the areas of two shapes. Thus, it is not sufficient to define perimeter and area in the derived classes - they must also be “defined” in the base class.

To simplify support of a variety of data types, it would be helpful to define a function that would do the "right" thing to a set of data, regardless of data type.

While C++ cannot intuit what the "right" thing might be, it does allow functions with the same name to execute different blocks of code, depending on the data type of the argument.

Use of the same function name to perform different tasks, depending on parameter types, is known as function overloading.

Principles of good program design and object-oriented design dictate that functions with the same name do comparable things to the various data types they accept as parameters.

Notice that since the overloaded functions do similar things, it is possible to save a lot of coding by allowing an overloaded function to call another one of its "versions" with parameters of a different type.

Overloaded functions MUST be different in the parameters they take. Two functions that are indistinguishable in parameters, but differ only in their return type are NOT allowed to be overloaded. (A calling function that recast the return value to void so as to ignore the return value would not know which version to call.)

FRIENDS

Somethimes we think we need a way for a function to access the members of a class without making the class members public.

We need to do that without making the function part of the class in three situations:

1. A function needs to access data from two or more classes. In this case, the function is not a member of either class. It must be declared as a friend of both if they both have needed private data.

2. A function in one class needs access to data in another class. It must be declared as a friend of the other class from which the private data is needed.

3. Member notation needs to be avoided. This is handled like case 1.

When a function is declared as a friend of another function, the friend function is allowed to access the private members of the class.

The use of friends is not always the best solution. Usually writing “getter” and “setter” functions is better than creating friends. Careful object-oriented design can yield class hierarchies that eliminate or reduce the use of friends.

The use of friends tends to compromise the whole idea of data encapsulation. As a result, their use is controversial. While not as objectionable as global variables, it ranks near it.

Lab 1 for Part 3

1. Compare and contrast the concept of class and object.

2. Compare and contrast the concept of protocol and message.

3. Compare and contrast the concept of polymorphism and (function and operator) overloading.

Object-Oriented TerminologyPart 3, Page 1