C++ Text
C++ Text
C++ Text
It
was developed by Bjarne Stroustrup, AT&T Bell
Laboratories in Murray Hill, New Jersey, USA, in the
early 1980’s. Stroustrup, an admirer of Simuala67
and a strong supporter of C, wanted to combine the
best of both the languages and create a more
powerful language that could support object-
oriented programming features and still retain the
power and elegance of C. the result was C++.
Therefore, C++ is an extension of C with a major
addition of the class construct feature of Simual67.
Since the class was a major addition to the original
C language, Stroustrup initially called the new
language ‘C with classes’. However, later in 1983,
the name was changed to C++. The idea of C++
comes from the C increment operator ++, thereby
suggesting that C++ is an augmented (incremented)
version of C.
During the early 1990’s the language underwent a
number of improvements and changes. In
November 1997, the ANSI/ISO standards committee
standardized these changes and added several new
features to the language specifications.
C++ is a superset of C. Most of what we already
know about C applies to C++ also. Therefore, almost
all C programs are also C++ programs. However,
there are a few minor differences that will prevent
a C program to run under C++ compiler. We shall
see these differences later and when they are
encountered . a comprehensive list of major
differences between ANSI and ANSI C++ is given in
Appendix A.
The most important facilities that C++ adds on to C
are classes, inheritance , function overloading, and
operator overloading. These features enable
creating of abstract data type inherit properties
from existing data types and support
polymorphism, thereby making C++ truly object-
oriented language.
The object-oriented features in C++ allow
programmers to build large programs with clarity,
extensibility and ease of maintenance,
incorporating the spirit and efficiency of C. the
addition of new features has transformed C from a
language that currently facilitate top-down,
structured design, to one that provides bottom-up,
object-oriented design.
Applications of C++ :- C++ is a versatile language for
handling very large programs. It is suitable for
virtually programming task including development
of editors, compilers, databases, communication
systems and any complex real-life application
systems.
Since C++ allows us to create hierarchy-related
objects, we can build special object oriented
libraries which can be used later by many
programmers.
While C++ is able to map the real-would
problem properly, the C part of C++ gives the
language the ability to get close to the machine-
level details.
C++ programs are easily maintainable and
expandable. When a new feature need to be
implemented, it is very easy to add to the
existing structure of an object.
It is expected that C++ will replace C as a
general-purpose language in the near future.
A Simple C++ Program:- Let us begin with a simple
example of a C++ program that prints a string on
the screen.
#include<iostream>//include header file
Using namespace std;
int main()
{
cout<<”C++ is better than C.”;//C++ statement
return 0;
}// End of example
This simple program demonstrates several C++
features.
Program Features:- Like C, the C++ program is a
collection of functions. The above example contains
only one function, main(). As usual, execution
begins at main(). Every C++ program must have a
main(). C++ is a free-form language. With a few
exceptions, the compiler ignores carriage returns
and white spaces. Like C, the C++ statements
terminate with semicolons.
Commets:- C++ introduces a new comment symbol
//(double slash). Commets start with a double slash
symbol and terminate at the end of the line. A
comment may start anywhere in the line, and
whatever follows till the end of the line is ignored.
Note that there is no closing symbol.
The double slash comment is basically a single line
comment. Multiline comments can be written as
follows;
//this is an example of
//C++ program to illustrate
//some of its features
The C comment symbols /*,*/ are still valid and are
more suitable for multiline comments. He following
comment is allowed:
/* this is an example of C++ program to illustrate
some of its features*/
We can use either or both styles in our programs.
Since this is a book on C++, we will use only the C++
style. However, remember that we can not insert a
// style comment within the text of a program line.
For example, the double slash comment cannot be
used in the manner as shown below:
For(j=0;j<n;/*loops n times*/j++)
Output Operator:- the only statement in Program is
an output statement. The statement
cout<<”C++ is better than C”;
causes the string in quotation marks to be displayed
on he screen. This statement introduces two new
C++ features, cout and <<. The identifier cout
(pronounced as ‘C out’) is a predefined object that
represents the standard output stream in C++.
Here, the standard output stream represents the
screen. It is also possible to redirect the output to
other output devices. We shall later discuss streams
in detail.
The operator << is called the insertion or put to
operator. It inserts (or sends) the contents of the
variable on its right to the object on its left.
keyboard
cout “C++”
cin 45.5
keyboard
Include files
Class declaration
Member functions definitions
Main function program
It is a common practice to organize a program into
three separate files. The class declarations are
placed in a header file and the definitions of
member functions go into another file. This
approach enables the programmer to separate the
abstract specification of the interface (class
definition) from the implementation details
(member functions definitions). Finally, the main
program that uses the class is placed in a third file
which “includes” the previous two files as well as
any other files required.
This approach is based on the concept of client-
server model as shown in fig. the class definition
including the member functions constitute the
server that provides services to the main program
known as client. The client uses the server through
the public interface of the class.
Member functions
Class definition
Server
client
Main function program