Friday, 6 March 2015

Encapsulation

Encapsulation is the mechanism that binds together code and the data it manipulates,
and keeps both safe from outside interference and misuse. In an object-oriented
language, code and data may be combined in such a way that a self-contained "black
box" is created. When code and data are linked together in this fashion, an object is
created. In other words, an object is the device that supports encapsulation.
Within an object, code, data, or both may be private to that object or public. Private
code or data is known to and accessible only by another part of the object. That is,
private code or data may not be accessed by a piece of the program that exists outside
the object. When code or data is public, other parts of your program may access it even
though it is defined within an object. Typically, the public parts of an object are used to
provide a controlled interface to the private elements of the object.
For all intents and purposes, an object is a variable of a user-defined type. It may
seem strange that an object that links both code and data can be thought of as a
variable. However, in object-oriented programming, this is precisely the case. Each
time you define a new type of object, you are creating a new data type. Each specific
instance of this data type is a compound variable.

What Is Object-Oriented Programming?

Since object-oriented programming (OOP) drove the creation of C++, it is necessary to
understand its foundational principles. OOP is a powerful way to approach the job of
programming. Programming methodologies have changed dramatically since the
invention of the computer, primarily to accommodate the increasing complexity of
programs. For example, when computers were first invented, programming was done
by toggling in the binary machine instructions using the computer's front panel. As
long as programs were just a few hundred instructions long, this approach worked. As
programs grew, assembly language was invented so that a programmer could deal
with larger, increasingly complex programs, using symbolic representations of the
machine instructions. As programs continued to grow, high-level languages were
introduced that gave the programmer more tools with which to handle complexity.
The first widespread language was, of course, FORTRAN. Although FORTRAN was a
very impressive first step, it is hardly a language that encourages clear, easy-tounderstand
programs.
The 1960s gave birth to structured programming. This is the method encouraged by
languages such asCand Pascal. The use of structured languages made it possible to write
moderately complex programs fairly easily. Structured languages are characterized by
their support for stand-alone subroutines, local variables, rich control constructs, and
their lack of reliance upon the GOTO. Although structured languages are a powerful tool,
even they reach their limit when a project becomes too large.
Consider this: At each milestone in the development of programming, techniques
and tools were created to allow the programmer to deal with increasingly greater
complexity. Each step of the way, the new approach took the best elements of the
previous methods and moved forward. Prior to the invention of OOP, many projects
were nearing (or exceeding) the point where the structured approach no longer worked. Object-oriented methods were created to help programmers break through
these barriers.
Object-oriented programming took the best ideas of structured programming
and combined them with several new concepts. The result was a different way of
organizing a program. In the most general sense, a program can be organized in
one of two ways: around its code (what is happening) or around its data (who is being
affected). Using only structured programming techniques, programs are typically
organized around code. This approach can be thought of as "code acting on data." For
example, a program written in a structured language such as C is defined by its
functions, any of which may operate on any type of data used by the program.
Object-oriented programs work the other way around. They are organized
around data, with the key principle being "data controlling access to code." In an
object-oriented language, you define the data and the routines that are permitted
to act on that data. Thus, a data type defines precisely what sort of operations can
be applied to that data.
To support the principles of object-oriented programming, all OOP languages
have three traits in common: encapsulation, polymorphism, and inheritance. Let's examine each.

The Origins of C++

C++ began as an expanded version of C. The C++ extensions were first invented
by Bjarne Stroustrup in 1979 at Bell Laboratories in Murray Hill, New Jersey. He
initially called the new language "C with Classes." However, in 1983 the name was
changed to C++.
Although C was one of the most liked and widely used professional programming
languages in the world, the invention of C++ was necessitated by one major programming
factor: increasing complexity. Over the years, computer programs have become
larger and more complex. Even though C is an excellent programming language, it has
its limits. In C, once a program exceeds from 25,000 to 100,000 lines of code, it becomes
so complex that it is difficult to grasp as a totality. The purpose of C++ is to allow this
barrier to be broken. The essence of C++ is to allow the programmer to comprehend
and manage larger, more complex programs.
Most additions made by Stroustrup to C support object-oriented programming,
sometimes referred to as OOP. (See the next section for a brief explanation of objectoriented
programming.) Stroustrup states that some of C++'s object-oriented features
were inspired by another object-oriented language called Simula67. Therefore, C++
represents the blending of two powerful programming methods.
Since C++ was first invented, it has undergone three major revisions, with each
adding to and altering the language. The first revision was in 1985 and the second in
1990. The third occurred during the standardization of C++. Several years ago, work
began on a standard for C++. Toward that end, a joint ANSI (American National
Standards Institute) and ISO (International Standards Organization) standardization
committee was formed. The first draft of the proposed standard was created on
January 25, 1994. In that draft, the ANSI/ISO C++ committee (of which I am a member)
kept the features first defined by Stroustrup and added some new ones as well. But in
general, this initial draft reflected the state of C++ at the time.
Soon after the completion of the first draft of the C++ standard, an event occurred
that caused the language to be greatly expanded: the creation of the Standard Template
Library (STL) by Alexander Stepanov. The STL is a set of generic routines that you can
use to manipulate data. It is both powerful and elegant, but also quite large. Subsequent to the first draft, the committee voted to include the STL in the specification for C++. The
addition of the STL expanded the scope of C++ well beyond its original definition. While
important, the inclusion of the STL, among other things, slowed the standardization
of C++.
It is fair to say that the standardization of C++ took far longer than anyone had
expected when it began. In the process, many new features were added to the language
and many small changes were made. In fact, the version of C++ defined by the C++
committee is much larger and more complex than Stroustrup's original design.
However, the standard is now complete. The final draft was passed out of committee
on November 14, 1997. A standard for C++ is now a reality.
The material in this book describes Standard C++, including all of its newest
features. This is the version of C++ created by the ANSI/ISO standardization
committee, and it is the one that is currently accepted by all major compilers

NOW I WILL START TO EXPLAIN LANGUAGE C++