Saturday, 26 May 2012

C++


Use of C++
C++ is used by hundreds of thousands of programmers in essentially every application domain. This use is
supported by about a dozen independent implementations, hundreds of libraries, hundreds of textbooks,
several technical journals, many conferences, and innumerable consultants. Training and education at a
variety of levels are widely available.
Early applications tended to have a strong systems programming flavor. For example, several major
operating systems have been written in C++ [Campbell,1987] [Rozier,1988] [Hamilton,1993] [Berg,1995]
[Parrington,1995] and many more have key parts done in C++. C++ was designed so that every language
feature is usable in code under severe time and space constraints [Stroustrup,1994]. This allows C++ to be
used for device drivers and other software that rely on direct manipulation of hardware under real-time constraints.
In such code, predictability of performance is at least as important as raw speed. Often, so is compactness
of the resulting system.
Most applications have sections of code that are critical for acceptable performance. However, the largest
amount of code is not in such sections. For most code, maintainability, ease of extension, and ease of
testing is key. C++’s support for these concerns has led to its widespread use where reliability is a must and
in areas where requirements change significantly over time. Examples are banking, trading, insurance,
telecommunications, and military applications. For years, the central control of the U.S. long-distance telephone
system has relied on C++ and every 800 call (that is, a call paid for by the called party) has been
routed by a C++ program [Kamath,1993]. Many such applications are large and long-lived. As a result,
stability, compatibility, and scalability have been constant concerns in the development of C++. Millionline
C++ programs are not uncommon.
Like C, C++ wasn’t specifically designed with numerical computation in mind. However, much numerical,
scientific, and engineering computation is done in C++. A major reason for this is that traditional
numerical work must often be combined with graphics and with computations relying on data structures
that don’t fit into the traditional Fortran mold [Budge,1992] [Barton,1994]. Graphics and user interfaces
are areas in which C++ is heavily used.
All of this points to what may be C++’s greatest strength: its ability to be used effectively for applications
that require work in a variety of application areas. It is quite common to find an application that
involves local and wide-area networking, numerics, graphics, user interaction, and database access. Traditionally,
such application areas have been considered distinct, and they have most often been served by distinct
technical communities using a variety of programming languages. However, C++ has been widely
used in all of those areas. Furthermore, it is able to coexist with code fragments and programs written in
other languages.
C++ is widely used for teaching and research. This has surprised some who – correctly – point out that
C++ isn’t the smallest or cleanest language ever designed. However, C++ is
– clean enough for successful teaching of basic concepts,
– realistic, efficient, and flexible enough for demanding projects,
– available enough for organizations and collaborations relying on diverse development and execution
environments,
– comprehensive enough to be a vehicle for teaching advanced concepts and techniques, and
– commercial enough to be a vehicle for putting what is learned into non-academic use.
Thanks to the ISO standards process (§2), C++ is also well-specified, stable, and supported by a standard library.

Strings in C++


Strings and I/O
Strings and input/output operations are not provided directly by special language constructs in C++.
Instead, the standard library provides string and I/O types. For example:
#i n c l u d e <s t r i n g > // make standard strings available
#i n c l u d e <i o s t r e a m > // make standard I/O available
i n t m a i n ()
{
u s i n g n a m e s p a c e s t d ;
s t r i n g n a m e ;
c o u t << "P l e a s e e n t e r y o u r n a m e : "; // prompt the user
c i n >> n a m e ; // read a name
c o u t << "H e l l o , " << n a m e << ´\ n ´; // output the name followed by a newline
r e t u r n 0 ;
}
This example uses the standard input and output streams, c i n and c o u t with their operators >> (‘‘get
from’’) and << (‘‘put to’’).
The I/O streams support a variety of formatting and buffering facilities, and strings support common
string operations such as concatenation, insertion, and extraction of characters and strings. Both streams
and strings can be used with characters of any character set.
The standard library facilities can be – and usually are – implemented using only facilities available to
all users. Consequently, where the standard facilities happen to be inadequate, a user can provide equally
elegant alternatives.

C++



Compile, Link, and Execute
Traditionally, a C or a C++ program consists of a number of source files that are individually compiled into
object files. These object files are then linked together to produce the executable form of the program.
Each separately compiled program fragment must contain enough information to allow it to be linked
together with other program fragments. Most language rules are checked by the compiler as it compiles an
individual source file (translation unit). The linker checks to ensure that names are used consistently in different
compilation units and that every name used actually refers to something that has been properly
defined. The typical C++ runtime environment performs few checks on the executing code. A programmer
who wants run-time checking must provide the tests as part of the source code.
C++ interpreters and dynamic linkers modify this picture only slightly by postponing some checks until
the first use of a code fragment.
For example, I might write a simple factorial program and represent it as a separate source file f a c t .c :
/ / file fact.c:
#i n c l u d e "f a c t .h "
l o n g f a c t (l o n g f ) // recursive factorial
{
i f (f >1 )
r e t u r n f *f a c t (f -1 );
e l s e
r e t u r n 1 ;
}
A separately compiled program fragment has an interface consisting of the minimal information needed to
use it. For this simple f a c t .c program fragment, the interface consists of the declaration of f a c t () stored in
a file f a c t .h :
/ / file fact.h:
l o n g f a c t (l o n g );
The interface is #i n c l u d e d in each translation unit that uses it. I also tend to #i n c l u d e an interface into the
translation unit that defines it to give the compiler a chance to diagnose inconsistencies early.
The f a c t () function can now be used like this:

/ / file main.c:
#i n c l u d e "f a c t .h "
#i n c l u d e <i o s t r e a m >
i n t m a i n ()
{
s t d :: c o u t << "f a c t o r i a l (7 ) i s " << f a c t (7 ) << ´\ n ´;
r e t u r n 0 ;
}


Saturday, 12 May 2012

Introdution of C++


C++ Design Aims
C++ was designed to deliver the flexibility and efficiency of C for systems programming together with
Simula’s facilities for program organization (usually referred to as object-oriented programming). Great
care was taken that the higher-level programming techniques from Simula could be applied to the systems
programming domain. That is, the abstraction mechanisms provided by C++ were specifically designed to
be applicable to programming tasks that demanded the highest degree of efficiency and flexibility.
These aims can be summarized:

Aims:
C++ makes programming more enjoyable for serious programmers.
C++ is a general-purpose programming language that
– is a better C
– supports data abstraction
– supports object-oriented programming
– supports generic programming

Support for generic programming emerged late as an explicit goal. During most of the evolution of C++, I
presented generic programming styles and the language features that support them (§4.4) under the heading
of ‘‘data abstraction.’’


The Design and Evolution of C++
C++ was designed and implemented by Bjarne Stroustrup (the author of this article) at AT&T Bell Laboratories
to combine the organizational and design strengths of Simula with C’s facilities for systems programming.
The initial version of C++, called ‘‘C with Classes’’ [Stroustrup,1980], was first used in 1980;
it supported traditional system programming techniques (§3) and data abstraction (§4.1). The basic facilities
for object-oriented programming (§4.2-4.3) were added in 1983 and object-oriented design and programming
techniques were gradually introduced into the C++ community. The language was first made
commercially available in 1985 [Stroustrup,1986] [Stroustrup,1986b]. Facilities for generic programming
(§4.4) were added to the language in the 1987-1989 time frame [Ellis,1990] [Stroustrup,1991].
As the result of widespread use and the appearance of several independently-developed C++

implementations, formal standardization of C++ started in 1990 under the auspices of the American
National Standards Institute, ANSI, and later the International Standards Organization, ISO, leading to an
international standard in 1998 [C++,1998]. During the period of standardization the standards committee
acted as an important focus for the C++ community and its draft standards acted as interim definitions of
the language. As an active member of the standards committee, I was a key participant in the further evolution
of C++. Standard C++ is a better approximation to my ideals for C++ than were earlier versions. The
design and evolution of C++ is documented in [Stroustrup,1994] [Stroustrup,1996] and [Stroustrup,1997b].
The language as it is defined at the end of the standardization process and the key design and programming
techniques it directly supports are presented in [Stroustrup,1997].
An Overview of the C++ Programming Language

1 Introduction and Overview
The C++ programming language provides a model of memory and computation that closely matches that of
most computers. In addition, it provides powerful and flexible mechanisms for abstraction; that is, language
constructs that allow the programmer to introduce and use new types of objects that match the concepts
of an application. Thus, C++ supports styles of programming that rely on fairly direct manipulation
of hardware resources to deliver a high degree of efficiency plus higher-level styles of programming that
rely on user-defined types to provide a model of data and computation that is closer to a human’s view of
the task being performed by a computer. These higher-level styles of programming are often called data
abstraction, object-oriented programming, and generic programming.
This paper is organized around the main programming styles directly supported by C++:

§2 The Design and Evolution of C++ describes the aims of C++ and the principles that guided its evolution.
§3 The C Programming Model presents the C subset of C++ and other C++ facilities supporting traditional
systems-programming styles.
§4 The C++ Abstraction Mechanisms introduces C++’s class concept and its use for defining new types
that can be used exactly as built-in types, shows how abstract classes can be used to provide interfaces
to objects of a variety of types, describes the use of class hierarchies in object-oriented programming,
and presents templates in support of generic programming.
§5 Large-Scale Programming describes namespaces and exception handling provided to ease the composition
of programs out of separate parts.
§6 The C++ Standard Library presents standard facilities such as I/O streams, strings, containers (e.g.
v e c t o r , l i s t , and m a p ), generic algorithms (e.g. s o r t (), f i n d (), f o r _ e a c h ()) and support for
numeric computation.
To round off, a brief overview of some of the tasks that C++ has been used for and some suggestions for
further reading are given.