Introduction to Programming in C++

This essay Introduction to Programming in C++ has a total of 10536 words and 57 pages.

Introduction to Programming in C++

A program is a sequence of instructions for a computer to execute. Every program is written
in some programming language. The C++ (pronounced “see-plus-plus”) language is one of the
newest and most powerful programming languages available. It allows the programmer to write
efficient, structured, object-oriented programs.
This chapter introduces some of the basic features of C++. You should compile and run each
example in this chapter.
1.1 SIMPLE PROGRAMS
Our first example illustrates the main parts of a C++ program.
EXAMPLE 1.1 The Hello World Program
#include <iostream.h>
// This program prints "Hello, World."
main0
1
tout CC "Hello, World.n";
return 0;
The #include directive on the first line is necessary for the program to have output. It refers to an
external file named i o s t ream. h where information about the cou t object is provided. Note that the
angle brackets < and > are not part of the file name; they are used to indicate that this is a Standard C++
Library file.
The second line is a comment, identified by the double slashes / /. Comments are included in programs
to provide explanations for human readers. They are ignored by the compiler.
The third line contains the function header main ( ) . This is required for every C++ program. It tells
the compiler where the program begins. The parentheses ( > following main are also required.
The fourth and seventh lines contain only the braces { and }. These enclose the body of the
main ( > function and are also required for every C++ program.
The fifth line contains the statement
tout << "Hello, World.n";
This tells the system to send the message II He1 lo, War Id. n " to the tout (“see-out”) object. That
object is the standard output stream which usually represents the computer display screen. The name
cou t stands for “console output.” The output should look like this:
1
2 INTRODUCTION TO PROGRAMMING IN C++ [CHAP. 1
The n symbol is the newline symbol. Note that this single symbol is formed from the two characters ‘ ’
and ‘n'. Putting this symbol at the end of the quoted string tells the system to begin a new line after printing
the preceding characters, thus ending the current line.
The sixth line contains the statement return 0. That terminates the execution of the program and
returns control to the computer’s operating system. The number 0 is used to signal that the program has
ended successfully.
The output statement on the fifth line includes several common C++ symbols. The symbol <C is
called the output operator or the insertion operator. It inserts the message into the output stream. The
symbol n included at the end of the message stands for the newline character. Whenever it appears in
an output message, it causes the current line of output to be terminated, thereby starting a new line. Note
that both of these symbols (< < and n) require two characters, side-by-side, with no space between
them.
Note the semicolon ; at the ends of the fifth and sixth lines. C++ requires every statement to end
with a semicolon. It need not be at the end of a line. We may put several statements on the same line, and
we may have one statement extend over several lines. But no matter how it is positioned on one or more
lines, every statement must end with a semicolon.
We can imagine the relationship of the tout obiect to the program and the displav screen like this:
Hello, World.
I#include <iostream.h>
main0
.{
tout << "Hello, World.n";
>
The output stream cou t acts as a conduit, piping the output from the program to the display screen (or
printer or other output device), byte by byte.
The program in Example 1.1 is not minimal. Only some of its parts are required for every
program. In fact, a C++ program need not have any statements. Of course, such an “empty program”
will not do anything. The next example shows the shortest possible C++ program.
EXAMPLE 1.2 The Shortest C++ Program
main0 0
This “empty program” does nothing. It simply reveals the required skeleton for every C++ program.
The return 0; statement is not required by most compilers. Some compilers will issue a
warning if it is omitted. We include it in each example in this first chapter.
It is also recommended that you include at the beginning of every program a brief comment
that describes what the program does.
CHAP. l] INTRODUCTION TO PROGRAMMING IN C++
1.2 THE OUTPUT OPERATOR
The symbol C< is called the insertion operator or the output operator. It inserts objects

Read essay without registering

Donate an essay now and get the full essay emailed you




Acceptable files: .txt, .doc, .docx, .rtf

Email Address

Topics Related to Introduction to Programming in C++

Procedural programming languages, C++, Cross-platform software, Object-oriented programming languages, Variable, C++ classes, Const, External variable, MAD, ALGOL 68, D, C Sharp syntax