UMBC CMSC202, Computer Science II, Fall 1998,
Sections 0101, 0102, 0103, 0104
2. Introduction to OOP
Thursday September 03, 1998
Assigned Reading: 2.1-2.4
Handouts (available on-line):
Programs from this lecture.
- Introduction to Object-Oriented Programming (OOP) and C++. First we
have to learn some terminology. Some of these terms describe concepts we
already know from non-OOP programming, but historically OOP advocates used
their own terminology and we are stuck with them. Some terms we will be
using include: members, private members, public members, objects, host
objects, classes, encapsulation, overloading, references, information
hiding, access control, data abstraction, methods, member function,
constructors, default constructors. It will take some time to get used to
using and hearing these words, but you should not be discouraged by mere
- Our first attempt at making objects.
- C++ is an extension of C. Anything you do in C, is allowed in C++,
but to take advantage of C++ and to follow an object-oriented approach, you
would do things differently in C++.
- One main difference between C++ and C is in the header files. To
define a new class of objects, you use the "class" directive. In our first
header file, we declare a new kind of
object called "Box". The "class" directive is a combination of function
prototypes and a type definition.
- In our example, the definition of the class Box includes function
prototypes for Box(), identify(), volume(),
grow(), shrink() and random(). These are called
"member functions" or "methods" in object-oriented lingo. They are just
- The definition of Box also includes data members: length,
height and width. These are just called "members" in C++
lingo. The correspond to the idea of fields of a record in C. For example,
if b1 has type Box, then the length field of the Box
b1 can be accessed using b1.length. However, since the
length field of Box is private, only member functions can access
the length, height and width members.
- The member functions in our Box class are implemented in a file called
box1.C. C++ programs usually have a .C
extension. On Unix, we use the CC command to compile C++ programs.
Otherwise, compiling C++ programs is very similar to compiling C programs.
Observe that the names of the member functions in the class Box are
prefaced with "Box::" in the implementations.
- Turning to the first main program we
can see how member functions in the class Box are called. First, we need
objects of type Box. In this program we create 3 objects of type Box in
Box b1, b2, b3 ;
Now, member functions can be called using the "." operator. The syntax is
the same as C syntax for a field in a record. One way to remember the
syntax for member functions is to pretend that each member function is a
field in a record. So, for example, the statement:
calls the function identify(). In this case, the object
b1 is called the host object. You can think of this statement as
a command to the object b1 to identify itself. The sample run demonstrates the effects of
executing our first C++ program.
- Returning to the implementations of the member functions in the file
box1.C, observe that the function
identify() refers to variables called length,
height and width. These are members (or fields) of the
host object b1. Since the function identify was called using
b1, the variable length refers to the length
member of b1 and not that of some other Box object. In other
respects, member functions are like ordinary C functions. You can have
local variables, you can have loops, you can call other functions, etc.
- The member function Box::Box is a special function called a
constructor. It is distinguished by two things. First, the name of the
function is the name of the class. Second, the function prototype does not
have a return type and is not allowed to have a return type, not even void.
This constructor is called whenever new objects of type Box are declared.
For example, in the statement above that created three new objects
b1, b2 and b3 of type Box, the Box constructor
was called three times. Each time, the Box constructor assigns random
values to the length, height and width members
of the new object. Thus, constructors provide a very convenient mechanism
for initializing objects as they are created.
- The member function random() is also different from other
member functions in our Box class. This function was declared in the
private section of the Box definition in the header file. Thus, random() can only
be called by other member functions in the Box class. So, a function call
would generate an error at compile time. Similarly, the length,
height and width fields are private, and references to
them outside the member functions would cause compile time errors. Our second main program and sample run demonstrate this.
- Our third main program using the Box
class shows that we can use the symbol "Box" like a type in a C program.
For example, we can declare an array with 4 elements of type Box using the
Box boxes ;
In this case, the constructor Box::Box is called 4 times and the
members of each Box object in the array is initialized using
random() as the sample run
shows. Note that we can also declare pointers to Box, using for example:
Box *bptr ;
Here, the analogy to the syntax for fields of records in C still holds. To
refer to the member function identify(), we can dereference the
pointer to Box using then choose a field using:
or we can use the -> operator:
- The new operator in C++ provides functionality that is
similar to a call to malloc(). In this program, the statement:
bptr = new Box[items] ;
creates a dynamically allocated array of Boxes. As before, each Box object
in the array is initialized by a call to the constructor function
Box::Box. If the new operator cannot make the array for
some reason (say the system is out of memory), then it returns NULL (just
like malloc()). As you would expect, since bptr is a
pointer to the first element of an array, we can use array notation like
bptr[i] to refer to an element of the array.
- The delete operation used at the end is analogous to free. It
releases the memory that bptr points to, which was previously
allocated by the new operation.
We looked briefly at an embellished version of our Box class in the header
file box2a.h. In this version, the
client (someone who uses the Box objects) is allowed to use an alternate
constructor to create Boxes with specified dimensions. Also, this
version of the Box class supports more operations, including
turn(), spin() and rotate(). A new data member
top is used to keep track of the direction that the Box is
point to. This example should convince you that in order to keep the
data about boxes consistent, it is best not to let a client manipulate
the data members directly. It would be too easy to make a mistake, say
by forgetting to update the orientation of the Box after turning it.
(See also, the main program and
29 Oct 1998 17:14:04 EST
to Fall 1998 CMSC 202 Section Homepage