Let Us C++
Let Us C++
Let Us C++
Intro to OOP
1
2 Let Us C++
The Beginning...
The earliest computers were programmed in binary. Mechanical
switches were used to load programs. With the advent of mass
storage devices and larger and cheaper computer memories, the
first high-level computer programming languages came into
Chapter 1: Intro to OOP 3
(b) The control was transferred via the dangerous goto statement.
As a result, there was too much jumping around in the
program, often without any clear indication of how, where
and why the control is flowing.
Structured Programming
To overcome the limitations mentioned above, a quest began to
develop new languages with new features that would help to create
more sophisticated applications. The breakthrough occurred in late
60’s and early 70’s with the introduction of structured
programming. The long programs that the programmer found
difficult to comprehend could now be broken down into smaller
units of few hundred statements. Functions/subroutines/procedures
were introduced in these languages to make the programs more
comprehensible to their human creators. A program could now be
divided into functions, with each function having a clearly defined
purpose and a clearly defined interface to the other functions in the
program.
If you want to modify the data in an object, you know exactly what
functions interact with it: the member functions in the object. No
other functions can access the data. This simplifies writing,
debugging, and maintaining the program.
Object
8 Let Us C++
Data
Member Function
Member Function
Object Object
Data Data
Member Function Member Function
Member Function Member Function
Figure 1.1
There is more to OOP programming than just binding the data and
functions together. OOP, for example, facilitates creating reusable
code that can eventually save a lot of work. A feature called
polymorphism permits you to create multiple definitions for
operators and functions. Another feature called inheritance permits
Chapter 1: Intro to OOP 9
you to derive new classes from old ones. As you can see, OOP
introduces many new ideas and involves a different approach to
programming than the procedural programming. In short, instead
of concentrating on tasks, you concentrate on representing
concepts.
Objects
In structured programming a problem is approached by dividing it
into functions. Unlike this, in object-oriented programming the
problem is divided into objects. Thinking in terms of objects rather
than functions makes the designing of program easier. Following
are few candidates that can be treated as objects in different
programming situations:
− Employees in a payroll processing system
− Data structures like linked lists, stacks, queues etc.
− GUI elements like windows, menus, icons etc.
− Hardware devices like disk drive, keyboard, printer, etc.
− Various elements in computer games like cannons, guns,
animals, etc.
− Customers, sales persons in a sales tracking system
− Computers in a network model
− Etc.
Classes
10 Let Us C++
Most languages offer primitive data types like int, long and float.
Their data representation and response to arithmetic, assignment
and relational operators are defined as part of the language.
However, the language does not know user-defined data types.
The programmer defines its format and behaviour by defining a
class. For example, there can be a user-defined data type to
represent dates. The compiler and the computer do not know about
dates. Programmers have to define the behaviour of dates by
designing a date class. This class expresses the format of date and
the operations that can be performed on it. The way we can declare
many variables of the primitive type int, we can define many
objects of the date class. A class serves as a blueprint or a plan or a
template. It specifies what data and what functions will be
included in objects of that class. Defining the class doesn’t create
any objects, just as the mere existence of a type int doesn’t create
any variables.
Inheritance
OOP permits you to create your own data types (classes) just like
the types built into the language. However, unlike the built-in data
types, the user-defined classes can use other classes as building
blocks. Using a concept called inheritance new classes can be built
from the old ones. The new class referred to as a derived class, can
inherit the data structures and functions of the original, or the base
class. The new class can add data elements and functions to those
it inherits from its base class.
Feature A
Feature B
Publication class
Feature A Feature A
Feature B Feature B
Feature C Feature E
Periodical class Book class
Figure 1.2
Polymorphism
Extending the same example of the publication, periodical and
book, let us now understand another important concept. Our base
class, publication, defines methods for storing and retrieving data.
A periodical may be stored in a binder, while a book is usually
placed on a shelf. Furthermore, the way to find a specific
periodical is different from finding a book. Periodicals are located
through a guide to periodical literature, while books are found
12 Let Us C++
Reusability
Object-oriented programs are built from reusable software
components. Once a class is completed and tested, it can be
distributed to other programmers for use in their own programs.
This is called reusability. If those programmers want to add new
features or change the existing ones, new classes can be derived
from existing ones. The tried and tested capabilities of base classes
do not need to be redeveloped. Programmers can devote time to
writing new code instead of wasting time in rewriting existing
code. This way software becomes easier to test since programming
errors can be isolated within the new code of derived classes.
For example, you might have written (or purchased from someone
else) a class that creates a menu system. You are happy with the
working of this class and you don’t want to change it, but you
want to add the capability of displaying context sensitive help for
each menu item. To do this, you simply create a new class that
Chapter 1: Intro to OOP 13
inherits all the capabilities of the existing one but adds context
sensitive help. The ease with which existing software can be
reused is a major benefit of OOP.
Exercise
[A] State True or False: