Beginning with C++
Beginning with C++
2.1 What is C++?
C++ is an object-oriented programming language. It was developed by Bjarne Stroustrup at AT&T Bell Laboratories in Murray Hill, New Jersey, USA, in the early 1980's, Stroustrup, an admirer of Simula67 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 Simula67. 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 standardised 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 as and when they are encountered.
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 types, inherit properties from existing data types and support polymorphism, thereby making C++ a 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 facilitates top-down, structured design, to one that provides bottom-up, object-oriented design.
2.2 Applications of C++
C++ is a versatile language for handling very large programs. It is suitable for virtually any 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 buildspecial object oriented libraries which can be used later by many programmers.
- While C++ is able to map the real-world problem properly, the C part of C++ gives the language ability to get close to the machine-level details.
- C++ programs are easily maintainable and expandable. When a new feature needs to be implemented, it is very easy to add to the existing structure of an object.
- It is expected that C++ will replace Cas a general-purpose language in the near future.
2.3 A Simple C++ Program
Let us begin with a simple example of a C++ program that prints a string on the screen.
PRINTING A STRING
#include <iostream> // include header file
(Contd)
using namespace std;
coute "Certs better than C.; //C++statement
return 0;
//End of example
This simple program demonstrates several C++ features.
PROGRAM 2.1
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.
Comments
C++ introduces a new comment symbol // (double slash). Comments 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
//some of its features
The C comment symbols/", "/are still valid and are more suitable for multiline comments. The 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 2.1 is an output statement. The statement
cout<<"C++ is better than C.
causes the string in quotation marks to be displayed on the screen. This statement introduces two new C++ features, cout and << The identifier cout (pronounced as Cout) is a predefined object that represents the standard output stream in C++. Here, the standards 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 operators is called the insertion or pat zo operator. It inserts for sends) the contents of the variable on its right to the object on its left.
The object cout has a simple interface. If string represents a string variable, then the following statement will display its contents.
cout<<string
You may recall that the operator is the hit wise left shift operater and it can still be used for this purpose: This is an example of how one operator can be used for different purposes, depending on the context. This concept is known as operator overloading, an important aspect of polymorphism.
It is important to note that we can still use print for displaying an output. C++ accepta this notation. However, we will use cout oc to maintain the spirit of C++.
The lostream File
We have used the following include directive in the program:
#include <10stream
This directive causes the preprocessor to add the contents of the iostream file to the program. It contains declarations for the identifier cout and the operator << Some old versions of C++ use a header file called instream.h. This is one of the changes introduced by ANSI C++ (We should use instream h if the compiler does not support ANSI C++ features.)
The header file lostream should be included at the beginning of all programs that use input/output statements. Note that the naming conventions for header files may vary.
Some implementations use iostream.hpp: yet others lostream.hxx. We must include appropriate header film depending on the contents of the program and implementation.
Tables 2.1 and 2.2 provide lists of C++ standard library header files that may be needed in C++ programs. The header files with h extensium are "old style" files which should be used with old compilers. Table 2.1 abo gives the version of these files that should be used with the ANSI standard compilers.
Namespace




Comments
Post a Comment