2 Programming with ANSI C++
to work. Although the portions of the program (modules) are separated, these
are not totally independent. If a module is to be used at multiple places with a
little difference, then it is to be copied and pasted at all such locations and then
modifi ed. As a result, the program becomes messy. Another problem with large
programs is that they are usually developed by teams and effi cient teamwork
is not possible without a global view, that is, an abstracted view. For example,
while handling a marksheet printing program, a student is an abstraction. We shall assume
that all the data that we need for a student is available in the student abstraction (we will
soon call this abstraction a class) and will not worry about any other details of the student.
There may be hundreds of such abstractions available, but we should only be using them and
not worry about their internal structure. Thus, we are relieved from the burden of handling
a large program to one with only a few abstractions. In addition, we can view the entire
program together and can properly reason if a part of it is not working properly and is
required to be modifi ed.
Let us try to understand the same concept using an example from our everyday life.
Suppose we write something using Microsoft Word. We just use the buttons to save, undo,
and so on, without caring about how they work. Let us consider one more example from the
programming domain. The application programming for Windows may be done using VB
or Visual C++ (VC++), but we can use all facilities such as buttons, text boxes, or tables as
controls without worrying about how these controls are implemented or coded. What we get
is a control (for example, a text box control) that is programmed for readymade usage. This
simplifi es our task of programming. This is also true for real-time examples such as driving
a car, when we are only worried about the steering wheel, brakes, accelerators, and a few
other things and not about how acceleration is actually provided when we press the pedal.
Unfortunately, C does not provide any direct mechanism for such abstraction.
Not Designed for Reusability
There is another problem with the style of programming in C. We need many building blocks
for a large program. For example, a large payroll program would require all the information
about each employee. Management of employee information is one building block for
the payroll program. A human resource management program also requires the employee
information. Both sets of information may not be exactly the same, though both of them need
to be consistent. It would be a better approach to share the information as much as possible
between all those who need it, and provide additional information only in special cases. In
our case, the employee information is needed by the employee payroll and human resource
departments. In such case, the employee name, address, etc., can be stored at a
single location. From this shared pool, both payroll and personnel departments
may take the data input. Additional information can be inserted in the individual
records of both the departments wherever required. For example, if employee
experience is additionally required in the human resource information, then it is
stored in its database alone. Here, the idea is to reuse the shared information that
is available to all, with little modifi cation at local level. C does not provide any
mechanism to reuse the same information with modifi cations wherever required.
In this chapter, we will see that both these problems encountered in C are
very easily and effi ciently solved by C++, which follows the paradigm of object
orientation. When the new design was termed object-oriented or object-based,
Lengthy C programs
lose a ‘global view’
and become very diffi -
cult to visualize as a
single concept.
Two serious issues
emerge when large
pro grams are to be
written in C. The pro-
grammer loses a
global view of the pro-
gram and there is no
programming construct
available for reusing
the older code.