Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword or section
Like this
136Activity
P. 1
Robert Lafore

Robert Lafore

Ratings: (0)|Views: 14,816|Likes:
Published by api-3742886

More info:

Published by: api-3742886 on Oct 15, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

 
C++ Interactive Course by Robert Lafore,
Pdf by:
Venkat Reddy
Page 1 of 701
CHAPTER 1A FIRST LOOL AT OOP AND C++
Welcome to the exciting world of object-oriented programming! In this first chapter, I’ll start by discussing whyobject-oriented programming (OOP) was invented and why it offers advantages to the programmer. I’ll also providea quick overview of the main features of object-oriented languages. You’ll learn about the two most fundamentalaspects of OOP, objects and classes. Then I’ll focus on a particular kind of object—hot dog stand—and show howreal hot dog stands on the street relate to hot dog stand objects in a program. You’ll see how to use C++ to describea class of hot dog stand objects and how to make these objects carry out tasks.This approach goes to the very heart of OOP. Most books begin by skittering around the edges of C++, talking aboutold-fashioned procedural details. This one attacks objects and classes head-on. If you think of OOP as a fierce fire-breathing dragon, then you’re going to walk right up to it, look it squarely in the eye, and tell it you want answers,now!
Session 1: Why Do We Need OOP?
In this session, I’ll discuss, in a general way, how object-oriented programming arrived on the scene. OOP wasdeveloped because limitations were discovered in earlier approaches to programming. To appreciate what OOPdoes, you need to understand what these limitations are and how they arose from traditional programminglanguages.
Procedural Languages
Pascal, C, BASIC, Fortran, and similar traditional programming languages are
 procedural
languages. That is, eachstatement in the language tells the computer to
do
something: Get some input, add these numbers, divide by 6,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 createsthe 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 aprogram 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 functionis used in C++ and C. In other languages, the same concept may be called a
subroutine
, a
subprogram
, or a
 procedure
.) A program is divided into functions and—ideally, at least—each function has a clearly defined purposeand a clearly defined interface to the other functions in the program.The idea of breaking a program into functions can be extended by grouping a number of functions together into alarger entity called a
module
, but the principle is similar: a grouping of instructions that carry out specific tasks.Dividing a program into functions and modules is one of the cornerstones of 
structured programming
, the somewhatloosely defined discipline that has influenced programming design for several decades.
Problems with Structured Programming
As programs grow ever larger and more complex, even the structured programming approach begins to show signsof strain. You may have heard about, or been involved in, horror stories of program development. The project is toocomplex, the schedule slips, more programmers are added, complexity increases, costs skyrocket, the schedule slipsfurther, and disaster ensues (see
The Mythical Man-Month
, by Frederick P. Brooks, Jr., Addison-Wesley, 1982, for avivid description of this scenario).Analyzing the reasons for these failures reveals weaknesses in the procedural paradigm itself. No matter how wellthe structured programming approach is implemented, large programs become excessively complex.What are the reasons for this failure of procedural languages? One of the most crucial is the role played by data.
 
C++ Interactive Course by Robert Lafore,
Pdf by:
Venkat Reddy
Page 2 of 701In a procedural language, the emphasis is on doing things—read the keyboard, invert the vector, check for errors,and so on. The subdivision of a program into functions continues this emphasis. Functions
do things
, just as singleprogram statements do. What they do may be more complex or abstract, but the emphasis is still on the action.What happens to the data in this paradigm? Data is, after all, the reason for a program’s existence. The importantpart of an inventory program isn’t a function that displays the data or a function that checks for correct input; it’s theinventory data itself. Yet data is given second-class status in the organization of procedural languages.For example, in an inventory program, the data that makes up the inventory is probably read from a disk file intomemory, where it is treated as a global variable. By
global,
I mean that the variables that constitute the data aredeclared outside of any function so they are accessible to all functions. These functions perform various operationson the data. They read it, analyze it, update it, rearrange it, display it, write it back to the disk, and so on.I should note that most languages, such as Pascal and C, also support
local
variables, which are hidden within asingle function. But local variables are not useful for important data that must be accessed by many differentfunctions. Figure 1-1 shows the relationship between global and local variables.
 Global and local variablesSuppose a new programmer is hired to write a function to analyze this inventory data in a certain way. Unfamiliarwith the subtleties of the program, the programmer creates a function that accidentally corrupts the data. This is easyto do, because every function has complete access to the data. It’s like leaving your personal papers in the lobby of your apartment building: Anyone can change or destroy them. In the same way, global data can be corrupted byfunctions that have no business changing it.Another problem is that, because many functions access the same data, the way the data is stored becomes critical.The arrangement of the data can’t be changed without modifying all the functions that access it. If you add new dataitems, for example, you’ll need to modify all the functions that access the data so that they can also access these newitems. It will be hard to find all such functions and even harder to modify all of them correctly. It’s similar to whathappens when your local supermarket moves the bread from aisle 4 to aisle 12. Everyone who patronizes thesupermarket must figure out where the bread has gone and adjust their shopping habits accordingly. The relationshipof functions and data in procedural programs is shown in Figure 1-2.
 
C++ Interactive Course by Robert Lafore,
Pdf by:
Venkat Reddy
Page 3 of 701
 The procedural paradigmWhat is needed is a way to restrict access to the data, to
hide
it from all but a few critical functions. This will protectthe data, simplify maintenance, and offer other benefits, as you’ll see.
Relationship to the Real World
Procedural programs are often difficult to design. The problem is that their chief components—functions and datastructures—don’t model the real world very well. For example, suppose you are writing code to create the elementsof a graphics user interface: menus, windows, and so on. Quick now, what functions will you need? What datastructures? The answers are not obvious, to say the least. It would be better if windows and menus correspondedmore closely to actual program elements.
New Data Types
There are other problems with traditional languages. One is the difficulty of creating new data types. Computerlanguages typically have several built-in data types: integers, floating-point numbers, characters, and so on. What if you want to invent your own data type? Perhaps you want to work with complex numbers, or two-dimensionalcoordinates, or dates—quantities the built-in data types don’t handle easily. Being able to create your own types iscalled
extensibility
because you can extend the capabilities of the language. Traditional languages are not usuallyextensible. Without unnatural convolutions, you can’t bundle both x and y coordinates into a single variable called
Point 
and then add and subtract values of this type. Traditional programs are more complex to write and maintain.
The Object-Oriented Approach
The fundamental idea behind object-oriented languages is to combine into a single program entity both
data
and
the functions that operate on that data
. Such an entity is called an
object 
.An object’s functions, called
member functions
in C++ (because they belong to a particular class of objects),typically provide the only way to access its data. If you want to read a data item in an object, you call a memberfunction in the object. It will read the item and return the value to you. You can’t access the data directly. The data is
hidden
, so it is safe from accidental alteration. Data and its functions are said to be
encapsulated 
into a single entity.
 Encapsulation
and
data hiding
are key terms in the description of object-oriented languages.If you want to modify the data in an object, you know exactly what functions interact with it: the member functionsin the object. No other functions can access the data. This simplifies writing, debugging, and maintaining theprogram.A C++ program typically consists of a number of objects that communicate with each other by calling one another’smember functions. Figure 1-3 shows the organization of a C++ program.

Activity (136)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
safasaba liked this
shripad_potnis added this note
thanks for uploading its a good learning book
Pankaj soni liked this
Nagender Singh liked this
ashu580 liked this
Ign Sious liked this
captainpradipta liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->