Prev Next

Objects

Introduction to objects

Think of a type as a factory for producing things. For example, type IntStack is a factory for producing stacks. Each thing that is produced has the same capabilities, and initially looks the same. For example, each can do a push, and each can do a pop.

An object is similar to the things produced by a type. It can be the value of a variable. It has certain capabilities, such as the ability to do certain functions. The difference is that an object carries its operations with it. For example, you might have several different objects that have the capability to do a print operations. Some of those objects will do one thing when asked to do a print, others will do something else.

When you write a function that uses an object, you typically ask that object to do a few specific things. For example, you might ask it to print and to make a copy of itself. What you exploit when you use an object is its signature. You require that it has the ability to do the operations that you ask it to do. You can imagine that your function will do something meaningful on any object that has the correct signature. Your function takes on new generality when used with objects. Instead of working on just one type of thing, it becomes polymorphic.

Like a member of an ADT, an object holds data. It keeps that data in a way that is invisible to other parts of the program. So an object becomes a convenient place to store a package of information. An object typically holds data and also carries functions that manipulate that data. Since the data and functions are packaged together, the rest of the program does not need to deal with the data at all. If the rest of the program wants something done with that data, it asks the object to do the job on its own data.

Syntax

Objects are supported by C++, not by C. C++ has special syntax for dealing with objects. If x is an object, and you want to ask x to perform operation op with parameters a, b and c, you write

  x.op(a,b,c);
If p is a pointer to an object, rather than being an object itself, then you can ask the object that p points to to perform the same operation as follows.
  p->op(a,b,c);

Object interfaces

Like an ADT, an object has a signature and a semantics. The signature tells the operations that the object is capable of doing, and the semantics tells what it will actually do.

In order to create an object, you must build a factory that builds objects. Such a factory is called a class, and is covered next. The interface to an object is given with the interface to its class.

Object implementations

The implementation of an object is given by the implementation of its class. This approach to objects has some advantages over creating objects one at a time. It always gives you the capability of creating more of the same kind of thing. Also, it yield quite efficient implementations of objects. You think of an object as carrying with it implementations of its operations. What actually happens is that the object simply carries with it the name (or number) of its class. Each time an operation is required, the class is consulted. This is far more efficient that having objects actually carry the operations with them.

Passing objects to functions

Objects typically implement destructive operations that modify the data stored in the object. Because of this, you rarely want to make a copy of an object. Instead, you refer, via pointers, to a single object.

To prevent copying, objects are usually passed by reference or by pointer to functions. If a function returns an object, it usually returns it by reference or by pointer.


Prev Next