Introduction to C++
Structure of the Unit :
2.0 Objective
2.1 Introduction
2.2 A simple C++ Program
2.3 Structure of C++ Program
2.4 Creating the Source File
2.5 Compiling and Linking
2.6 C++ Streams
2.7 C+ + Stream Classes
2.8 Summary
2.9 Self
Assessment Question
2.0 Objective
After studying this unit you
will be able to understand :
n About
C++ programming including how to create, compile & link program.
n The
structure of C++ program and creating source file.
n About
C++ streams and buffering.
2.1 Introduction
Like C, C++
began its life at AT&T Bell Labs, New Jersey, USA where Bjarne Stroustrup
developed the language in the early 1980s. Its main purpose was to make writing
good programs easier and more pleasant for the individual programmer.
A computer
simulation language called Simula 67 inspired C++’s OOP aspect. Stroustrup
added OOP features to C without significantly changing the C component. Thus,
C++ is a superset of C, meaning that any valid C program is valid C++ program,
too. There are some minor discrepancies, but nothing crucial. C++ programs can
use existing C software libraries. Libraries are collection of programming
modules that you can call up from a program. The name C++ comes from the C
increment operator ++, which adds 1 to the value of a variable. The name C++
correctly suggests an augmented version of C. While the OOP aspect of C++gives
the language the ability to relate concepts involved in the problem, the C part
of C++ gives the language the ability to get close to the hardware. The three
most important facilities that C++ adds on to C are classes, function
overloading and operator overloading. These features enable us to create abstract
data types, inherit properties from existing data types and suppor
polymorphism, thus making C++ a truly object oriented language.
2.2 A simple C++ Program
Let us begin with a simple
example of C++ program.
#include<iostream.h >
// include header file
using namespace std;
int main()
{
cout << “Hello World”
<< endl;
return (0);
}
The following is the output of
the above example
Hello World
Program Feature
C++ follows in the footsteps of C where there is the concept
of the kernel of the language and an additional set of library routines. The
#include line is an instruction to the compiler to make available to the
following program unit what is defined in iostream.h. There is the concept of
compiler preprocessing in C and C++ programs. The # indicates a preprocessor
directive. The < > characters are used to indicate a standard language
header file, in this case iostream.h. I/O is not available in the kernel of the
language. It is made available by the inclusion of iostream.h in the complete
program.
The next line
is the start of the program itself. All programs are called main in C++. There
is also the concept of every program unit being a function. Functions in C++
either return a value (and in this case we are defining main to return an
integer value) or not. If we do not want a function to return a value we use
void rather than a data type in conjunction with the function name to indicate
this.
The next thing of interest is the {
character which indicates the start of the program. The next statement cout
(pronounced see out) prints some text to the standard out stream or screen.
Text is delimited in C++ with “ ” marks, endl is predefined in C++ to generate
an end of line. The ‘<<‘ are C++ operators. They are used to separate
items in the output stream. ; is the statement separator or terminator in C++.
Finally the program terminates with the return(0) statement. The ‘)’ character
signifies the end of the program.
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. We can say comments are non-executable statements ignored
by compiler at the time of compilation.
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.
The following comment is allowed
/* This is an example of C++ program to illustrate some of its
features */
2.3 Structure of C++ 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 definition). 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. 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.
Basic structure of a C++ program
#include <iostream.h> int main()
{
declaration1; declaration 2;
.
.
execution statement 1; execution statement 2;
.
.
return (0);
}
2.4 Creating the Source File
Like
C programs, C++ programs can be created using any text editor. For example, on
the UNIX, we can use vi or ed text editor for creating and editing the source
code. On the DOS system, we can use edlin or any other editor available or a
word processor system under non-document mode.
Some systems
such as Turbo C++ provide an integrated environment for developing and editing
programs. Appropriate manuals should be consulted for complete details.
The
file name should have a proper file extension to indicate that it is a C++
program file. C++ implementations use extensions such as .c, .C, .cc, .cpp and
.cxx. Turbo C++ and Borland C++ use .c for C programs and .cpp (C plus plus)
for C++ programs. Zortech C++ system uses .cxx while UNIX AT&T version uses
.C (capital C) and .cc. The operating system manuals should be consulted to
determine the proper file name extensions to be used.
2.5 Compiling and Linking
The process of
compiling and linking again depends upon the operating system. A few popular
systems are discussed in this section.
Unix AT&T C++
The process of
implementation of a C++ program under UNIX is similar to that of a C program.We
should use the "CC" (uppercase) command to compile the program.
Remember, we use lowercase "cc" for compiling C programs. The command
at the UNIX prompt would compile the C++ program source code contained in the
file example.C. The compiler would produce an object file example.o and then
automatically link with the library functions to produce an executable file.
The default executable filename is a.out.
A program spread over multiple
files can be complied as follows:
CC
file1. C file2.o
The statement
compiles only the file filel.C and links it with the previously compiled
file2.o file.This is useful when only one of the files needs to be modified.
The files that are not modified need not be compiled again.
Turbo C++ and Borland C++
Turbo C++ and
Borland C++ provide an integrated program development environment under MS DOS.
They provide a built-in editor and a menu bar which includes options such as
File, Edit, Compile and Run.
We can create
and save the source files under the File option, and edit them under the Edit
option. We can then compile the program under the Compile option and execute it
under the Run option. The Run option can be used without compiling the source
code. In this case, the RUN command causes the system to compile, link and run
the program in one step.
Visual C++
It is a Microsoft application development system for C++ that
runs under Windows. Visual C++ is a visual programming environment in which
basic program components can be selected through menu choices, buttons, icons,
and other predetermined methods.
2.6 C++ Streams
The I/O system in C++ is designed to work
with a wide variety of devices including terminals, disks, and tape drives. Although
each device is very different, the I/O system supplies an interface to the
programmer that is independent of the actual device being accessed. This
interface is known as stream.
A stream is a
sequence of bytes. It acts either as a source from which the input data can be
obtained or as a destination to which the output data can be sent. The source
stream that provides data to the program is called the input stream and the
destination stream that receives output from the program is called the output
stream. In other words, a program extracts the bytes from an input stream and
inserts bytes into an output stream .
Fig 2.2 Data Streams
The
data in the input stream can come from the keyboard or any other storage
device. Similarly, the data in the output stream can go to the screen or any
other storage device. As mentioned earlier, a stream acts as an interface
between the program and the input/output device. Therefore a C++ program
handles data (input or output) independent of the devices used.
C++
contains several pre-defined streams that are automatically opened when a
program begins its execution. These include cin and cout which have been used
very often in our earlier programs. We know that cin represents the input
stream connected to the standard input device (usually the keyboard) and cout
represents the output stream connected to the standard output device (usually
the screen). Note that the keyboard and the screen are default options. We can
redirect streams to other devices or files, if necessary.
2.7 C++ Stream Classes
The C++ I/O
system contains a hierarchy of classes that are used to define various streams
to deal with both the console and disk files. These classes are called stream
classes. Figure 2.3 shows the hierarchy of the stream classes used for input
and output operations with the console unit. These classes are declared in the
header file iostream. This file should be included in all the programs that
communicate with the console unit.
, ios is
the base class for istream (input stream) and ostream (output stream) which are, in turn, base classes for iostream
(input/output stream). The class ios is declared as the virtual base class so
that only one copy of its members are inherited by the iostream.
The class
ios provides the basic support for formatted and unformatted I/O operations.
The class istream provides the facilities for formatted and unformatted input
while the class ostream (through inheritance) provides the facilities for
formatted output. The class iostream provides the facilities for handling both
input and output streams. Three classes , namely, istream_withassign,
ostream_withassign, and
iostream_withassign add assignment operators to these classes.
The >> operator is overloaded in the istream class and
<< is overloaded in the ostream class. The following is the general
format for reading data from the keyboard:
cin>>
variable1 >> variable2 >> ………………….>> variableN
Variable1, variable2,... are valid C++ variable names that
have been declared already. This statement will cause the computer to stop the
execution and look for input data from the keyboard. The input data for this
statement would be:
data1
data2 …….. dataN
The input data are separated by white spaces and should
match the type of variable in the cin list. Spaces, newlines and tabs will be
skipped.
The operator >>
reads the data character by character and assigns it to the indicated location.
The reading for a variable will be terminated at the encounter of a white space
or a character that does not match the destination type. For example, consider
the following code:
int code;
cin
>> code;
Suppose the following data in given as input:
4258
D
The operator will read the characters upto 8 and the value
4258 is assigned to code. The character D remains in the input stream and will
be input to the next cin statement. The
general form for displaying data on the screen is:
cout << item1<< item2 <<
.... << itemN
The items item1 through itemN may be variables or constants
of any basic type. We have used such statements in a number of examples
illustrated in previous units.
2.8 Summary
C++ is a
superset of C language. C++ adds a number of object oriented features such as
objects, inheritance, function
overloading and operator overloading. C++ program begins at main(). The header
file iostream should be included at
the beginning of all programs.
2.9 Self Assessment Questions
1. Write
a program to display the following output using a single cout statement
Physics = 77 ;
Chemestry = 69
2. Write
a program to read two numbers from the keyboard and display the large value on
the screen.
3. Write
a C++ program that will ask for a temperature in Fahrenheit and display in
Celsiums.
4.
How does a main () function in C++ differ from main( )
in C?
Contact--
Ankit Pundir
(ankitpundir623@gmail.com)
No comments:
Post a Comment