Why Do We Need Object-Oriented
Programming?
Object-oriented programming was developed because limitations were discovered in
earlier approaches to programming. To appreciate what OOP does, we need to understand
what these limitations are and how they arose from traditional programming
languages.
Procedural Languages
C, Pascal, FORTRAN, and similar languages are procedural languages. That is, each
statement in the language tells the computer to do something: Get some input, add
these numbers, divide by six, display that output. A program in a procedural language
is a list of instructions.
For very small programs, no other organizing principle (often called a paradigm) is needed.
The programmer creates the list of instructions, and the computer carries them out.
Division into Functions
When programs become larger, a single list of instructions becomes unwieldy. Few
programmers can comprehend a program of more than a few hundred statements
unless it is broken down into smaller units. For this reason the function was adopted
as a way to make programs more comprehensible to their human creators. (The term
function is used in C++ and C. In other languages the same concept may be referred
to as a subroutine, a subprogram, or a procedure.) A procedural program is divided
into functions, and (ideally, at least) each function has a clearly defined purpose and a
clearly defined interface to the other functions in the program.
Real-World Modeling
The second—and more important—problem with the procedural paradigm is that its
arrangement of separate data and functions does a poor job of modeling things in the
real world. In the physical world we deal with objects such as people and cars. Such
objects aren’t like data and they aren’t like functions. Complex real-world objects
have both attributes and behavior.
Attributes
Examples of attributes (sometimes called characteristics) are, for people, eye color
and job title; and, for cars, horsepower and number of doors. As it turns out, attributes
in the real world are equivalent to data in a program: they have a certain specific values,
such as blue (for eye color) or four (for the number of doors).
Behavior
Behavior is something a real-world object does in response to some stimulus. If you
ask your boss for a raise, she will generally say yes or no. If you apply the brakes in a
car, it will generally stop. Saying something and stopping are examples of behavior.
Behavior is like a function: you call a function to do something (display the inventory,
for example) and it does it.
So neither data nor functions, by themselves, model real-world objects effectively.
Basic Program Construction
Let’s look at a very simple C++ program. This program is called FIRST, so its source file is
FIRST.CPP. It simply prints a sentence on the screen. Here it is:
#include <iostream>
using namespace std;
int main()
{
cout << “Every age has a language of its own\n”;
return 0;
}
Despite its small size, this program demonstrates a great deal about the construction of C++
programs. Let’s examine it in detail.
Functions
Functions are one of the fundamental building blocks of C++. The FIRST program consists
almost entirely of a single function called main(). The only parts of this program that are not
part of the function are the first two lines—the ones that start with #include and using. (We’ll
see what these lines do in a moment.)
We noted in Chapter 1, “The Big Picture,” that a function can be part of a class, in which case
it is called a member function. However, functions can also exist independently of classes. We
are not yet ready to talk about classes, so we will show functions that are separate standalone
entities, as main() is here.
Function Name
The parentheses following the word main are the distinguishing feature of a function. Without
the parentheses the compiler would think that main refers to a variable or to some other program
element. When we discuss functions in the text, we’ll follow the same convention that
C++ uses: We’ll put parentheses following the function name. Later on we’ll see that the
parentheses aren’t always empty. They’re used to hold function arguments: values passed from
the calling program to the function.
The word int preceding the function name indicates that this particular function has a return
value of type int. Don’t worry about this now; we’ll learn about data types later in this chapter
and return values in Chapter 5, “Functions.”
Braces and the Function Body
The body of a function is surrounded by braces (sometimes called curly brackets). These
braces play the same role as the BEGIN and END keywords in some other languages: They surround
or delimit a block of program statements. Every function must use this pair of braces
around the function body. In this example there are only two statements in the function body:
the line starting with cout, and the line starting with return. However, a function body can
consist of many statements.
Always Start with main()
When you run a C++ program, the first statement executed will be at the beginning of a function
called main(). (At least that’s true of the console mode programs in this book.) The program
may consist of many functions, classes, and other program elements, but on startup,
control always goes to main(). If there is no function called main() in your program, an error
will be reported when you run the program.